]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - INSTALL/ExtendPersistentImg.sh
4 echo 'Usage: ExtendPersistentImg.sh file size'
5 echo ' file persistent dat file'
6 echo ' size extend size in MB'
8 echo ' sh ExtendPersistentImg.sh ubuntu.dat 2048 - This command would extend ubuntu.dat by 2048MB (2GB)'
9 echo ' sh ExtendPersistentImg.sh ubuntu.dat -2048 - This command reduces ubuntu.dat by 2048MB (-2GB)'
13 if [ -z "$1" -o "$1" = "-h" ]; then
24 if [ $uid -ne 0 ]; then
25 print_err
"Please use sudo or run the script as root."
29 if [ "$1" = "__vbash__" ]; then
32 if readlink
/bin
/sh
| grep -q bash
; then
35 exec /bin
/bash
$0 "__vbash__" "$@"
43 if [ ! -f "$file" ]; then
44 echo "$file not exist."
48 if echo $size | grep -q "^-"; then
55 if echo $size | grep -q "[^0-9]"; then
60 fsize
=$(stat -c '%s' $file)
62 fsmod
=$(expr $fsize % 1024)
63 if [ $fsmod -ne 0 ]; then
64 echo "File size of $file is not aligned by 1MB, please check."
69 fsMB
=$(expr $fsize / 1024 / 1024)
71 if [ "$mode" = "Extend" ]; then
72 total
=$(expr $fsMB + $size)
74 if [ $fsMB -le $size ]; then
75 echo "File size of $file is less than ${size}MB."
78 total
=$(expr $fsMB - $size)
82 magic
=$(hexdump -n3 -e '3/1 "%02X"' $file)
83 if [ "$magic" = "584653" ]; then
84 if [ "$mode" = "Shrink" ]; then
85 echo "Shrink is not supported for XFS filesystem."
89 if which xfs_growfs
>/dev
/null
2>&1; then
92 echo 'xfs_growfs not found, please install xfsprogs first'
96 if which resize2fs
>/dev
/null
2>&1; then
99 echo 'resize2fs not found, please install e2fsprogs first'
104 if [ "$mode" = "Extend" ]; then
105 echo "$mode dat file... (current is ${fsMB}MB, append ${size}MB, total ${total}MB)"
106 dd if=/dev
/zero bs
=1M count
=$size status
=none
>> "$file"
109 echo "$mode dat file... (current is ${fsMB}MB, reduce ${size}MB, finally ${total}MB)"
113 freeloop
=$(losetup -f)
114 losetup
$freeloop "$file"
116 if [ "$cmd" = "resize2fs" ]; then
117 echo "$mode ext filesystem by resize2fs ..."
118 echo "resize2fs $freeloop ${total}M"
120 resize2fs
$freeloop ${total}M
123 echo "$mode xfs filesystem by xfs_growfs ..."
125 mount
$freeloop $tmpdir
128 umount
$tmpdir && rm -rf $tmpdir
133 if [ $ret -eq 0 -a "$mode" = "Shrink" ]; then
134 echo "truncate persistent file ..."
135 truncate
"$file" -s ${total}M
140 if [ $ret -eq 0 ]; then
141 echo "======= SUCCESS ========="
143 echo "======= FAILED ========="