]>
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"
68 if [ -z "$MODE" ]; then
73 if ! [ -b "$DISK" ]; then
74 vterr
"Disk $DISK does not exist"
78 if [ -e /sys
/class
/block
/${DISK#/dev/}/start
]; then
79 vterr
"$DISK is a partition, please use the whole disk."
81 vterr
" sudo sh Ventoy2Disk.sh -i /dev/sdb1 <=== This is wrong"
82 vtinfo
" sudo sh Ventoy2Disk.sh -i /dev/sdb <=== This is right"
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"
91 vterr
"$RESERVE_SIZE_MB is invalid for reserved space"
96 vtdebug
"MODE=$MODE FORCE=$FORCE RESERVE_SPACE=$RESERVE_SPACE RESERVE_SIZE_MB=$RESERVE_SIZE_MB"
99 if check_tool_work_ok
; then
100 vtdebug
"check tool work ok"
102 vterr
"Some tools can not run on current system. Please check log.txt for details."
106 if [ "$MODE" = "list" ]; then
107 version
=$(get_disk_ventoy_version $DISK)
108 if [ $?
-eq 0 ]; then
109 echo "Ventoy Version in Disk: $version"
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"
115 echo "Disk Partition Style : MBR"
118 if check_disk_secure_boot
$DISK; then
119 echo "Secure Boot Support : YES"
121 echo "Secure Boot Support : NO"
124 echo "Ventoy Version: NA"
131 check_umount_disk
"$DISK"
133 if grep "$DISK" /proc
/mounts
; then
134 vterr
"$DISK is already mounted, please umount it first!"
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!"
147 if dd if="$DISK" of
=/dev
/null bs
=1 count
=1 >/dev
/null
2>&1; then
148 vtdebug
"root permission check ok ..."
150 vterr
"Failed to access $DISK, maybe root privilege is needed!"
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
161 if [ -d .
/tmp_mnt
]; then
162 vterr
"tmp_mnt directory exits, please delete it first."
168 if [ "$MODE" = "install" -a -z "$NONDESTRUCTIVE" ]; then
169 vtdebug
"install Ventoy ..."
171 if [ -n "$VTGPT" ]; then
172 if parted
-v > /dev
/null
2>&1; then
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."
180 if parted
-v > /dev
/null
2>&1; then
182 elif fdisk
-v >/dev
/null
2>&1; then
185 vterr
"Both parted and fdisk are not found in the system, Ventoy can't create new partitions."
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."
201 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
202 disk_size_gb
=$(expr $disk_sector_num / 2097152)
204 if [ $disk_sector_num -gt 4294967296 ] && [ -z "$VTGPT" ]; then
205 vterr
"$DISK is over 2TB size, MBR will not work on it."
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)
213 if [ $disk_sector_num -le $reserve_sector_num ]; then
214 vterr
"Can't reserve $RESERVE_SIZE_MB MB space from $DISK"
221 parted
-s $DISK p
2>&1 | grep Model
222 echo "Size : $disk_size_gb GB"
223 if [ -n "$VTGPT" ]; then
230 if [ -n "$RESERVE_SPACE" ]; then
231 echo "You will reserve $RESERVE_SIZE_MB MB disk space "
236 vtwarn
"You will install Ventoy to $DISK."
237 vtwarn
"All the data on the disk $DISK will be lost!!!"
240 read -p 'Continue? (y/n) ' Answer
241 if [ "$Answer" != "y" ]; then
242 if [ "$Answer" != "Y" ]; then
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
256 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
257 vterr
"No enough space in disk $DISK"
262 check_umount_disk
"$DISK"
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."
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
273 vtdebug
"format_ventoy_disk_mbr $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
274 format_ventoy_disk_mbr
$RESERVE_SIZE_MB $DISK $PARTTOOL
279 # DiskSize > 32GB Cluster Size use 128KB
280 # DiskSize < 32GB Cluster Size use 32KB
281 if [ $disk_size_gb -gt 32 ]; then
287 PART1
=$(get_disk_part_name $DISK 1)
288 PART2
=$(get_disk_part_name $DISK 2)
291 dd status
=none conv
=fsync
if=/dev
/zero of
=$DISK bs
=512 count
=32 seek
=$part2_start_sector
294 vtinfo
"Format partition 1 ..."
295 mkexfatfs
-n "$VTNEW_LABEL" -s $cluster_sectors ${PART1}
297 vtinfo
"writing data to disk ..."
298 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=446
300 if [ -n "$VTGPT" ]; then
301 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
302 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
303 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
305 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
309 check_umount_disk
"$DISK"
311 xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start_sector
314 testUUIDStr
=$(vtoy_gen_uuid | hexdump -C)
315 vtdebug
"test uuid: $testUUIDStr"
318 vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} seek
=384 bs
=1 count
=16
321 vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} skip
=12 seek
=440 bs
=1 count
=4
323 vtinfo
"sync data ..."
326 vtinfo
"esp partition processing ..."
327 if [ "$SECUREBOOT" != "YES" ]; then
329 check_umount_disk
"$DISK"
330 vtoycli partresize
-s $DISK $part2_start_sector
334 vtinfo
"Install Ventoy to $DISK successfully finished."
337 elif [ "$MODE" = "install" -a -n "$NONDESTRUCTIVE" ]; then
338 vtdebug
"non-destructive install Ventoy ..."
340 version
=$(get_disk_ventoy_version $DISK)
341 if [ $?
-eq 0 ]; then
342 if [ -z "$FORCE" ]; then
343 vtwarn
"$DISK already contains a Ventoy with version $version."
344 vtwarn
"You can not do and don not need non-destructive installation."
350 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
351 disk_size_gb
=$(expr $disk_sector_num / 2097152)
353 if vtoycli partresize
-t $DISK; then
361 parted
-s $DISK p
2>&1 | grep Model
362 echo "Size : $disk_size_gb GB"
363 echo "Style: $OldStyle"
367 vtwarn
"Ventoy will try non-destructive installation on $DISK if possible."
370 read -p 'Continue? (y/n) ' Answer
371 if [ "$Answer" != "y" ]; then
372 if [ "$Answer" != "Y" ]; then
377 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
378 vterr
"No enough space in disk $DISK"
382 PART1
=$(get_disk_part_name $DISK 1)
383 PART2
=$(get_disk_part_name $DISK 2)
385 #Part1 size in MB aligned with 4KB
386 PART1_SECTORS
=$(cat /sys/class/block/${PART1#/dev/}/size)
387 PART1_4K
=$(expr $PART1_SECTORS / 8)
388 PART1_MB
=$(expr $PART1_4K / 256)
389 PART1_NEW_MB
=$(expr $PART1_MB - 32)
391 echo "$PART1 is ${PART1_MB}MB"
393 #check partition layout
394 echo "check partition layout ..."
395 vtoycli partresize
-c $DISK
397 if [ $vtRet -eq 0 ]; then
401 check_umount_disk
"$DISK"
403 check_umount_disk
"$DISK"
405 if [ $vtRet -eq 1 ]; then
406 echo "Free space enough, start install..."
407 part2_start_sector
=$(expr $PART1_SECTORS + 2048)
408 elif [ $vtRet -eq 2 ]; then
409 echo "We need to shrink partition 1 firstly ..."
411 PART1_BLKID
=$(blkid $PART1)
414 if echo $PART1_BLKID | egrep -q -i 'TYPE=ntfs|TYPE=.ntfs'; then
415 echo "Partition 1 contains NTFS filesystem"
418 if [ $?
-ne 0 ]; then
419 echo "###[FAIL] ntfsresize not found. Please install ntfs-3g package."
423 echo "ntfsfix -b -d $PART1 ..."
426 echo "ntfsresize --size ${PART1_NEW_MB}Mi $PART1 ..."
427 ntfsresize
-f --size ${PART1_NEW_MB}Mi
$PART1
428 if [ $?
-ne 0 ]; then
429 echo "###[FAIL] ntfsresize failed."
432 elif echo $PART1_BLKID | egrep -q -i 'TYPE=ext[2-4]|TYPE=.ext[2-4]'; then
433 echo "Partition 1 contains EXT filesystem"
436 if [ $?
-ne 0 ]; then
437 echo "###[FAIL] resize2fs not found. Please install e2fsprogs package."
441 echo "e2fsck -f $PART1 ..."
444 echo "resize2fs $PART1 ${PART1_NEW_MB}M ..."
445 resize2fs
$PART1 ${PART1_NEW_MB}M
446 if [ $?
-ne 0 ]; then
447 echo "###[FAIL] resize2fs failed."
451 echo "###[FAIL] Unsupported filesystem in partition 1."
456 PART1_NEW_END_MB
=$(expr $PART1_NEW_MB + 1)
457 part2_start_sector
=$(expr $PART1_NEW_END_MB \* 2048)
461 vtinfo
"writing data to disk part2_start=$part2_start_sector ..."
463 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=440
465 if [ "$OldStyle" = "GPT" ]; then
466 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
467 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
468 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
470 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
473 xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start_sector
476 testUUIDStr
=$(vtoy_gen_uuid | hexdump -C)
477 vtdebug
"test uuid: $testUUIDStr"
480 vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} seek
=384 bs
=1 count
=16
482 vtinfo
"sync data ..."
485 vtinfo
"esp partition processing ..."
486 if [ "$SECUREBOOT" != "YES" ]; then
487 vtoycli partresize
-s $DISK $part2_start_sector
490 vtinfo
"update partition table $DISK $part2_start_sector ..."
491 vtoycli partresize
-p $DISK $part2_start_sector
492 if [ $?
-eq 0 ]; then
495 vtinfo
"Ventoy non-destructive installation on $DISK successfully finished."
499 vterr
"Ventoy non-destructive installation on $DISK failed."
504 vtdebug
"update Ventoy ..."
506 oldver
=$(get_disk_ventoy_version $DISK)
507 if [ $?
-ne 0 ]; then
508 if is_disk_contains_ventoy
$DISK; then
511 vtwarn
"$DISK does not contain Ventoy or data corrupted"
513 vtwarn
"Please use -i option if you want to install ventoy to $DISK"
519 #reserve secure boot option
520 if [ -z "$SECUREBOOT" ]; then
521 if check_disk_secure_boot
$DISK; then
528 curver
=$(cat ./ventoy/version)
530 vtinfo
"Upgrade operation is safe, all the data in the 1st partition (iso files and other) will be unchanged!"
533 read -p "Update Ventoy $oldver ===> $curver Continue? (y/n)" Answer
534 if [ "$Answer" != "y" ]; then
535 if [ "$Answer" != "Y" ]; then
540 PART2
=$(get_disk_part_name $DISK 2)
541 SHORT_PART2
=${PART2#/dev/}
542 part2_start
=$(cat /sys/class/block/$SHORT_PART2/start)
544 PART1_TYPE
=$(dd if=$DISK bs=1 count=1 skip=450 status=none | hexdump -n1 -e '1/1 "%02X"')
548 dd status
=none conv
=fsync
if=${DISK} skip
=384 bs
=1 count
=16 of
=.
/diskuuid.bin
550 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=440
551 dd status
=none conv
=fsync
if=.
/diskuuid.bin of
=$DISK bs
=1 count
=16 seek
=384
556 dd status
=none conv
=fsync
if=${DISK} skip
=2040 bs
=512 count
=8 of
=.
/rsvdata.bin
558 if [ "$PART1_TYPE" = "EE" ]; then
559 vtdebug
"This is GPT partition style ..."
560 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
561 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
562 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
564 vtdebug
"This is MBR partition style ..."
566 PART1_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=446 status=none | hexdump -n1 -e '1/1 "%02X"')
567 PART2_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=462 status=none | hexdump -n1 -e '1/1 "%02X"')
569 vtdebug
"PART1_ACTIVE=$PART1_ACTIVE PART2_ACTIVE=$PART2_ACTIVE"
570 if [ "$PART1_ACTIVE" = "00" ] && [ "$PART2_ACTIVE" = "80" ]; then
571 vtdebug
"change 1st partition active, 2nd partition inactive ..."
572 echo -en '\x80' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=446 status
=none
573 echo -en '\x00' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=462 status
=none
575 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
578 dd status
=none conv
=fsync
if=.
/rsvdata.bin seek
=2040 bs
=512 count
=8 of
=${DISK}
581 check_umount_disk
"$DISK"
583 xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start
586 vtinfo
"esp partition processing ..."
587 if [ "$SECUREBOOT" != "YES" ]; then
589 check_umount_disk
"$DISK"
590 vtoycli partresize
-s $DISK $part2_start
594 vtinfo
"Update Ventoy on $DISK successfully finished."