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