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