summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@lukeshu.com>2018-08-16 01:19:05 -0400
committerLuke Shumaker <lukeshu@lukeshu.com>2018-08-16 01:19:05 -0400
commit93ba79d3eacfd1ba225d309a226540619f370b05 (patch)
treeeaa5896172efa00a1b7a5a66552a18da98159739
parentbd382780bbbe1f150288b24fe713820b2d3e7921 (diff)
osi-mk: Implement --edit --size
-rwxr-xr-xosi-mk25
1 files changed, 24 insertions, 1 deletions
diff --git a/osi-mk b/osi-mk
index d6ddd74..1d136b2 100755
--- a/osi-mk
+++ b/osi-mk
@@ -181,7 +181,30 @@ main() {
btrfstune -fu "$arg_file"
} |& sed "s|^|${prefix} |"
fi
- # TODO: resize according to $arg_size
+ if [[ -n $arg_size ]]; then
+ # Calculate sizes in exact bytes now, to avoid any discrepancies
+ # between truncate(1) and btrfs-filesystem(8).
+ tmpfile=$(mktemp -t -- "${0##*/}.XXXXXXXXXX")
+ trap "rm -- ${tmpfile@Q}" EXIT
+ truncate --reference="$arg_file" --size="$arg_size" -- "$tmpfile"
+ old_size=$(stat --format='%s' -- "$arg_file")
+ new_size=$(stat --format='%s' -- "$tmpfile")
+ rm -- "$tmpfile"
+ trap - EXIT
+
+ # Do the resize
+ arg_mountpoint=$(mktemp -dt -- "${0##*/}.XXXXXXXXXX")
+ trap "rmdir -- ${arg_mountpoint@Q}" EXIT
+ if (( new_size > old_size )); then
+ truncate --size="$new_size" -- "$arg_file"
+ sudo -- ./osi-mount --root -- "$arg_file" "$arg_mountpoint" btrfs filesystem resize max "$arg_mountpoint"
+ elif (( new_size < old_size )); then
+ sudo -- ./osi-mount --root -- "$arg_file" "$arg_mountpoint" btrfs filesystem resize "$new_size" "$arg_mountpoint"
+ truncate --size="$new_size" -- "$arg_file"
+ fi
+ rmdir -- "$arg_mountpoint" EXIT
+ trap - EXIT
+ fi
else
printf -v prefix "$(gettext -- '%s [format]')" "$NAME"
{