]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - INSTALL/tool/VentoyWorker.sh
update
[Ventoy.git] / INSTALL / tool / VentoyWorker.sh
1 #!/bin/sh
2
3 . ./tool/ventoy_lib.sh
4
5 print_usage() {
6 echo 'Usage: Ventoy2Disk.sh CMD [ OPTION ] /dev/sdX'
7 echo ' CMD:'
8 echo ' -i install ventoy to sdX (fail if disk already installed with ventoy)'
9 echo ' -I force install ventoy to sdX (no matter installed or not)'
10 echo ' -u update ventoy in sdX'
11 echo ''
12 echo ' OPTION: (optional)'
13 echo ' -r SIZE_MB preserve some space at the bottom of the disk (only for install)'
14 echo ' -s enable secure boot support (default is disabled)'
15 echo ' -g use GPT partition style, default is MBR (only for install)'
16 echo ''
17 }
18
19 RESERVE_SIZE_MB=0
20 while [ -n "$1" ]; do
21 if [ "$1" = "-i" ]; then
22 MODE="install"
23 elif [ "$1" = "-I" ]; then
24 MODE="install"
25 FORCE="Y"
26 elif [ "$1" = "-u" ]; then
27 MODE="update"
28 elif [ "$1" = "-s" ]; then
29 SECUREBOOT="YES"
30 elif [ "$1" = "-g" ]; then
31 VTGPT="YES"
32 elif [ "$1" = "-r" ]; then
33 RESERVE_SPACE="YES"
34 shift
35 RESERVE_SIZE_MB=$1
36 else
37 if ! [ -b "$1" ]; then
38 vterr "$1 is NOT a valid device"
39 print_usage
40 exit 1
41 fi
42 DISK=$1
43 fi
44
45 shift
46 done
47
48 if [ -z "$MODE" ]; then
49 print_usage
50 exit 1
51 fi
52
53 if ! [ -b "$DISK" ]; then
54 vterr "Disk $DISK does not exist"
55 exit 1
56 fi
57
58 if [ -e /sys/class/block/${DISK#/dev/}/start ]; then
59 vterr "$DISK is a partition, please use the whole disk"
60 exit 1
61 fi
62
63 if [ -n "$RESERVE_SPACE" ]; then
64 if echo $RESERVE_SIZE_MB | grep -q '^[0-9][0-9]*$'; then
65 vtdebug "User will reserve $RESERVE_SIZE_MB MB disk space"
66 else
67 vterr "$RESERVE_SIZE_MB is invalid for reserved space"
68 exit 1
69 fi
70 fi
71
72 if dd if="$DISK" of=/dev/null bs=1 count=1 >/dev/null 2>&1; then
73 vtdebug "root permission check ok ..."
74 else
75 vterr "Failed to access $DISK, maybe root privilege is needed!"
76 echo ''
77 exit 1
78 fi
79
80 vtdebug "MODE=$MODE FORCE=$FORCE RESERVE_SPACE=$RESERVE_SPACE RESERVE_SIZE_MB=$RESERVE_SIZE_MB"
81
82 if ! check_tool_work_ok; then
83 vterr "Some tools can not run in current system. Please check log.txt for detail."
84 exit 1
85 fi
86
87 grep "^$DISK" /proc/mounts | while read mtline; do
88 mtpnt=$(echo $mtline | awk '{print $2}')
89 vtdebug "Trying to umount $mtpnt ..."
90 umount $mtpnt >/dev/null 2>&1
91 done
92
93 if swapon -s | grep -q "^${DISK}[0-9]"; then
94 swapon -s | grep "^${DISK}[0-9]" | awk '{print $1}' | while read line; do
95 vtdebug "Trying to swapoff $line ..."
96 swapoff $line
97 done
98 fi
99
100
101 if grep "$DISK" /proc/mounts; then
102 vterr "$DISK is already mounted, please umount it first!"
103 exit 1
104 fi
105
106 if swapon -s | grep -q "^${DISK}[0-9]"; then
107 vterr "$DISK is used as swap, please swapoff it first!"
108 exit 1
109 fi
110
111
112 if [ "$MODE" = "install" ]; then
113 vtdebug "install ventoy ..."
114
115 if [ -n "$VTGPT" ]; then
116 if parted -v > /dev/null 2>&1; then
117 PARTTOOL='parted'
118 else
119 vterr "parted is not found in the system, Ventoy can't create new partition."
120 exit 1
121 fi
122 else
123 if parted -v > /dev/null 2>&1; then
124 PARTTOOL='parted'
125 elif fdisk -v >/dev/null 2>&1; then
126 PARTTOOL='fdisk'
127 else
128 vterr "Both parted and fdisk are not found in the system, Ventoy can't create new partition."
129 exit 1
130 fi
131 fi
132
133 version=$(get_disk_ventoy_version $DISK)
134 if [ $? -eq 0 ]; then
135 if [ -z "$FORCE" ]; then
136 vtwarn "$DISK already contains a Ventoy with version $version"
137 vtwarn "Use -u option to do a safe upgrade operation."
138 vtwarn "OR if you really want to reinstall ventoy to $DISK, please use -I option."
139 vtwarn ""
140 exit 1
141 fi
142 fi
143
144 disk_sector_num=$(cat /sys/block/${DISK#/dev/}/size)
145 disk_size_gb=$(expr $disk_sector_num / 2097152)
146
147 if [ $disk_sector_num -gt 4294967296 ] && [ -z "$VTGPT" ]; then
148 vterr "$DISK is over 2TB size, MBR will not work on it."
149 exit 1
150 fi
151
152 if [ -n "$RESERVE_SPACE" ]; then
153 sum_size_mb=$(expr $RESERVE_SIZE_MB + $VENTOY_PART_SIZE_MB)
154 reserve_sector_num=$(expr $sum_size_mb \* 2048)
155
156 if [ $disk_sector_num -le $reserve_sector_num ]; then
157 vterr "Can't reserve $RESERVE_SIZE_MB MB space from $DISK"
158 exit 1
159 fi
160 fi
161
162 #Print disk info
163 echo "Disk : $DISK"
164 parted -s $DISK p 2>&1 | grep Model
165 echo "Size : $disk_size_gb GB"
166 if [ -n "$VTGPT" ]; then
167 echo "Style: GPT"
168 else
169 echo "Style: MBR"
170 fi
171 echo ''
172
173 if [ -n "$RESERVE_SPACE" ]; then
174 echo "You will reserve $RESERVE_SIZE_MB MB disk space "
175 fi
176 echo ''
177
178 vtwarn "Attention:"
179 vtwarn "You will install Ventoy to $DISK."
180 vtwarn "All the data on the disk $DISK will be lost!!!"
181 echo ""
182
183 read -p 'Continue? (y/n)' Answer
184 if [ "$Answer" != "y" ]; then
185 if [ "$Answer" != "Y" ]; then
186 exit 0
187 fi
188 fi
189
190 echo ""
191 vtwarn "All the data on the disk $DISK will be lost!!!"
192 read -p 'Double-check. Continue? (y/n)' Answer
193 if [ "$Answer" != "y" ]; then
194 if [ "$Answer" != "Y" ]; then
195 exit 0
196 fi
197 fi
198
199
200 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
201 vterr "No enough space in disk $DISK"
202 exit 1
203 fi
204
205 if ! dd if=/dev/zero of=$DISK bs=1 count=512 status=none conv=fsync; then
206 vterr "Write data to $DISK failed, please check whether it's in use."
207 exit 1
208 fi
209
210 if [ -n "$VTGPT" ]; then
211 vtdebug "format_ventoy_disk_gpt $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
212 format_ventoy_disk_gpt $RESERVE_SIZE_MB $DISK $PARTTOOL
213 else
214 vtdebug "format_ventoy_disk_mbr $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
215 format_ventoy_disk_mbr $RESERVE_SIZE_MB $DISK $PARTTOOL
216 fi
217
218 # format part1
219 if ventoy_is_linux64; then
220 cmd=./tool/mkexfatfs_64
221 else
222 cmd=./tool/mkexfatfs_32
223 fi
224
225 if [ -d ./tool/ ]; then
226 chmod +x -R ./tool/
227 fi
228
229 # DiskSize > 32GB Cluster Size use 128KB
230 # DiskSize < 32GB Cluster Size use 32KB
231 if [ $disk_size_gb -gt 32 ]; then
232 cluster_sectors=256
233 else
234 cluster_sectors=64
235 fi
236
237 $cmd -n ventoy -s $cluster_sectors ${DISK}1
238
239 vtinfo "writing data to disk ..."
240
241 dd status=none conv=fsync if=./boot/boot.img of=$DISK bs=1 count=446
242
243 if [ -n "$VTGPT" ]; then
244 echo -en '\x22' | dd status=none of=$DISK conv=fsync bs=1 count=1 seek=92
245 ./tool/xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2014 seek=34
246 echo -en '\x23' | dd of=$DISK conv=fsync bs=1 count=1 seek=17908 status=none
247 else
248 ./tool/xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2047 seek=1
249 fi
250
251 ./tool/xzcat ./ventoy/ventoy.disk.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=$VENTOY_SECTOR_NUM seek=$part2_start_sector
252
253 #disk uuid
254 ./tool/vtoy_gen_uuid | dd status=none conv=fsync of=${DISK} seek=384 bs=1 count=16
255
256 #disk signature
257 ./tool/vtoy_gen_uuid | dd status=none conv=fsync of=${DISK} skip=12 seek=440 bs=1 count=4
258
259 vtinfo "sync data ..."
260 sync
261
262 vtinfo "esp partition processing ..."
263
264 sleep 1
265 mtpnt=$(grep "^${DISK}2" /proc/mounts | awk '{print $2}')
266 if [ -n "$mtpnt" ]; then
267 umount $mtpnt >/dev/null 2>&1
268 fi
269
270 if [ "$SECUREBOOT" != "YES" ]; then
271 mkdir ./tmp_mnt
272
273 vtdebug "mounting part2 ...."
274 for tt in 1 2 3; do
275 if mount ${DISK}2 ./tmp_mnt; then
276 vtdebug "mounting part2 success"
277 break
278 fi
279
280 mtpnt=$(grep "^${DISK}2" /proc/mounts | awk '{print $2}')
281 if [ -n "$mtpnt" ]; then
282 umount $mtpnt >/dev/null 2>&1
283 fi
284 sleep 2
285 done
286
287 rm -f ./tmp_mnt/EFI/BOOT/BOOTX64.EFI
288 rm -f ./tmp_mnt/EFI/BOOT/grubx64.efi
289 rm -f ./tmp_mnt/EFI/BOOT/MokManager.efi
290 rm -f ./tmp_mnt/ENROLL_THIS_KEY_IN_MOKMANAGER.cer
291 mv ./tmp_mnt/EFI/BOOT/grubx64_real.efi ./tmp_mnt/EFI/BOOT/BOOTX64.EFI
292
293 umount ./tmp_mnt
294 rm -rf ./tmp_mnt
295 fi
296
297 echo ""
298 vtinfo "Install Ventoy to $DISK successfully finished."
299 echo ""
300
301 else
302 vtdebug "update ventoy ..."
303
304 oldver=$(get_disk_ventoy_version $DISK)
305 if [ $? -ne 0 ]; then
306 vtwarn "$DISK does not contain ventoy or data corupted"
307 echo ""
308 vtwarn "Please use -i option if you want to install ventoy to $DISK"
309 echo ""
310 exit 1
311 fi
312
313 curver=$(cat ./ventoy/version)
314
315 vtinfo "Upgrade operation is safe, all the data in the 1st partition (iso files and other) will be unchanged!"
316 echo ""
317
318 read -p "Update Ventoy $oldver ===> $curver Continue? (y/n)" Answer
319 if [ "$Answer" != "y" ]; then
320 if [ "$Answer" != "Y" ]; then
321 exit 0
322 fi
323 fi
324
325 PART2=$(get_disk_part_name $DISK 2)
326 SHORT_PART2=${PART2#/dev/}
327 part2_start=$(cat /sys/class/block/$SHORT_PART2/start)
328
329 PART1_TYPE=$(dd if=$DISK bs=1 count=1 skip=450 status=none | ./tool/hexdump -n1 -e '1/1 "%02X"')
330
331 if [ "$PART1_TYPE" = "EE" ]; then
332 vtdebug "This is GPT partition style ..."
333 ./tool/xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2014 seek=34
334 echo -en '\x23' | dd of=$DISK conv=fsync bs=1 count=1 seek=17908 status=none
335 else
336 vtdebug "This is MBR partition style ..."
337 dd status=none conv=fsync if=./boot/boot.img of=$DISK bs=1 count=440
338
339 PART1_ACTIVE=$(dd if=$DISK bs=1 count=1 skip=446 status=none | ./tool/hexdump -n1 -e '1/1 "%02X"')
340 PART2_ACTIVE=$(dd if=$DISK bs=1 count=1 skip=462 status=none | ./tool/hexdump -n1 -e '1/1 "%02X"')
341
342 vtdebug "PART1_ACTIVE=$PART1_ACTIVE PART2_ACTIVE=$PART2_ACTIVE"
343 if [ "$PART1_ACTIVE" = "00" ] && [ "$PART2_ACTIVE" = "80" ]; then
344 vtdebug "change 1st partition active, 2nd partition inactive ..."
345 echo -en '\x80' | dd of=$DISK conv=fsync bs=1 count=1 seek=446 status=none
346 echo -en '\x00' | dd of=$DISK conv=fsync bs=1 count=1 seek=462 status=none
347 fi
348 ./tool/xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2047 seek=1
349 fi
350
351 ./tool/xzcat ./ventoy/ventoy.disk.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=$VENTOY_SECTOR_NUM seek=$part2_start
352
353 sync
354
355 if [ "$SECUREBOOT" != "YES" ]; then
356 mkdir ./tmp_mnt
357
358 vtdebug "mounting part2 ...."
359 for tt in 1 2 3; do
360 if mount ${DISK}2 ./tmp_mnt; then
361 vtdebug "mounting part2 success"
362 break
363 fi
364 sleep 2
365 done
366
367 rm -f ./tmp_mnt/EFI/BOOT/BOOTX64.EFI
368 rm -f ./tmp_mnt/EFI/BOOT/grubx64.efi
369 rm -f ./tmp_mnt/EFI/BOOT/MokManager.efi
370 rm -f ./tmp_mnt/ENROLL_THIS_KEY_IN_MOKMANAGER.cer
371 mv ./tmp_mnt/EFI/BOOT/grubx64_real.efi ./tmp_mnt/EFI/BOOT/BOOTX64.EFI
372
373 umount ./tmp_mnt
374 rm -rf ./tmp_mnt
375 fi
376
377 echo ""
378 vtinfo "Update Ventoy to $DISK successfully finished."
379 echo ""
380
381 fi
382
383