]>
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 if [ "$PARTTOOL" = "parted" ]; then
198 if parted
-s $DISK p
2>&1 | grep -i -q 'sector size.*4096.*4096'; then
199 vterr
"Currently Ventoy does not support 4K native device."
203 if fdisk
-l $DISK | grep -i -q 'sector size.*4096.*4096'; then
204 vterr
"Currently Ventoy does not support 4K native device."
210 version
=$(get_disk_ventoy_version $DISK)
211 if [ $?
-eq 0 ]; then
212 if [ -z "$FORCE" ]; then
213 vtwarn
"$DISK already contains a Ventoy with version $version"
214 vtwarn
"Use -u option to do a safe upgrade operation."
215 vtwarn
"OR if you really want to reinstall Ventoy to $DISK, please use -I option."
221 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
222 disk_size_gb
=$(expr $disk_sector_num / 2097152)
224 if [ $disk_sector_num -gt 4294967296 ] && [ -z "$VTGPT" ]; then
225 vterr
"$DISK is over 2TB size, MBR will not work on it."
229 if [ -n "$RESERVE_SPACE" ]; then
230 sum_size_mb
=$(expr $RESERVE_SIZE_MB + $VENTOY_PART_SIZE_MB)
231 reserve_sector_num
=$(expr $sum_size_mb \* 2048)
233 if [ $disk_sector_num -le $reserve_sector_num ]; then
234 vterr
"Can't reserve $RESERVE_SIZE_MB MB space from $DISK"
241 parted
-s $DISK p
2>&1 | grep Model
242 echo "Size : $disk_size_gb GB"
243 if [ -n "$VTGPT" ]; then
250 if [ -n "$RESERVE_SPACE" ]; then
251 echo "You will reserve $RESERVE_SIZE_MB MB disk space "
256 vtwarn
"You will install Ventoy to $DISK."
257 vtwarn
"All the data on the disk $DISK will be lost!!!"
260 read -p 'Continue? (y/n) ' Answer
261 if [ "$Answer" != "y" ]; then
262 if [ "$Answer" != "Y" ]; then
268 vtwarn
"All the data on the disk $DISK will be lost!!!"
269 read -p 'Double-check. Continue? (y/n) ' Answer
270 if [ "$Answer" != "y" ]; then
271 if [ "$Answer" != "Y" ]; then
276 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
277 vterr
"No enough space in disk $DISK"
282 check_umount_disk
"$DISK"
284 if ! dd if=/dev
/zero of
=$DISK bs
=64 count
=512 status
=none conv
=fsync
; then
285 vterr
"Write data to $DISK failed, please check whether it's in use."
289 if [ -n "$VTGPT" ]; then
290 vtdebug
"format_ventoy_disk_gpt $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
291 format_ventoy_disk_gpt
$RESERVE_SIZE_MB $DISK $PARTTOOL
293 vtdebug
"format_ventoy_disk_mbr $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
294 format_ventoy_disk_mbr
$RESERVE_SIZE_MB $DISK $PARTTOOL
299 # DiskSize > 32GB Cluster Size use 128KB
300 # DiskSize < 32GB Cluster Size use 32KB
301 if [ $disk_size_gb -gt 32 ]; then
307 PART1
=$(get_disk_part_name $DISK 1)
308 PART2
=$(get_disk_part_name $DISK 2)
311 dd status
=none conv
=fsync
if=/dev
/zero of
=$DISK bs
=512 count
=32 seek
=$part2_start_sector
314 wait_and_create_part
${PART1} ${PART2}
315 if [ -b ${PART1} ]; then
316 vtinfo
"Format partition 1 ${PART1} ..."
317 mkexfatfs
-n "$VTNEW_LABEL" -s $cluster_sectors ${PART1}
318 if [ $?
-ne 0 ]; then
319 echo "mkexfatfs failed, now retry..."
320 mkexfatfs
-n "$VTNEW_LABEL" -s $cluster_sectors ${PART1}
321 if [ $?
-ne 0 ]; then
322 echo "######### mkexfatfs failed, exit ########"
326 echo "mkexfatfs success"
329 vterr
"${PART1} NOT exist"
332 vtinfo
"writing data to disk ..."
333 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=446
335 if [ -n "$VTGPT" ]; then
336 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
337 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
338 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
340 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
344 check_umount_disk
"$DISK"
346 xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start_sector
349 testUUIDStr
=$(vtoy_gen_uuid | hexdump -C)
350 vtdebug
"test uuid: $testUUIDStr"
353 vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} seek
=384 bs
=1 count
=16
356 vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} skip
=12 seek
=440 bs
=1 count
=4
358 vtinfo
"sync data ..."
361 vtinfo
"esp partition processing ..."
363 if [ "$SECUREBOOT" != "YES" ]; then
365 check_umount_disk
"$DISK"
366 vtoycli partresize
-s $DISK $part2_start_sector
370 vtinfo
"Install Ventoy to $DISK successfully finished."
373 elif [ "$MODE" = "install" -a -n "$NONDESTRUCTIVE" ]; then
374 vtdebug
"non-destructive install Ventoy ..."
376 version
=$(get_disk_ventoy_version $DISK)
377 if [ $?
-eq 0 ]; then
378 if [ -z "$FORCE" ]; then
379 vtwarn
"$DISK already contains a Ventoy with version $version."
380 vtwarn
"You can not do and don not need non-destructive installation."
386 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
387 disk_size_gb
=$(expr $disk_sector_num / 2097152)
389 if vtoycli partresize
-t $DISK; then
397 parted
-s $DISK p
2>&1 | grep Model
398 echo "Size : $disk_size_gb GB"
399 echo "Style: $OldStyle"
403 vtwarn
"Ventoy will try non-destructive installation on $DISK if possible."
406 read -p 'Continue? (y/n) ' Answer
407 if [ "$Answer" != "y" ]; then
408 if [ "$Answer" != "Y" ]; then
413 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
414 vterr
"No enough space in disk $DISK"
418 PART1
=$(get_disk_part_name $DISK 1)
419 PART2
=$(get_disk_part_name $DISK 2)
421 #Part1 size in MB aligned with 4KB
422 PART1_SECTORS
=$(cat /sys/class/block/${PART1#/dev/}/size)
423 PART1_4K
=$(expr $PART1_SECTORS / 8)
424 PART1_MB
=$(expr $PART1_4K / 256)
425 PART1_NEW_MB
=$(expr $PART1_MB - 32)
427 echo "$PART1 is ${PART1_MB}MB"
429 #check partition layout
430 echo "check partition layout ..."
431 vtoycli partresize
-c $DISK
433 if [ $vtRet -eq 0 ]; then
437 check_umount_disk
"$DISK"
439 check_umount_disk
"$DISK"
441 if [ $vtRet -eq 1 ]; then
442 echo "Free space enough, start install..."
443 part2_start_sector
=$(expr $PART1_SECTORS + 2048)
444 elif [ $vtRet -eq 2 ]; then
445 echo "We need to shrink partition 1 firstly ..."
447 PART1_BLKID
=$(blkid $PART1)
450 if echo $PART1_BLKID | grep -E -q -i 'TYPE=ntfs|TYPE=.ntfs'; then
451 echo "Partition 1 contains NTFS filesystem"
454 if [ $?
-ne 0 ]; then
455 echo "###[FAIL] ntfsresize not found. Please install ntfs-3g package."
459 echo "ntfsfix -b -d $PART1 ..."
462 echo "ntfsresize --size ${PART1_NEW_MB}Mi $PART1 ..."
463 ntfsresize
-f --size ${PART1_NEW_MB}Mi
$PART1
464 if [ $?
-ne 0 ]; then
465 echo "###[FAIL] ntfsresize failed."
468 elif echo $PART1_BLKID | grep -E -q -i 'TYPE=ext[2-4]|TYPE=.ext[2-4]'; then
469 echo "Partition 1 contains EXT filesystem"
472 if [ $?
-ne 0 ]; then
473 echo "###[FAIL] resize2fs not found. Please install e2fsprogs package."
477 echo "e2fsck -f $PART1 ..."
480 echo "resize2fs $PART1 ${PART1_NEW_MB}M ..."
481 resize2fs
$PART1 ${PART1_NEW_MB}M
482 if [ $?
-ne 0 ]; then
483 echo "###[FAIL] resize2fs failed."
487 echo "###[FAIL] Unsupported filesystem in partition 1."
492 PART1_NEW_END_MB
=$(expr $PART1_NEW_MB + 1)
493 part2_start_sector
=$(expr $PART1_NEW_END_MB \* 2048)
497 vtinfo
"writing data to disk part2_start=$part2_start_sector ..."
499 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=440
501 if [ "$OldStyle" = "GPT" ]; then
502 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
503 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
504 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
506 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
509 xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start_sector
512 testUUIDStr
=$(vtoy_gen_uuid | hexdump -C)
513 vtdebug
"test uuid: $testUUIDStr"
516 vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} seek
=384 bs
=1 count
=16
518 vtinfo
"sync data ..."
521 vtinfo
"esp partition processing ..."
522 if [ "$SECUREBOOT" != "YES" ]; then
523 vtoycli partresize
-s $DISK $part2_start_sector
526 vtinfo
"update partition table $DISK $part2_start_sector ..."
527 vtoycli partresize
-p $DISK $part2_start_sector
528 if [ $?
-eq 0 ]; then
531 vtinfo
"Ventoy non-destructive installation on $DISK successfully finished."
535 vterr
"Ventoy non-destructive installation on $DISK failed."
540 vtdebug
"update Ventoy ..."
542 oldver
=$(get_disk_ventoy_version $DISK)
543 if [ $?
-ne 0 ]; then
544 if is_disk_contains_ventoy
$DISK; then
547 vtwarn
"$DISK does not contain Ventoy or data corrupted"
549 vtwarn
"Please use -i option if you want to install ventoy to $DISK"
555 #reserve secure boot option
556 if [ -z "$SECUREBOOT" ]; then
557 if check_disk_secure_boot
$DISK; then
564 curver
=$(cat ./ventoy/version)
566 vtinfo
"Upgrade operation is safe, all the data in the 1st partition (iso files and other) will be unchanged!"
569 read -p "Update Ventoy $oldver ===> $curver Continue? (y/n) " Answer
570 if [ "$Answer" != "y" ]; then
571 if [ "$Answer" != "Y" ]; then
576 PART2
=$(get_disk_part_name $DISK 2)
577 SHORT_PART2
=${PART2#/dev/}
578 part2_start
=$(cat /sys/class/block/$SHORT_PART2/start)
580 PART1_TYPE
=$(dd if=$DISK bs=1 count=1 skip=450 status=none | hexdump -n1 -e '1/1 "%02X"')
584 dd status
=none conv
=fsync
if=${DISK} skip
=384 bs
=1 count
=16 of
=.
/diskuuid.bin
586 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=440
587 dd status
=none conv
=fsync
if=.
/diskuuid.bin of
=$DISK bs
=1 count
=16 seek
=384
592 dd status
=none conv
=fsync
if=${DISK} skip
=2040 bs
=512 count
=8 of
=.
/rsvdata.bin
594 if [ "$PART1_TYPE" = "EE" ]; then
595 vtdebug
"This is GPT partition style ..."
596 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
597 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
598 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
600 vtdebug
"This is MBR partition style ..."
602 PART1_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=446 status=none | hexdump -n1 -e '1/1 "%02X"')
603 PART2_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=462 status=none | hexdump -n1 -e '1/1 "%02X"')
605 vtdebug
"PART1_ACTIVE=$PART1_ACTIVE PART2_ACTIVE=$PART2_ACTIVE"
606 if [ "$PART1_ACTIVE" = "00" ] && [ "$PART2_ACTIVE" = "80" ]; then
607 vtdebug
"change 1st partition active, 2nd partition inactive ..."
608 echo -en '\x80' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=446 status
=none
609 echo -en '\x00' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=462 status
=none
611 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
614 dd status
=none conv
=fsync
if=.
/rsvdata.bin seek
=2040 bs
=512 count
=8 of
=${DISK}
617 check_umount_disk
"$DISK"
619 xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start
622 vtinfo
"esp partition processing ..."
623 if [ "$SECUREBOOT" != "YES" ]; then
625 check_umount_disk
"$DISK"
626 vtoycli partresize
-s $DISK $part2_start
630 if [ "$PART1_TYPE" = "EE" ]; then
631 vtinfo
"update esp partition attribute"
638 vtinfo
"Update Ventoy on $DISK successfully finished."