]>
glassweightruler.freedombox.rocks Git - Ventoy.git/blob - INSTALL/tool/VentoyWorker.sh
7 echo 'Usage: Ventoy2Disk.sh CMD [ OPTION ] /dev/sdX'
9 echo ' -i install Ventoy to sdX (fails if disk already installed with Ventoy)'
10 echo ' -I force install Ventoy to sdX (no matter if installed or not)'
11 echo ' -u update Ventoy in sdX'
12 echo ' -l list Ventoy information in sdX'
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)'
27 if [ "$1" = "-i" ]; then
29 elif [ "$1" = "-I" ]; then
32 elif [ "$1" = "-n" ]; then
34 elif [ "$1" = "-u" ]; then
36 elif [ "$1" = "-l" ]; then
38 elif [ "$1" = "-s" ]; then
40 elif [ "$1" = "-S" ]; then
42 elif [ "$1" = "-g" ]; then
44 elif [ "$1" = "-L" ]; then
47 elif [ "$1" = "-r" ]; then
51 elif [ "$1" = "-V" ] || [ "$1" = "--version" ]; then
53 elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
57 if ! [ -b "$1" ]; then
58 vterr
"$1 is NOT a valid device"
63 # Resolve symlinks now, will be needed to look up information about the device in
64 # the /sys/ filesystem, for example /sys/class/block/${DISK#/dev/}/start
65 # The main use case is supporting /dev/disk/by-id/ symlinks instead of raw devices
66 if [ -L "$DISK" ]; then
67 DISK
=$(readlink -e -n "$DISK")
74 if [ -z "$MODE" ]; then
79 if ! [ -b "$DISK" ]; then
80 vterr
"Disk $DISK does not exist"
84 if [ -e /sys
/class
/block
/${DISK#/dev/}/start
]; then
85 vterr
"$DISK is a partition, please use the whole disk."
87 vterr
" sudo sh Ventoy2Disk.sh -i /dev/sdb1 <=== This is wrong"
88 vtinfo
" sudo sh Ventoy2Disk.sh -i /dev/sdb <=== This is right"
93 if [ -n "$RESERVE_SPACE" -a "$MODE" = "install" ]; then
94 if echo $RESERVE_SIZE_MB | grep -q '^[0-9][0-9]*$'; then
95 vtdebug
"User will reserve $RESERVE_SIZE_MB MB disk space"
97 vterr
"$RESERVE_SIZE_MB is invalid for reserved space"
102 vtdebug
"MODE=$MODE FORCE=$FORCE RESERVE_SPACE=$RESERVE_SPACE RESERVE_SIZE_MB=$RESERVE_SIZE_MB"
105 if check_tool_work_ok
; then
106 vtdebug
"check tool work ok"
108 vterr
"Some tools can not run on current system. Please check log.txt for details."
112 if [ "$MODE" = "list" ]; then
113 version
=$(get_disk_ventoy_version $DISK)
114 if [ $?
-eq 0 ]; then
115 echo "Ventoy Version in Disk: $version"
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"
121 echo "Disk Partition Style : MBR"
124 if check_disk_secure_boot
$DISK; then
125 echo "Secure Boot Support : YES"
127 echo "Secure Boot Support : NO"
130 echo "Ventoy Version: NA"
137 check_umount_disk
"$DISK"
139 if grep "$DISK" /proc
/mounts
; then
140 vterr
"$DISK is already mounted, please umount it first!"
144 #check swap partition
145 if swapon
--help 2>&1 | grep -q '^ \-s,'; then
146 if swapon
-s | grep -q "^${DISK}[0-9]"; then
147 vterr
"$DISK is used as swap, please swapoff it first!"
153 if dd if="$DISK" of
=/dev
/null bs
=1 count
=1 >/dev
/null
2>&1; then
154 vtdebug
"root permission check ok ..."
156 vterr
"Failed to access $DISK, maybe root privilege is needed!"
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
167 if [ -d .
/tmp_mnt
]; then
168 vterr
"tmp_mnt directory exists, please delete it first."
174 if [ "$MODE" = "install" -a -z "$NONDESTRUCTIVE" ]; then
175 vtdebug
"install Ventoy ..."
177 if [ -n "$VTGPT" ]; then
178 if parted
-v > /dev
/null
2>&1; then
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."
186 if parted
-v > /dev
/null
2>&1; then
188 elif fdisk
-v >/dev
/null
2>&1; then
191 vterr
"Both parted and fdisk are not found in the system, Ventoy can't create new partitions."
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."
207 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
208 disk_size_gb
=$(expr $disk_sector_num / 2097152)
210 if [ $disk_sector_num -gt 4294967296 ] && [ -z "$VTGPT" ]; then
211 vterr
"$DISK is over 2TB size, MBR will not work on it."
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)
219 if [ $disk_sector_num -le $reserve_sector_num ]; then
220 vterr
"Can't reserve $RESERVE_SIZE_MB MB space from $DISK"
227 parted
-s $DISK p
2>&1 | grep Model
228 echo "Size : $disk_size_gb GB"
229 if [ -n "$VTGPT" ]; then
236 if [ -n "$RESERVE_SPACE" ]; then
237 echo "You will reserve $RESERVE_SIZE_MB MB disk space "
242 vtwarn
"You will install Ventoy to $DISK."
243 vtwarn
"All the data on the disk $DISK will be lost!!!"
246 read -p 'Continue? (y/n) ' Answer
247 if [ "$Answer" != "y" ]; then
248 if [ "$Answer" != "Y" ]; then
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
262 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
263 vterr
"No enough space in disk $DISK"
268 check_umount_disk
"$DISK"
270 if ! dd if=/dev
/zero of
=$DISK bs
=64 count
=512 status
=none conv
=fsync
; then
271 vterr
"Write data to $DISK failed, please check whether it's in use."
275 if [ -n "$VTGPT" ]; then
276 vtdebug
"format_ventoy_disk_gpt $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
277 format_ventoy_disk_gpt
$RESERVE_SIZE_MB $DISK $PARTTOOL
279 vtdebug
"format_ventoy_disk_mbr $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
280 format_ventoy_disk_mbr
$RESERVE_SIZE_MB $DISK $PARTTOOL
285 # DiskSize > 32GB Cluster Size use 128KB
286 # DiskSize < 32GB Cluster Size use 32KB
287 if [ $disk_size_gb -gt 32 ]; then
293 PART1
=$(get_disk_part_name $DISK 1)
294 PART2
=$(get_disk_part_name $DISK 2)
297 dd status
=none conv
=fsync
if=/dev
/zero of
=$DISK bs
=512 count
=32 seek
=$part2_start_sector
300 wait_and_create_part
${PART1} ${PART2}
301 if [ -b ${PART1} ]; then
302 vtinfo
"Format partition 1 ${PART1} ..."
303 mkexfatfs
-n "$VTNEW_LABEL" -s $cluster_sectors ${PART1}
304 if [ $?
-ne 0 ]; then
305 echo "mkexfatfs failed, now retry..."
306 mkexfatfs
-n "$VTNEW_LABEL" -s $cluster_sectors ${PART1}
307 if [ $?
-ne 0 ]; then
308 echo "######### mkexfatfs failed, exit ########"
312 echo "mkexfatfs success"
315 vterr
"${PART1} NOT exist"
318 vtinfo
"writing data to disk ..."
319 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=446
321 if [ -n "$VTGPT" ]; then
322 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
323 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
324 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
326 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
330 check_umount_disk
"$DISK"
332 xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start_sector
335 testUUIDStr
=$(vtoy_gen_uuid | hexdump -C)
336 vtdebug
"test uuid: $testUUIDStr"
339 vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} seek
=384 bs
=1 count
=16
342 vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} skip
=12 seek
=440 bs
=1 count
=4
344 vtinfo
"sync data ..."
347 vtinfo
"esp partition processing ..."
349 if [ "$SECUREBOOT" != "YES" ]; then
351 check_umount_disk
"$DISK"
352 vtoycli partresize
-s $DISK $part2_start_sector
356 vtinfo
"Install Ventoy to $DISK successfully finished."
359 elif [ "$MODE" = "install" -a -n "$NONDESTRUCTIVE" ]; then
360 vtdebug
"non-destructive install Ventoy ..."
362 version
=$(get_disk_ventoy_version $DISK)
363 if [ $?
-eq 0 ]; then
364 if [ -z "$FORCE" ]; then
365 vtwarn
"$DISK already contains a Ventoy with version $version."
366 vtwarn
"You can not do and don not need non-destructive installation."
372 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
373 disk_size_gb
=$(expr $disk_sector_num / 2097152)
375 if vtoycli partresize
-t $DISK; then
383 parted
-s $DISK p
2>&1 | grep Model
384 echo "Size : $disk_size_gb GB"
385 echo "Style: $OldStyle"
389 vtwarn
"Ventoy will try non-destructive installation on $DISK if possible."
392 read -p 'Continue? (y/n) ' Answer
393 if [ "$Answer" != "y" ]; then
394 if [ "$Answer" != "Y" ]; then
399 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
400 vterr
"No enough space in disk $DISK"
404 PART1
=$(get_disk_part_name $DISK 1)
405 PART2
=$(get_disk_part_name $DISK 2)
407 #Part1 size in MB aligned with 4KB
408 PART1_SECTORS
=$(cat /sys/class/block/${PART1#/dev/}/size)
409 PART1_4K
=$(expr $PART1_SECTORS / 8)
410 PART1_MB
=$(expr $PART1_4K / 256)
411 PART1_NEW_MB
=$(expr $PART1_MB - 32)
413 echo "$PART1 is ${PART1_MB}MB"
415 #check partition layout
416 echo "check partition layout ..."
417 vtoycli partresize
-c $DISK
419 if [ $vtRet -eq 0 ]; then
423 check_umount_disk
"$DISK"
425 check_umount_disk
"$DISK"
427 if [ $vtRet -eq 1 ]; then
428 echo "Free space enough, start install..."
429 part2_start_sector
=$(expr $PART1_SECTORS + 2048)
430 elif [ $vtRet -eq 2 ]; then
431 echo "We need to shrink partition 1 firstly ..."
433 PART1_BLKID
=$(blkid $PART1)
436 if echo $PART1_BLKID | egrep -q -i 'TYPE=ntfs|TYPE=.ntfs'; then
437 echo "Partition 1 contains NTFS filesystem"
440 if [ $?
-ne 0 ]; then
441 echo "###[FAIL] ntfsresize not found. Please install ntfs-3g package."
445 echo "ntfsfix -b -d $PART1 ..."
448 echo "ntfsresize --size ${PART1_NEW_MB}Mi $PART1 ..."
449 ntfsresize
-f --size ${PART1_NEW_MB}Mi
$PART1
450 if [ $?
-ne 0 ]; then
451 echo "###[FAIL] ntfsresize failed."
454 elif echo $PART1_BLKID | egrep -q -i 'TYPE=ext[2-4]|TYPE=.ext[2-4]'; then
455 echo "Partition 1 contains EXT filesystem"
458 if [ $?
-ne 0 ]; then
459 echo "###[FAIL] resize2fs not found. Please install e2fsprogs package."
463 echo "e2fsck -f $PART1 ..."
466 echo "resize2fs $PART1 ${PART1_NEW_MB}M ..."
467 resize2fs
$PART1 ${PART1_NEW_MB}M
468 if [ $?
-ne 0 ]; then
469 echo "###[FAIL] resize2fs failed."
473 echo "###[FAIL] Unsupported filesystem in partition 1."
478 PART1_NEW_END_MB
=$(expr $PART1_NEW_MB + 1)
479 part2_start_sector
=$(expr $PART1_NEW_END_MB \* 2048)
483 vtinfo
"writing data to disk part2_start=$part2_start_sector ..."
485 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=440
487 if [ "$OldStyle" = "GPT" ]; then
488 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
489 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
490 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
492 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
495 xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start_sector
498 testUUIDStr
=$(vtoy_gen_uuid | hexdump -C)
499 vtdebug
"test uuid: $testUUIDStr"
502 vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} seek
=384 bs
=1 count
=16
504 vtinfo
"sync data ..."
507 vtinfo
"esp partition processing ..."
508 if [ "$SECUREBOOT" != "YES" ]; then
509 vtoycli partresize
-s $DISK $part2_start_sector
512 vtinfo
"update partition table $DISK $part2_start_sector ..."
513 vtoycli partresize
-p $DISK $part2_start_sector
514 if [ $?
-eq 0 ]; then
517 vtinfo
"Ventoy non-destructive installation on $DISK successfully finished."
521 vterr
"Ventoy non-destructive installation on $DISK failed."
526 vtdebug
"update Ventoy ..."
528 oldver
=$(get_disk_ventoy_version $DISK)
529 if [ $?
-ne 0 ]; then
530 if is_disk_contains_ventoy
$DISK; then
533 vtwarn
"$DISK does not contain Ventoy or data corrupted"
535 vtwarn
"Please use -i option if you want to install ventoy to $DISK"
541 #reserve secure boot option
542 if [ -z "$SECUREBOOT" ]; then
543 if check_disk_secure_boot
$DISK; then
550 curver
=$(cat ./ventoy/version)
552 vtinfo
"Upgrade operation is safe, all the data in the 1st partition (iso files and other) will be unchanged!"
555 read -p "Update Ventoy $oldver ===> $curver Continue? (y/n) " Answer
556 if [ "$Answer" != "y" ]; then
557 if [ "$Answer" != "Y" ]; then
562 PART2
=$(get_disk_part_name $DISK 2)
563 SHORT_PART2
=${PART2#/dev/}
564 part2_start
=$(cat /sys/class/block/$SHORT_PART2/start)
566 PART1_TYPE
=$(dd if=$DISK bs=1 count=1 skip=450 status=none | hexdump -n1 -e '1/1 "%02X"')
570 dd status
=none conv
=fsync
if=${DISK} skip
=384 bs
=1 count
=16 of
=.
/diskuuid.bin
572 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=440
573 dd status
=none conv
=fsync
if=.
/diskuuid.bin of
=$DISK bs
=1 count
=16 seek
=384
578 dd status
=none conv
=fsync
if=${DISK} skip
=2040 bs
=512 count
=8 of
=.
/rsvdata.bin
580 if [ "$PART1_TYPE" = "EE" ]; then
581 vtdebug
"This is GPT partition style ..."
582 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
583 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
584 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
586 vtdebug
"This is MBR partition style ..."
588 PART1_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=446 status=none | hexdump -n1 -e '1/1 "%02X"')
589 PART2_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=462 status=none | hexdump -n1 -e '1/1 "%02X"')
591 vtdebug
"PART1_ACTIVE=$PART1_ACTIVE PART2_ACTIVE=$PART2_ACTIVE"
592 if [ "$PART1_ACTIVE" = "00" ] && [ "$PART2_ACTIVE" = "80" ]; then
593 vtdebug
"change 1st partition active, 2nd partition inactive ..."
594 echo -en '\x80' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=446 status
=none
595 echo -en '\x00' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=462 status
=none
597 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
600 dd status
=none conv
=fsync
if=.
/rsvdata.bin seek
=2040 bs
=512 count
=8 of
=${DISK}
603 check_umount_disk
"$DISK"
605 xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start
608 vtinfo
"esp partition processing ..."
609 if [ "$SECUREBOOT" != "YES" ]; then
611 check_umount_disk
"$DISK"
612 vtoycli partresize
-s $DISK $part2_start
616 vtinfo
"Update Ventoy on $DISK successfully finished."