]> glassweightruler.freedombox.rocks Git - Ventoy.git/blobdiff - INSTALL/ExtendPersistentImg.sh
1.1.02 release
[Ventoy.git] / INSTALL / ExtendPersistentImg.sh
index 21c8435aefbc3c936257e38e5bd86031bf792114..5f8b852f5f6330e2cfe6eac232573bb8affdcbc9 100644 (file)
@@ -1,11 +1,12 @@
-#!/bin/sh
+#!/bin/bash
 
 print_usage() {
     echo 'Usage:  ExtendPersistentImg.sh file size'
     echo '   file   persistent dat file'
     echo '   size   extend size in MB'
-    echo 'Example:'
-    echo '   sh ExtendPersistentImg.sh ubuntu.dat 2048'
+    echo 'Examples:'
+    echo '   sh ExtendPersistentImg.sh ubuntu.dat 2048 - This command would extend ubuntu.dat by 2048MB (2GB)'
+    echo '   sh ExtendPersistentImg.sh ubuntu.dat -2048 - This command reduces ubuntu.dat by 2048MB (-2GB)'
     echo ''
 }
 
@@ -19,6 +20,23 @@ if [ -z "$2" ]; then
     exit 1
 fi
 
+uid=$(id -u)
+if [ $uid -ne 0 ]; then
+    print_err "Please use sudo or run the script as root."
+    exit 1
+fi
+
+if [ "$1" = "__vbash__" ]; then
+    shift
+else
+    if readlink /bin/sh | grep -q bash; then
+        :
+    else
+        exec /bin/bash $0 "__vbash__" "$@"
+    fi
+fi
+
+
 file=$1
 size=$2
 
@@ -27,6 +45,13 @@ if [ ! -f "$file" ]; then
     exit 1
 fi
 
+if echo $size | grep -q "^-"; then
+    mode="Shrink"
+    size=${size:1}
+else
+    mode="Extend"
+fi
+
 if echo $size | grep -q "[^0-9]"; then
     print_usage
     exit 1
@@ -42,10 +67,25 @@ fi
 
 
 fsMB=$(expr $fsize / 1024 / 1024)
-total=$(expr $fsMB + $size)
+
+if [ "$mode" = "Extend" ]; then
+    total=$(expr $fsMB + $size)
+else
+    if [ $fsMB -le $size ]; then
+        echo "File size of $file is less than ${size}MB."
+        exit 1
+    fi
+    total=$(expr $fsMB - $size)
+fi
+
 
 magic=$(hexdump -n3 -e  '3/1 "%02X"' $file)
 if [ "$magic" = "584653" ]; then
+    if [ "$mode" = "Shrink" ]; then
+        echo "Shrink is not supported for XFS filesystem."
+        exit 1
+    fi
+
     if which xfs_growfs >/dev/null 2>&1; then
         cmd=xfs_growfs
     else
@@ -61,23 +101,26 @@ else
     fi
 fi
 
+if [ "$mode" = "Extend" ]; then
+    echo "$mode dat file... (current is ${fsMB}MB, append ${size}MB, total ${total}MB)"
+    dd if=/dev/zero bs=1M count=$size status=none >> "$file"
+    sync
+else
+    echo "$mode dat file... (current is ${fsMB}MB, reduce ${size}MB, finally ${total}MB)"
+fi
 
-echo "Extend dat file... (current is ${fsMB}MB, append ${size}MB, total ${total}MB)"
-dd if=/dev/zero bs=1M count=$size status=none >> "$file"
-sync
 
 freeloop=$(losetup -f)
 losetup $freeloop "$file"
 
 if [ "$cmd" = "resize2fs" ]; then    
-    echo "Extend ext filesystem by resize2fs ..."
+    echo "$mode ext filesystem by resize2fs ..."
     echo "resize2fs $freeloop ${total}M"
     e2fsck -f $freeloop
     resize2fs $freeloop ${total}M
     ret=$?
 else
-    echo "Extend xfs filesystem by xfs_growfs ..."
-
+    echo "$mode xfs filesystem by xfs_growfs ..."
     tmpdir=$(mktemp -d)
     mount $freeloop $tmpdir
     xfs_growfs $freeloop
@@ -87,6 +130,12 @@ fi
 
 losetup -d $freeloop
 
+if [ $ret -eq 0 -a "$mode" = "Shrink" ]; then
+    echo "truncate persistent file ..."
+    truncate "$file" -s ${total}M
+    ret=$?
+fi
+
 echo ""
 if [ $ret -eq 0 ]; then
     echo "======= SUCCESS ========="
@@ -94,4 +143,3 @@ else
     echo "======= FAILED ========="
 fi
 echo ""
-