]>
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 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 exits, 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 ..."
348 if [ "$SECUREBOOT" != "YES" ]; then
350 check_umount_disk
"$DISK"
351 vtoycli partresize
-s $DISK $part2_start_sector
355 vtinfo
"Install Ventoy to $DISK successfully finished."
358 elif [ "$MODE" = "install" -a -n "$NONDESTRUCTIVE" ]; then
359 vtdebug
"non-destructive install Ventoy ..."
361 version
=$(get_disk_ventoy_version $DISK)
362 if [ $?
-eq 0 ]; then
363 if [ -z "$FORCE" ]; then
364 vtwarn
"$DISK already contains a Ventoy with version $version."
365 vtwarn
"You can not do and don not need non-destructive installation."
371 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
372 disk_size_gb
=$(expr $disk_sector_num / 2097152)
374 if vtoycli partresize
-t $DISK; then
382 parted
-s $DISK p
2>&1 | grep Model
383 echo "Size : $disk_size_gb GB"
384 echo "Style: $OldStyle"
388 vtwarn
"Ventoy will try non-destructive installation on $DISK if possible."
391 read -p 'Continue? (y/n) ' Answer
392 if [ "$Answer" != "y" ]; then
393 if [ "$Answer" != "Y" ]; then
398 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
399 vterr
"No enough space in disk $DISK"
403 PART1
=$(get_disk_part_name $DISK 1)
404 PART2
=$(get_disk_part_name $DISK 2)
406 #Part1 size in MB aligned with 4KB
407 PART1_SECTORS
=$(cat /sys/class/block/${PART1#/dev/}/size)
408 PART1_4K
=$(expr $PART1_SECTORS / 8)
409 PART1_MB
=$(expr $PART1_4K / 256)
410 PART1_NEW_MB
=$(expr $PART1_MB - 32)
412 echo "$PART1 is ${PART1_MB}MB"
414 #check partition layout
415 echo "check partition layout ..."
416 vtoycli partresize
-c $DISK
418 if [ $vtRet -eq 0 ]; then
422 check_umount_disk
"$DISK"
424 check_umount_disk
"$DISK"
426 if [ $vtRet -eq 1 ]; then
427 echo "Free space enough, start install..."
428 part2_start_sector
=$(expr $PART1_SECTORS + 2048)
429 elif [ $vtRet -eq 2 ]; then
430 echo "We need to shrink partition 1 firstly ..."
432 PART1_BLKID
=$(blkid $PART1)
435 if echo $PART1_BLKID | egrep -q -i 'TYPE=ntfs|TYPE=.ntfs'; then
436 echo "Partition 1 contains NTFS filesystem"
439 if [ $?
-ne 0 ]; then
440 echo "###[FAIL] ntfsresize not found. Please install ntfs-3g package."
444 echo "ntfsfix -b -d $PART1 ..."
447 echo "ntfsresize --size ${PART1_NEW_MB}Mi $PART1 ..."
448 ntfsresize
-f --size ${PART1_NEW_MB}Mi
$PART1
449 if [ $?
-ne 0 ]; then
450 echo "###[FAIL] ntfsresize failed."
453 elif echo $PART1_BLKID | egrep -q -i 'TYPE=ext[2-4]|TYPE=.ext[2-4]'; then
454 echo "Partition 1 contains EXT filesystem"
457 if [ $?
-ne 0 ]; then
458 echo "###[FAIL] resize2fs not found. Please install e2fsprogs package."
462 echo "e2fsck -f $PART1 ..."
465 echo "resize2fs $PART1 ${PART1_NEW_MB}M ..."
466 resize2fs
$PART1 ${PART1_NEW_MB}M
467 if [ $?
-ne 0 ]; then
468 echo "###[FAIL] resize2fs failed."
472 echo "###[FAIL] Unsupported filesystem in partition 1."
477 PART1_NEW_END_MB
=$(expr $PART1_NEW_MB + 1)
478 part2_start_sector
=$(expr $PART1_NEW_END_MB \* 2048)
482 vtinfo
"writing data to disk part2_start=$part2_start_sector ..."
484 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=440
486 if [ "$OldStyle" = "GPT" ]; then
487 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
488 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
489 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
491 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
494 xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start_sector
497 testUUIDStr
=$(vtoy_gen_uuid | hexdump -C)
498 vtdebug
"test uuid: $testUUIDStr"
501 vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} seek
=384 bs
=1 count
=16
503 vtinfo
"sync data ..."
506 vtinfo
"esp partition processing ..."
507 if [ "$SECUREBOOT" != "YES" ]; then
508 vtoycli partresize
-s $DISK $part2_start_sector
511 vtinfo
"update partition table $DISK $part2_start_sector ..."
512 vtoycli partresize
-p $DISK $part2_start_sector
513 if [ $?
-eq 0 ]; then
516 vtinfo
"Ventoy non-destructive installation on $DISK successfully finished."
520 vterr
"Ventoy non-destructive installation on $DISK failed."
525 vtdebug
"update Ventoy ..."
527 oldver
=$(get_disk_ventoy_version $DISK)
528 if [ $?
-ne 0 ]; then
529 if is_disk_contains_ventoy
$DISK; then
532 vtwarn
"$DISK does not contain Ventoy or data corrupted"
534 vtwarn
"Please use -i option if you want to install ventoy to $DISK"
540 #reserve secure boot option
541 if [ -z "$SECUREBOOT" ]; then
542 if check_disk_secure_boot
$DISK; then
549 curver
=$(cat ./ventoy/version)
551 vtinfo
"Upgrade operation is safe, all the data in the 1st partition (iso files and other) will be unchanged!"
554 read -p "Update Ventoy $oldver ===> $curver Continue? (y/n)" Answer
555 if [ "$Answer" != "y" ]; then
556 if [ "$Answer" != "Y" ]; then
561 PART2
=$(get_disk_part_name $DISK 2)
562 SHORT_PART2
=${PART2#/dev/}
563 part2_start
=$(cat /sys/class/block/$SHORT_PART2/start)
565 PART1_TYPE
=$(dd if=$DISK bs=1 count=1 skip=450 status=none | hexdump -n1 -e '1/1 "%02X"')
569 dd status
=none conv
=fsync
if=${DISK} skip
=384 bs
=1 count
=16 of
=.
/diskuuid.bin
571 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=440
572 dd status
=none conv
=fsync
if=.
/diskuuid.bin of
=$DISK bs
=1 count
=16 seek
=384
577 dd status
=none conv
=fsync
if=${DISK} skip
=2040 bs
=512 count
=8 of
=.
/rsvdata.bin
579 if [ "$PART1_TYPE" = "EE" ]; then
580 vtdebug
"This is GPT partition style ..."
581 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
582 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
583 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
585 vtdebug
"This is MBR partition style ..."
587 PART1_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=446 status=none | hexdump -n1 -e '1/1 "%02X"')
588 PART2_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=462 status=none | hexdump -n1 -e '1/1 "%02X"')
590 vtdebug
"PART1_ACTIVE=$PART1_ACTIVE PART2_ACTIVE=$PART2_ACTIVE"
591 if [ "$PART1_ACTIVE" = "00" ] && [ "$PART2_ACTIVE" = "80" ]; then
592 vtdebug
"change 1st partition active, 2nd partition inactive ..."
593 echo -en '\x80' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=446 status
=none
594 echo -en '\x00' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=462 status
=none
596 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
599 dd status
=none conv
=fsync
if=.
/rsvdata.bin seek
=2040 bs
=512 count
=8 of
=${DISK}
602 check_umount_disk
"$DISK"
604 xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start
607 vtinfo
"esp partition processing ..."
608 if [ "$SECUREBOOT" != "YES" ]; then
610 check_umount_disk
"$DISK"
611 vtoycli partresize
-s $DISK $part2_start
615 vtinfo
"Update Ventoy on $DISK successfully finished."