]>
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 enabled)'
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)'
28 if [ "$1" = "-i" ]; then
30 elif [ "$1" = "-I" ]; then
33 elif [ "$1" = "-n" ]; then
35 elif [ "$1" = "-u" ]; then
37 elif [ "$1" = "-l" ]; then
39 elif [ "$1" = "-s" ]; then
41 elif [ "$1" = "-S" ]; then
43 elif [ "$1" = "-g" ]; then
45 elif [ "$1" = "-L" ]; then
48 elif [ "$1" = "-r" ]; then
52 elif [ "$1" = "-V" ] || [ "$1" = "--version" ]; then
54 elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
58 if ! [ -b "$1" ]; then
59 vterr
"$1 is NOT a valid device"
64 # Resolve symlinks now, will be needed to look up information about the device in
65 # the /sys/ filesystem, for example /sys/class/block/${DISK#/dev/}/start
66 # The main use case is supporting /dev/disk/by-id/ symlinks instead of raw devices
67 if [ -L "$DISK" ]; then
68 DISK
=$(readlink -e -n "$DISK")
75 if [ -z "$MODE" ]; then
80 if ! [ -b "$DISK" ]; then
81 vterr
"Disk $DISK does not exist"
85 if [ -e /sys
/class
/block
/${DISK#/dev/}/start
]; then
86 vterr
"$DISK is a partition, please use the whole disk."
88 vterr
" sudo sh Ventoy2Disk.sh -i /dev/sdb1 <=== This is wrong"
89 vtinfo
" sudo sh Ventoy2Disk.sh -i /dev/sdb <=== This is right"
94 if [ -n "$RESERVE_SPACE" -a "$MODE" = "install" ]; then
95 if echo $RESERVE_SIZE_MB | grep -q '^[0-9][0-9]*$'; then
96 vtdebug
"User will reserve $RESERVE_SIZE_MB MB disk space"
98 vterr
"$RESERVE_SIZE_MB is invalid for reserved space"
103 vtdebug
"MODE=$MODE FORCE=$FORCE RESERVE_SPACE=$RESERVE_SPACE RESERVE_SIZE_MB=$RESERVE_SIZE_MB"
106 if check_tool_work_ok
; then
107 vtdebug
"check tool work ok"
109 vterr
"Some tools can not run on current system. Please check log.txt for details."
113 if [ "$MODE" = "list" ]; then
114 version
=$(get_disk_ventoy_version $DISK)
115 if [ $?
-eq 0 ]; then
116 echo "Ventoy Version in Disk: $version"
118 vtPart1Type
=$(dd if=$DISK bs=1 count=1 skip=450 status=none | hexdump -n1 -e '1/1 "%02X"')
119 if [ "$vtPart1Type" = "EE" ]; then
120 echo "Disk Partition Style : GPT"
122 echo "Disk Partition Style : MBR"
125 if check_disk_secure_boot
$DISK; then
126 echo "Secure Boot Support : YES"
128 echo "Secure Boot Support : NO"
131 echo "Ventoy Version: NA"
138 check_umount_disk
"$DISK"
140 if grep "$DISK" /proc
/mounts
; then
141 vterr
"$DISK is already mounted, please umount it first!"
145 #check swap partition
146 if swapon
--help 2>&1 | grep -q '^ \-s,'; then
147 if swapon
-s | grep -q "^${DISK}[0-9]"; then
148 vterr
"$DISK is used as swap, please swapoff it first!"
154 if dd if="$DISK" of
=/dev
/null bs
=1 count
=1 >/dev
/null
2>&1; then
155 vtdebug
"root permission check ok ..."
157 vterr
"Failed to access $DISK, maybe root privilege is needed!"
163 #check tmp_mnt directory
164 if [ -d .
/tmp_mnt
]; then
165 vtdebug
"There is a tmp_mnt directory, now delete it."
166 umount .
/tmp_mnt
>/dev
/null
2>&1
168 if [ -d .
/tmp_mnt
]; then
169 vterr
"tmp_mnt directory exists, please delete it first."
175 if [ "$MODE" = "install" -a -z "$NONDESTRUCTIVE" ]; then
176 vtdebug
"install Ventoy ..."
178 if [ -n "$VTGPT" ]; then
179 if parted
-v > /dev
/null
2>&1; then
182 vterr
"parted is not found in the system, Ventoy can't create new partitions without it."
183 vterr
"You should install \"GNU parted\" first."
187 if parted
-v > /dev
/null
2>&1; then
189 elif fdisk
-v >/dev
/null
2>&1; then
192 vterr
"Both parted and fdisk are not found in the system, Ventoy can't create new partitions."
197 version
=$(get_disk_ventoy_version $DISK)
198 if [ $?
-eq 0 ]; then
199 if [ -z "$FORCE" ]; then
200 vtwarn
"$DISK already contains a Ventoy with version $version"
201 vtwarn
"Use -u option to do a safe upgrade operation."
202 vtwarn
"OR if you really want to reinstall Ventoy to $DISK, please use -I option."
208 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
209 disk_size_gb
=$(expr $disk_sector_num / 2097152)
211 if [ $disk_sector_num -gt 4294967296 ] && [ -z "$VTGPT" ]; then
212 vterr
"$DISK is over 2TB size, MBR will not work on it."
216 if [ -n "$RESERVE_SPACE" ]; then
217 sum_size_mb
=$(expr $RESERVE_SIZE_MB + $VENTOY_PART_SIZE_MB)
218 reserve_sector_num
=$(expr $sum_size_mb \* 2048)
220 if [ $disk_sector_num -le $reserve_sector_num ]; then
221 vterr
"Can't reserve $RESERVE_SIZE_MB MB space from $DISK"
228 parted
-s $DISK p
2>&1 | grep Model
229 echo "Size : $disk_size_gb GB"
230 if [ -n "$VTGPT" ]; then
237 if [ -n "$RESERVE_SPACE" ]; then
238 echo "You will reserve $RESERVE_SIZE_MB MB disk space "
243 vtwarn
"You will install Ventoy to $DISK."
244 vtwarn
"All the data on the disk $DISK will be lost!!!"
247 read -p 'Continue? (y/n) ' Answer
248 if [ "$Answer" != "y" ]; then
249 if [ "$Answer" != "Y" ]; then
255 vtwarn
"All the data on the disk $DISK will be lost!!!"
256 read -p 'Double-check. Continue? (y/n) ' Answer
257 if [ "$Answer" != "y" ]; then
258 if [ "$Answer" != "Y" ]; then
263 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
264 vterr
"No enough space in disk $DISK"
269 check_umount_disk
"$DISK"
271 if ! dd if=/dev
/zero of
=$DISK bs
=64 count
=512 status
=none conv
=fsync
; then
272 vterr
"Write data to $DISK failed, please check whether it's in use."
276 if [ -n "$VTGPT" ]; then
277 vtdebug
"format_ventoy_disk_gpt $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
278 format_ventoy_disk_gpt
$RESERVE_SIZE_MB $DISK $PARTTOOL
280 vtdebug
"format_ventoy_disk_mbr $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
281 format_ventoy_disk_mbr
$RESERVE_SIZE_MB $DISK $PARTTOOL
286 # DiskSize > 32GB Cluster Size use 128KB
287 # DiskSize < 32GB Cluster Size use 32KB
288 if [ $disk_size_gb -gt 32 ]; then
294 PART1
=$(get_disk_part_name $DISK 1)
295 PART2
=$(get_disk_part_name $DISK 2)
298 dd status
=none conv
=fsync
if=/dev
/zero of
=$DISK bs
=512 count
=32 seek
=$part2_start_sector
301 wait_and_create_part
${PART1} ${PART2}
302 if [ -b ${PART1} ]; then
303 vtinfo
"Format partition 1 ${PART1} ..."
304 mkexfatfs
-n "$VTNEW_LABEL" -s $cluster_sectors ${PART1}
305 if [ $?
-ne 0 ]; then
306 echo "mkexfatfs failed, now retry..."
307 mkexfatfs
-n "$VTNEW_LABEL" -s $cluster_sectors ${PART1}
308 if [ $?
-ne 0 ]; then
309 echo "######### mkexfatfs failed, exit ########"
313 echo "mkexfatfs success"
316 vterr
"${PART1} NOT exist"
319 vtinfo
"writing data to disk ..."
320 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=446
322 if [ -n "$VTGPT" ]; then
323 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
324 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
325 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
327 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
331 check_umount_disk
"$DISK"
333 xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start_sector
336 testUUIDStr
=$(vtoy_gen_uuid | hexdump -C)
337 vtdebug
"test uuid: $testUUIDStr"
340 vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} seek
=384 bs
=1 count
=16
343 vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} skip
=12 seek
=440 bs
=1 count
=4
345 vtinfo
"sync data ..."
348 vtinfo
"esp partition processing ..."
350 if [ "$SECUREBOOT" != "YES" ]; then
352 check_umount_disk
"$DISK"
353 vtoycli partresize
-s $DISK $part2_start_sector
357 vtinfo
"Install Ventoy to $DISK successfully finished."
360 elif [ "$MODE" = "install" -a -n "$NONDESTRUCTIVE" ]; then
361 vtdebug
"non-destructive install Ventoy ..."
363 version
=$(get_disk_ventoy_version $DISK)
364 if [ $?
-eq 0 ]; then
365 if [ -z "$FORCE" ]; then
366 vtwarn
"$DISK already contains a Ventoy with version $version."
367 vtwarn
"You can not do and don not need non-destructive installation."
373 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
374 disk_size_gb
=$(expr $disk_sector_num / 2097152)
376 if vtoycli partresize
-t $DISK; then
384 parted
-s $DISK p
2>&1 | grep Model
385 echo "Size : $disk_size_gb GB"
386 echo "Style: $OldStyle"
390 vtwarn
"Ventoy will try non-destructive installation on $DISK if possible."
393 read -p 'Continue? (y/n) ' Answer
394 if [ "$Answer" != "y" ]; then
395 if [ "$Answer" != "Y" ]; then
400 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
401 vterr
"No enough space in disk $DISK"
405 PART1
=$(get_disk_part_name $DISK 1)
406 PART2
=$(get_disk_part_name $DISK 2)
408 #Part1 size in MB aligned with 4KB
409 PART1_SECTORS
=$(cat /sys/class/block/${PART1#/dev/}/size)
410 PART1_4K
=$(expr $PART1_SECTORS / 8)
411 PART1_MB
=$(expr $PART1_4K / 256)
412 PART1_NEW_MB
=$(expr $PART1_MB - 32)
414 echo "$PART1 is ${PART1_MB}MB"
416 #check partition layout
417 echo "check partition layout ..."
418 vtoycli partresize
-c $DISK
420 if [ $vtRet -eq 0 ]; then
424 check_umount_disk
"$DISK"
426 check_umount_disk
"$DISK"
428 if [ $vtRet -eq 1 ]; then
429 echo "Free space enough, start install..."
430 part2_start_sector
=$(expr $PART1_SECTORS + 2048)
431 elif [ $vtRet -eq 2 ]; then
432 echo "We need to shrink partition 1 firstly ..."
434 PART1_BLKID
=$(blkid $PART1)
437 if echo $PART1_BLKID | egrep -q -i 'TYPE=ntfs|TYPE=.ntfs'; then
438 echo "Partition 1 contains NTFS filesystem"
441 if [ $?
-ne 0 ]; then
442 echo "###[FAIL] ntfsresize not found. Please install ntfs-3g package."
446 echo "ntfsfix -b -d $PART1 ..."
449 echo "ntfsresize --size ${PART1_NEW_MB}Mi $PART1 ..."
450 ntfsresize
-f --size ${PART1_NEW_MB}Mi
$PART1
451 if [ $?
-ne 0 ]; then
452 echo "###[FAIL] ntfsresize failed."
455 elif echo $PART1_BLKID | egrep -q -i 'TYPE=ext[2-4]|TYPE=.ext[2-4]'; then
456 echo "Partition 1 contains EXT filesystem"
459 if [ $?
-ne 0 ]; then
460 echo "###[FAIL] resize2fs not found. Please install e2fsprogs package."
464 echo "e2fsck -f $PART1 ..."
467 echo "resize2fs $PART1 ${PART1_NEW_MB}M ..."
468 resize2fs
$PART1 ${PART1_NEW_MB}M
469 if [ $?
-ne 0 ]; then
470 echo "###[FAIL] resize2fs failed."
474 echo "###[FAIL] Unsupported filesystem in partition 1."
479 PART1_NEW_END_MB
=$(expr $PART1_NEW_MB + 1)
480 part2_start_sector
=$(expr $PART1_NEW_END_MB \* 2048)
484 vtinfo
"writing data to disk part2_start=$part2_start_sector ..."
486 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=440
488 if [ "$OldStyle" = "GPT" ]; then
489 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
490 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
491 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
493 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
496 xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start_sector
499 testUUIDStr
=$(vtoy_gen_uuid | hexdump -C)
500 vtdebug
"test uuid: $testUUIDStr"
503 vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} seek
=384 bs
=1 count
=16
505 vtinfo
"sync data ..."
508 vtinfo
"esp partition processing ..."
509 if [ "$SECUREBOOT" != "YES" ]; then
510 vtoycli partresize
-s $DISK $part2_start_sector
513 vtinfo
"update partition table $DISK $part2_start_sector ..."
514 vtoycli partresize
-p $DISK $part2_start_sector
515 if [ $?
-eq 0 ]; then
518 vtinfo
"Ventoy non-destructive installation on $DISK successfully finished."
522 vterr
"Ventoy non-destructive installation on $DISK failed."
527 vtdebug
"update Ventoy ..."
529 oldver
=$(get_disk_ventoy_version $DISK)
530 if [ $?
-ne 0 ]; then
531 if is_disk_contains_ventoy
$DISK; then
534 vtwarn
"$DISK does not contain Ventoy or data corrupted"
536 vtwarn
"Please use -i option if you want to install ventoy to $DISK"
542 #reserve secure boot option
543 if [ -z "$SECUREBOOT" ]; then
544 if check_disk_secure_boot
$DISK; then
551 curver
=$(cat ./ventoy/version)
553 vtinfo
"Upgrade operation is safe, all the data in the 1st partition (iso files and other) will be unchanged!"
556 read -p "Update Ventoy $oldver ===> $curver Continue? (y/n) " Answer
557 if [ "$Answer" != "y" ]; then
558 if [ "$Answer" != "Y" ]; then
563 PART2
=$(get_disk_part_name $DISK 2)
564 SHORT_PART2
=${PART2#/dev/}
565 part2_start
=$(cat /sys/class/block/$SHORT_PART2/start)
567 PART1_TYPE
=$(dd if=$DISK bs=1 count=1 skip=450 status=none | hexdump -n1 -e '1/1 "%02X"')
571 dd status
=none conv
=fsync
if=${DISK} skip
=384 bs
=1 count
=16 of
=.
/diskuuid.bin
573 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=440
574 dd status
=none conv
=fsync
if=.
/diskuuid.bin of
=$DISK bs
=1 count
=16 seek
=384
579 dd status
=none conv
=fsync
if=${DISK} skip
=2040 bs
=512 count
=8 of
=.
/rsvdata.bin
581 if [ "$PART1_TYPE" = "EE" ]; then
582 vtdebug
"This is GPT partition style ..."
583 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
584 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
585 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
587 vtdebug
"This is MBR partition style ..."
589 PART1_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=446 status=none | hexdump -n1 -e '1/1 "%02X"')
590 PART2_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=462 status=none | hexdump -n1 -e '1/1 "%02X"')
592 vtdebug
"PART1_ACTIVE=$PART1_ACTIVE PART2_ACTIVE=$PART2_ACTIVE"
593 if [ "$PART1_ACTIVE" = "00" ] && [ "$PART2_ACTIVE" = "80" ]; then
594 vtdebug
"change 1st partition active, 2nd partition inactive ..."
595 echo -en '\x80' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=446 status
=none
596 echo -en '\x00' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=462 status
=none
598 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
601 dd status
=none conv
=fsync
if=.
/rsvdata.bin seek
=2040 bs
=512 count
=8 of
=${DISK}
604 check_umount_disk
"$DISK"
606 xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start
609 vtinfo
"esp partition processing ..."
610 if [ "$SECUREBOOT" != "YES" ]; then
612 check_umount_disk
"$DISK"
613 vtoycli partresize
-s $DISK $part2_start
617 vtinfo
"Update Ventoy on $DISK successfully finished."