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