]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - INSTALL/tool/VentoyWorker.sh
update tip message for VTOY_DEFAULT_SEARCH_ROOT (#1373)
[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 (fails 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 ' -n try non-destructive installation (only for install)'
20 echo ''
21 }
22
23
24 VTNEW_LABEL='Ventoy'
25 RESERVE_SIZE_MB=0
26 while [ -n "$1" ]; do
27 if [ "$1" = "-i" ]; then
28 MODE="install"
29 elif [ "$1" = "-I" ]; then
30 MODE="install"
31 FORCE="Y"
32 elif [ "$1" = "-n" ]; then
33 NONDESTRUCTIVE="Y"
34 elif [ "$1" = "-u" ]; then
35 MODE="update"
36 elif [ "$1" = "-l" ]; then
37 MODE="list"
38 elif [ "$1" = "-s" ]; then
39 SECUREBOOT="YES"
40 elif [ "$1" = "-S" ]; then
41 SECUREBOOT="NO"
42 elif [ "$1" = "-g" ]; then
43 VTGPT="YES"
44 elif [ "$1" = "-L" ]; then
45 shift
46 VTNEW_LABEL=$1
47 elif [ "$1" = "-r" ]; then
48 RESERVE_SPACE="YES"
49 shift
50 RESERVE_SIZE_MB=$1
51 elif [ "$1" = "-V" ] || [ "$1" = "--version" ]; then
52 exit 0
53 elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
54 print_usage
55 exit 0
56 else
57 if ! [ -b "$1" ]; then
58 vterr "$1 is NOT a valid device"
59 print_usage
60 exit 1
61 fi
62 DISK=$1
63 fi
64
65 shift
66 done
67
68 if [ -z "$MODE" ]; then
69 print_usage
70 exit 1
71 fi
72
73 if ! [ -b "$DISK" ]; then
74 vterr "Disk $DISK does not exist"
75 exit 1
76 fi
77
78 if [ -e /sys/class/block/${DISK#/dev/}/start ]; then
79 vterr "$DISK is a partition, please use the whole disk."
80 echo "For example:"
81 vterr " sudo sh Ventoy2Disk.sh -i /dev/sdb1 <=== This is wrong"
82 vtinfo " sudo sh Ventoy2Disk.sh -i /dev/sdb <=== This is right"
83 echo ""
84 exit 1
85 fi
86
87 if [ -n "$RESERVE_SPACE" -a "$MODE" = "install" ]; then
88 if echo $RESERVE_SIZE_MB | grep -q '^[0-9][0-9]*$'; then
89 vtdebug "User will reserve $RESERVE_SIZE_MB MB disk space"
90 else
91 vterr "$RESERVE_SIZE_MB is invalid for reserved space"
92 exit 1
93 fi
94 fi
95
96 vtdebug "MODE=$MODE FORCE=$FORCE RESERVE_SPACE=$RESERVE_SPACE RESERVE_SIZE_MB=$RESERVE_SIZE_MB"
97
98 #check tools
99 if check_tool_work_ok; then
100 vtdebug "check tool work ok"
101 else
102 vterr "Some tools can not run on current system. Please check log.txt for details."
103 exit 1
104 fi
105
106 if [ "$MODE" = "list" ]; then
107 version=$(get_disk_ventoy_version $DISK)
108 if [ $? -eq 0 ]; then
109 echo "Ventoy Version in Disk: $version"
110
111 vtPart1Type=$(dd if=$DISK bs=1 count=1 skip=450 status=none | hexdump -n1 -e '1/1 "%02X"')
112 if [ "$vtPart1Type" = "EE" ]; then
113 echo "Disk Partition Style : GPT"
114 else
115 echo "Disk Partition Style : MBR"
116 fi
117
118 if check_disk_secure_boot $DISK; then
119 echo "Secure Boot Support : YES"
120 else
121 echo "Secure Boot Support : NO"
122 fi
123 else
124 echo "Ventoy Version: NA"
125 fi
126 echo ""
127 exit 0
128 fi
129
130 #check mountpoint
131 check_umount_disk "$DISK"
132
133 if grep "$DISK" /proc/mounts; then
134 vterr "$DISK is already mounted, please umount it first!"
135 exit 1
136 fi
137
138 #check swap partition
139 if swapon --help 2>&1 | grep -q '^ \-s,'; then
140 if swapon -s | grep -q "^${DISK}[0-9]"; then
141 vterr "$DISK is used as swap, please swapoff it first!"
142 exit 1
143 fi
144 fi
145
146 #check access
147 if dd if="$DISK" of=/dev/null bs=1 count=1 >/dev/null 2>&1; then
148 vtdebug "root permission check ok ..."
149 else
150 vterr "Failed to access $DISK, maybe root privilege is needed!"
151 echo ''
152 exit 1
153 fi
154
155
156 #check tmp_mnt directory
157 if [ -d ./tmp_mnt ]; then
158 vtdebug "There is a tmp_mnt directory, now delete it."
159 umount ./tmp_mnt >/dev/null 2>&1
160 rm -rf ./tmp_mnt
161 if [ -d ./tmp_mnt ]; then
162 vterr "tmp_mnt directory exits, please delete it first."
163 exit 1
164 fi
165 fi
166
167
168 if [ "$MODE" = "install" -a -z "$NONDESTRUCTIVE" ]; then
169 vtdebug "install Ventoy ..."
170
171 if [ -n "$VTGPT" ]; then
172 if parted -v > /dev/null 2>&1; then
173 PARTTOOL='parted'
174 else
175 vterr "parted is not found in the system, Ventoy can't create new partitions without it."
176 vterr "You should install \"GNU parted\" first."
177 exit 1
178 fi
179 else
180 if parted -v > /dev/null 2>&1; then
181 PARTTOOL='parted'
182 elif fdisk -v >/dev/null 2>&1; then
183 PARTTOOL='fdisk'
184 else
185 vterr "Both parted and fdisk are not found in the system, Ventoy can't create new partitions."
186 exit 1
187 fi
188 fi
189
190 version=$(get_disk_ventoy_version $DISK)
191 if [ $? -eq 0 ]; then
192 if [ -z "$FORCE" ]; then
193 vtwarn "$DISK already contains a Ventoy with version $version"
194 vtwarn "Use -u option to do a safe upgrade operation."
195 vtwarn "OR if you really want to reinstall Ventoy to $DISK, please use -I option."
196 vtwarn ""
197 exit 1
198 fi
199 fi
200
201 disk_sector_num=$(cat /sys/block/${DISK#/dev/}/size)
202 disk_size_gb=$(expr $disk_sector_num / 2097152)
203
204 if [ $disk_sector_num -gt 4294967296 ] && [ -z "$VTGPT" ]; then
205 vterr "$DISK is over 2TB size, MBR will not work on it."
206 exit 1
207 fi
208
209 if [ -n "$RESERVE_SPACE" ]; then
210 sum_size_mb=$(expr $RESERVE_SIZE_MB + $VENTOY_PART_SIZE_MB)
211 reserve_sector_num=$(expr $sum_size_mb \* 2048)
212
213 if [ $disk_sector_num -le $reserve_sector_num ]; then
214 vterr "Can't reserve $RESERVE_SIZE_MB MB space from $DISK"
215 exit 1
216 fi
217 fi
218
219 #Print disk info
220 echo "Disk : $DISK"
221 parted -s $DISK p 2>&1 | grep Model
222 echo "Size : $disk_size_gb GB"
223 if [ -n "$VTGPT" ]; then
224 echo "Style: GPT"
225 else
226 echo "Style: MBR"
227 fi
228 echo ''
229
230 if [ -n "$RESERVE_SPACE" ]; then
231 echo "You will reserve $RESERVE_SIZE_MB MB disk space "
232 fi
233 echo ''
234
235 vtwarn "Attention:"
236 vtwarn "You will install Ventoy to $DISK."
237 vtwarn "All the data on the disk $DISK will be lost!!!"
238 echo ""
239
240 read -p 'Continue? (y/n) ' Answer
241 if [ "$Answer" != "y" ]; then
242 if [ "$Answer" != "Y" ]; then
243 exit 0
244 fi
245 fi
246
247 echo ""
248 vtwarn "All the data on the disk $DISK will be lost!!!"
249 read -p 'Double-check. Continue? (y/n) ' Answer
250 if [ "$Answer" != "y" ]; then
251 if [ "$Answer" != "Y" ]; then
252 exit 0
253 fi
254 fi
255
256 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
257 vterr "No enough space in disk $DISK"
258 exit 1
259 fi
260
261 # check and umount
262 check_umount_disk "$DISK"
263
264 if ! dd if=/dev/zero of=$DISK bs=64 count=512 status=none conv=fsync; then
265 vterr "Write data to $DISK failed, please check whether it's in use."
266 exit 1
267 fi
268
269 if [ -n "$VTGPT" ]; then
270 vtdebug "format_ventoy_disk_gpt $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
271 format_ventoy_disk_gpt $RESERVE_SIZE_MB $DISK $PARTTOOL
272 else
273 vtdebug "format_ventoy_disk_mbr $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
274 format_ventoy_disk_mbr $RESERVE_SIZE_MB $DISK $PARTTOOL
275 fi
276
277 # format part1
278
279 # DiskSize > 32GB Cluster Size use 128KB
280 # DiskSize < 32GB Cluster Size use 32KB
281 if [ $disk_size_gb -gt 32 ]; then
282 cluster_sectors=256
283 else
284 cluster_sectors=64
285 fi
286
287 PART1=$(get_disk_part_name $DISK 1)
288 PART2=$(get_disk_part_name $DISK 2)
289
290 #clean part2
291 dd status=none conv=fsync if=/dev/zero of=$DISK bs=512 count=32 seek=$part2_start_sector
292
293 #format part1
294 wait_and_create_part ${PART1} ${PART2}
295 if [ -b ${PART1} ]; then
296 vtinfo "Format partition 1 ${PART1} ..."
297 mkexfatfs -n "$VTNEW_LABEL" -s $cluster_sectors ${PART1}
298 if [ $? -ne 0 ]; then
299 echo "mkexfatfs failed, now retry..."
300 mkexfatfs -n "$VTNEW_LABEL" -s $cluster_sectors ${PART1}
301 if [ $? -ne 0 ]; then
302 echo "######### mkexfatfs failed, exit ########"
303 exit 1
304 fi
305 else
306 echo "mkexfatfs success"
307 fi
308 else
309 vterr "${PART1} NOT exist"
310 fi
311
312 vtinfo "writing data to disk ..."
313 dd status=none conv=fsync if=./boot/boot.img of=$DISK bs=1 count=446
314
315 if [ -n "$VTGPT" ]; then
316 echo -en '\x22' | dd status=none of=$DISK conv=fsync bs=1 count=1 seek=92
317 xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2014 seek=34
318 echo -en '\x23' | dd of=$DISK conv=fsync bs=1 count=1 seek=17908 status=none
319 else
320 xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2047 seek=1
321 fi
322
323 # check and umount
324 check_umount_disk "$DISK"
325
326 xzcat ./ventoy/ventoy.disk.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=$VENTOY_SECTOR_NUM seek=$part2_start_sector
327
328 #test UUID
329 testUUIDStr=$(vtoy_gen_uuid | hexdump -C)
330 vtdebug "test uuid: $testUUIDStr"
331
332 #disk uuid
333 vtoy_gen_uuid | dd status=none conv=fsync of=${DISK} seek=384 bs=1 count=16
334
335 #disk signature
336 vtoy_gen_uuid | dd status=none conv=fsync of=${DISK} skip=12 seek=440 bs=1 count=4
337
338 vtinfo "sync data ..."
339 sync
340
341 vtinfo "esp partition processing ..."
342 if [ "$SECUREBOOT" != "YES" ]; then
343 sleep 2
344 check_umount_disk "$DISK"
345 vtoycli partresize -s $DISK $part2_start_sector
346 fi
347
348 echo ""
349 vtinfo "Install Ventoy to $DISK successfully finished."
350 echo ""
351
352 elif [ "$MODE" = "install" -a -n "$NONDESTRUCTIVE" ]; then
353 vtdebug "non-destructive install Ventoy ..."
354
355 version=$(get_disk_ventoy_version $DISK)
356 if [ $? -eq 0 ]; then
357 if [ -z "$FORCE" ]; then
358 vtwarn "$DISK already contains a Ventoy with version $version."
359 vtwarn "You can not do and don not need non-destructive installation."
360 vtwarn ""
361 exit 1
362 fi
363 fi
364
365 disk_sector_num=$(cat /sys/block/${DISK#/dev/}/size)
366 disk_size_gb=$(expr $disk_sector_num / 2097152)
367
368 if vtoycli partresize -t $DISK; then
369 OldStyle="GPT"
370 else
371 OldStyle="MBR"
372 fi
373
374 #Print disk info
375 echo "Disk : $DISK"
376 parted -s $DISK p 2>&1 | grep Model
377 echo "Size : $disk_size_gb GB"
378 echo "Style: $OldStyle"
379 echo ''
380
381 vtwarn "Attention:"
382 vtwarn "Ventoy will try non-destructive installation on $DISK if possible."
383 echo ""
384
385 read -p 'Continue? (y/n) ' Answer
386 if [ "$Answer" != "y" ]; then
387 if [ "$Answer" != "Y" ]; then
388 exit 0
389 fi
390 fi
391
392 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
393 vterr "No enough space in disk $DISK"
394 exit 1
395 fi
396
397 PART1=$(get_disk_part_name $DISK 1)
398 PART2=$(get_disk_part_name $DISK 2)
399
400 #Part1 size in MB aligned with 4KB
401 PART1_SECTORS=$(cat /sys/class/block/${PART1#/dev/}/size)
402 PART1_4K=$(expr $PART1_SECTORS / 8)
403 PART1_MB=$(expr $PART1_4K / 256)
404 PART1_NEW_MB=$(expr $PART1_MB - 32)
405
406 echo "$PART1 is ${PART1_MB}MB"
407
408 #check partition layout
409 echo "check partition layout ..."
410 vtoycli partresize -c $DISK
411 vtRet=$?
412 if [ $vtRet -eq 0 ]; then
413 exit 1
414 else
415 # check and umount
416 check_umount_disk "$DISK"
417 sleep 1
418 check_umount_disk "$DISK"
419
420 if [ $vtRet -eq 1 ]; then
421 echo "Free space enough, start install..."
422 part2_start_sector=$(expr $PART1_SECTORS + 2048)
423 elif [ $vtRet -eq 2 ]; then
424 echo "We need to shrink partition 1 firstly ..."
425
426 PART1_BLKID=$(blkid $PART1)
427 blkid $PART1
428
429 if echo $PART1_BLKID | egrep -q -i 'TYPE=ntfs|TYPE=.ntfs'; then
430 echo "Partition 1 contains NTFS filesystem"
431
432 which ntfsresize
433 if [ $? -ne 0 ]; then
434 echo "###[FAIL] ntfsresize not found. Please install ntfs-3g package."
435 exit 1
436 fi
437
438 echo "ntfsfix -b -d $PART1 ..."
439 ntfsfix -b -d $PART1
440
441 echo "ntfsresize --size ${PART1_NEW_MB}Mi $PART1 ..."
442 ntfsresize -f --size ${PART1_NEW_MB}Mi $PART1
443 if [ $? -ne 0 ]; then
444 echo "###[FAIL] ntfsresize failed."
445 exit 1
446 fi
447 elif echo $PART1_BLKID | egrep -q -i 'TYPE=ext[2-4]|TYPE=.ext[2-4]'; then
448 echo "Partition 1 contains EXT filesystem"
449
450 which resize2fs
451 if [ $? -ne 0 ]; then
452 echo "###[FAIL] resize2fs not found. Please install e2fsprogs package."
453 exit 1
454 fi
455
456 echo "e2fsck -f $PART1 ..."
457 e2fsck -f $PART1
458
459 echo "resize2fs $PART1 ${PART1_NEW_MB}M ..."
460 resize2fs $PART1 ${PART1_NEW_MB}M
461 if [ $? -ne 0 ]; then
462 echo "###[FAIL] resize2fs failed."
463 exit 1
464 fi
465 else
466 echo "###[FAIL] Unsupported filesystem in partition 1."
467 exit 1
468 fi
469
470 sync
471 PART1_NEW_END_MB=$(expr $PART1_NEW_MB + 1)
472 part2_start_sector=$(expr $PART1_NEW_END_MB \* 2048)
473 fi
474 fi
475
476 vtinfo "writing data to disk part2_start=$part2_start_sector ..."
477
478 dd status=none conv=fsync if=./boot/boot.img of=$DISK bs=1 count=440
479
480 if [ "$OldStyle" = "GPT" ]; then
481 echo -en '\x22' | dd status=none of=$DISK conv=fsync bs=1 count=1 seek=92
482 xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2014 seek=34
483 echo -en '\x23' | dd of=$DISK conv=fsync bs=1 count=1 seek=17908 status=none
484 else
485 xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2047 seek=1
486 fi
487
488 xzcat ./ventoy/ventoy.disk.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=$VENTOY_SECTOR_NUM seek=$part2_start_sector
489
490 #test UUID
491 testUUIDStr=$(vtoy_gen_uuid | hexdump -C)
492 vtdebug "test uuid: $testUUIDStr"
493
494 #disk uuid
495 vtoy_gen_uuid | dd status=none conv=fsync of=${DISK} seek=384 bs=1 count=16
496
497 vtinfo "sync data ..."
498 sync
499
500 vtinfo "esp partition processing ..."
501 if [ "$SECUREBOOT" != "YES" ]; then
502 vtoycli partresize -s $DISK $part2_start_sector
503 fi
504
505 vtinfo "update partition table $DISK $part2_start_sector ..."
506 vtoycli partresize -p $DISK $part2_start_sector
507 if [ $? -eq 0 ]; then
508 sync
509 echo ""
510 vtinfo "Ventoy non-destructive installation on $DISK successfully finished."
511 echo ""
512 else
513 echo ""
514 vterr "Ventoy non-destructive installation on $DISK failed."
515 echo ""
516 fi
517
518 else
519 vtdebug "update Ventoy ..."
520
521 oldver=$(get_disk_ventoy_version $DISK)
522 if [ $? -ne 0 ]; then
523 if is_disk_contains_ventoy $DISK; then
524 oldver="Unknown"
525 else
526 vtwarn "$DISK does not contain Ventoy or data corrupted"
527 echo ""
528 vtwarn "Please use -i option if you want to install ventoy to $DISK"
529 echo ""
530 exit 1
531 fi
532 fi
533
534 #reserve secure boot option
535 if [ -z "$SECUREBOOT" ]; then
536 if check_disk_secure_boot $DISK; then
537 SECUREBOOT="YES"
538 else
539 SECUREBOOT="NO"
540 fi
541 fi
542
543 curver=$(cat ./ventoy/version)
544
545 vtinfo "Upgrade operation is safe, all the data in the 1st partition (iso files and other) will be unchanged!"
546 echo ""
547
548 read -p "Update Ventoy $oldver ===> $curver Continue? (y/n)" Answer
549 if [ "$Answer" != "y" ]; then
550 if [ "$Answer" != "Y" ]; then
551 exit 0
552 fi
553 fi
554
555 PART2=$(get_disk_part_name $DISK 2)
556 SHORT_PART2=${PART2#/dev/}
557 part2_start=$(cat /sys/class/block/$SHORT_PART2/start)
558
559 PART1_TYPE=$(dd if=$DISK bs=1 count=1 skip=450 status=none | hexdump -n1 -e '1/1 "%02X"')
560
561 #reserve disk uuid
562 rm -f ./diskuuid.bin
563 dd status=none conv=fsync if=${DISK} skip=384 bs=1 count=16 of=./diskuuid.bin
564
565 dd status=none conv=fsync if=./boot/boot.img of=$DISK bs=1 count=440
566 dd status=none conv=fsync if=./diskuuid.bin of=$DISK bs=1 count=16 seek=384
567 rm -f ./diskuuid.bin
568
569 #reserve data
570 rm -f ./rsvdata.bin
571 dd status=none conv=fsync if=${DISK} skip=2040 bs=512 count=8 of=./rsvdata.bin
572
573 if [ "$PART1_TYPE" = "EE" ]; then
574 vtdebug "This is GPT partition style ..."
575 echo -en '\x22' | dd status=none of=$DISK conv=fsync bs=1 count=1 seek=92
576 xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2014 seek=34
577 echo -en '\x23' | dd of=$DISK conv=fsync bs=1 count=1 seek=17908 status=none
578 else
579 vtdebug "This is MBR partition style ..."
580
581 PART1_ACTIVE=$(dd if=$DISK bs=1 count=1 skip=446 status=none | hexdump -n1 -e '1/1 "%02X"')
582 PART2_ACTIVE=$(dd if=$DISK bs=1 count=1 skip=462 status=none | hexdump -n1 -e '1/1 "%02X"')
583
584 vtdebug "PART1_ACTIVE=$PART1_ACTIVE PART2_ACTIVE=$PART2_ACTIVE"
585 if [ "$PART1_ACTIVE" = "00" ] && [ "$PART2_ACTIVE" = "80" ]; then
586 vtdebug "change 1st partition active, 2nd partition inactive ..."
587 echo -en '\x80' | dd of=$DISK conv=fsync bs=1 count=1 seek=446 status=none
588 echo -en '\x00' | dd of=$DISK conv=fsync bs=1 count=1 seek=462 status=none
589 fi
590 xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2047 seek=1
591 fi
592
593 dd status=none conv=fsync if=./rsvdata.bin seek=2040 bs=512 count=8 of=${DISK}
594 rm -f ./rsvdata.bin
595
596 check_umount_disk "$DISK"
597
598 xzcat ./ventoy/ventoy.disk.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=$VENTOY_SECTOR_NUM seek=$part2_start
599 sync
600
601 vtinfo "esp partition processing ..."
602 if [ "$SECUREBOOT" != "YES" ]; then
603 sleep 2
604 check_umount_disk "$DISK"
605 vtoycli partresize -s $DISK $part2_start
606 fi
607
608 echo ""
609 vtinfo "Update Ventoy on $DISK successfully finished."
610 echo ""
611
612 fi
613
614