]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - INSTALL/ExtendPersistentImg.sh
docker file
[Ventoy.git] / INSTALL / ExtendPersistentImg.sh
1 #!/bin/bash
2
3 print_usage() {
4 echo 'Usage: ExtendPersistentImg.sh file size'
5 echo ' file persistent dat file'
6 echo ' size extend size in MB'
7 echo 'Examples:'
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)'
10 echo ''
11 }
12
13 if [ -z "$1" -o "$1" = "-h" ]; then
14 print_usage
15 exit 1
16 fi
17
18 if [ -z "$2" ]; then
19 print_usage
20 exit 1
21 fi
22
23 uid=$(id -u)
24 if [ $uid -ne 0 ]; then
25 print_err "Please use sudo or run the script as root."
26 exit 1
27 fi
28
29 if [ "$1" = "__vbash__" ]; then
30 shift
31 else
32 if readlink /bin/sh | grep -q bash; then
33 :
34 else
35 exec /bin/bash $0 "__vbash__" "$@"
36 fi
37 fi
38
39
40 file=$1
41 size=$2
42
43 if [ ! -f "$file" ]; then
44 echo "$file not exist."
45 exit 1
46 fi
47
48 if echo $size | grep -q "^-"; then
49 mode="Shrink"
50 size=${size:1}
51 else
52 mode="Extend"
53 fi
54
55 if echo $size | grep -q "[^0-9]"; then
56 print_usage
57 exit 1
58 fi
59
60 fsize=$(stat -c '%s' $file)
61
62 fsmod=$(expr $fsize % 1024)
63 if [ $fsmod -ne 0 ]; then
64 echo "File size of $file is not aligned by 1MB, please check."
65 exit 1
66 fi
67
68
69 fsMB=$(expr $fsize / 1024 / 1024)
70
71 if [ "$mode" = "Extend" ]; then
72 total=$(expr $fsMB + $size)
73 else
74 if [ $fsMB -le $size ]; then
75 echo "File size of $file is less than ${size}MB."
76 exit 1
77 fi
78 total=$(expr $fsMB - $size)
79 fi
80
81
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."
86 exit 1
87 fi
88
89 if which xfs_growfs >/dev/null 2>&1; then
90 cmd=xfs_growfs
91 else
92 echo 'xfs_growfs not found, please install xfsprogs first'
93 exit 1
94 fi
95 else
96 if which resize2fs >/dev/null 2>&1; then
97 cmd=resize2fs
98 else
99 echo 'resize2fs not found, please install e2fsprogs first'
100 exit 1
101 fi
102 fi
103
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"
107 sync
108 else
109 echo "$mode dat file... (current is ${fsMB}MB, reduce ${size}MB, finally ${total}MB)"
110 fi
111
112
113 freeloop=$(losetup -f)
114 losetup $freeloop "$file"
115
116 if [ "$cmd" = "resize2fs" ]; then
117 echo "$mode ext filesystem by resize2fs ..."
118 echo "resize2fs $freeloop ${total}M"
119 e2fsck -f $freeloop
120 resize2fs $freeloop ${total}M
121 ret=$?
122 else
123 echo "$mode xfs filesystem by xfs_growfs ..."
124 tmpdir=$(mktemp -d)
125 mount $freeloop $tmpdir
126 xfs_growfs $freeloop
127 ret=$?
128 umount $tmpdir && rm -rf $tmpdir
129 fi
130
131 losetup -d $freeloop
132
133 if [ $ret -eq 0 -a "$mode" = "Shrink" ]; then
134 echo "truncate persistent file ..."
135 truncate "$file" -s ${total}M
136 ret=$?
137 fi
138
139 echo ""
140 if [ $ret -eq 0 ]; then
141 echo "======= SUCCESS ========="
142 else
143 echo "======= FAILED ========="
144 fi
145 echo ""