]>
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 wait_and_create_part
${PART1} ${PART2}
295 if [ -b ${PART1} ]; then
296 vtinfo
"Format partition 1 ${PART1} ..."
297 mkexfatfs
-n "$VTNEW_LABEL" -s $cluster_sectors ${PART1}
298 if [ $?
-ne 0 ]; then
299 echo "mkexfatfs failed, now retry..."
300 mkexfatfs
-n "$VTNEW_LABEL" -s $cluster_sectors ${PART1}
301 if [ $?
-ne 0 ]; then
302 echo "######### mkexfatfs failed, exit ########"
306 echo "mkexfatfs success"
309 vterr
"${PART1} NOT exist"
312 vtinfo
"writing data to disk ..."
313 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=446
315 if [ -n "$VTGPT" ]; then
316 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
317 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
318 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
320 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
324 check_umount_disk
"$DISK"
326 xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start_sector
329 testUUIDStr
=$(vtoy_gen_uuid | hexdump -C)
330 vtdebug
"test uuid: $testUUIDStr"
333 vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} seek
=384 bs
=1 count
=16
336 vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} skip
=12 seek
=440 bs
=1 count
=4
338 vtinfo
"sync data ..."
341 vtinfo
"esp partition processing ..."
342 if [ "$SECUREBOOT" != "YES" ]; then
344 check_umount_disk
"$DISK"
345 vtoycli partresize
-s $DISK $part2_start_sector
349 vtinfo
"Install Ventoy to $DISK successfully finished."
352 elif [ "$MODE" = "install" -a -n "$NONDESTRUCTIVE" ]; then
353 vtdebug
"non-destructive install Ventoy ..."
355 version
=$(get_disk_ventoy_version $DISK)
356 if [ $?
-eq 0 ]; then
357 if [ -z "$FORCE" ]; then
358 vtwarn
"$DISK already contains a Ventoy with version $version."
359 vtwarn
"You can not do and don not need non-destructive installation."
365 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
366 disk_size_gb
=$(expr $disk_sector_num / 2097152)
368 if vtoycli partresize
-t $DISK; then
376 parted
-s $DISK p
2>&1 | grep Model
377 echo "Size : $disk_size_gb GB"
378 echo "Style: $OldStyle"
382 vtwarn
"Ventoy will try non-destructive installation on $DISK if possible."
385 read -p 'Continue? (y/n) ' Answer
386 if [ "$Answer" != "y" ]; then
387 if [ "$Answer" != "Y" ]; then
392 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
393 vterr
"No enough space in disk $DISK"
397 PART1
=$(get_disk_part_name $DISK 1)
398 PART2
=$(get_disk_part_name $DISK 2)
400 #Part1 size in MB aligned with 4KB
401 PART1_SECTORS
=$(cat /sys/class/block/${PART1#/dev/}/size)
402 PART1_4K
=$(expr $PART1_SECTORS / 8)
403 PART1_MB
=$(expr $PART1_4K / 256)
404 PART1_NEW_MB
=$(expr $PART1_MB - 32)
406 echo "$PART1 is ${PART1_MB}MB"
408 #check partition layout
409 echo "check partition layout ..."
410 vtoycli partresize
-c $DISK
412 if [ $vtRet -eq 0 ]; then
416 check_umount_disk
"$DISK"
418 check_umount_disk
"$DISK"
420 if [ $vtRet -eq 1 ]; then
421 echo "Free space enough, start install..."
422 part2_start_sector
=$(expr $PART1_SECTORS + 2048)
423 elif [ $vtRet -eq 2 ]; then
424 echo "We need to shrink partition 1 firstly ..."
426 PART1_BLKID
=$(blkid $PART1)
429 if echo $PART1_BLKID | egrep -q -i 'TYPE=ntfs|TYPE=.ntfs'; then
430 echo "Partition 1 contains NTFS filesystem"
433 if [ $?
-ne 0 ]; then
434 echo "###[FAIL] ntfsresize not found. Please install ntfs-3g package."
438 echo "ntfsfix -b -d $PART1 ..."
441 echo "ntfsresize --size ${PART1_NEW_MB}Mi $PART1 ..."
442 ntfsresize
-f --size ${PART1_NEW_MB}Mi
$PART1
443 if [ $?
-ne 0 ]; then
444 echo "###[FAIL] ntfsresize failed."
447 elif echo $PART1_BLKID | egrep -q -i 'TYPE=ext[2-4]|TYPE=.ext[2-4]'; then
448 echo "Partition 1 contains EXT filesystem"
451 if [ $?
-ne 0 ]; then
452 echo "###[FAIL] resize2fs not found. Please install e2fsprogs package."
456 echo "e2fsck -f $PART1 ..."
459 echo "resize2fs $PART1 ${PART1_NEW_MB}M ..."
460 resize2fs
$PART1 ${PART1_NEW_MB}M
461 if [ $?
-ne 0 ]; then
462 echo "###[FAIL] resize2fs failed."
466 echo "###[FAIL] Unsupported filesystem in partition 1."
471 PART1_NEW_END_MB
=$(expr $PART1_NEW_MB + 1)
472 part2_start_sector
=$(expr $PART1_NEW_END_MB \* 2048)
476 vtinfo
"writing data to disk part2_start=$part2_start_sector ..."
478 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=440
480 if [ "$OldStyle" = "GPT" ]; then
481 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
482 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
483 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
485 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
488 xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start_sector
491 testUUIDStr
=$(vtoy_gen_uuid | hexdump -C)
492 vtdebug
"test uuid: $testUUIDStr"
495 vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} seek
=384 bs
=1 count
=16
497 vtinfo
"sync data ..."
500 vtinfo
"esp partition processing ..."
501 if [ "$SECUREBOOT" != "YES" ]; then
502 vtoycli partresize
-s $DISK $part2_start_sector
505 vtinfo
"update partition table $DISK $part2_start_sector ..."
506 vtoycli partresize
-p $DISK $part2_start_sector
507 if [ $?
-eq 0 ]; then
510 vtinfo
"Ventoy non-destructive installation on $DISK successfully finished."
514 vterr
"Ventoy non-destructive installation on $DISK failed."
519 vtdebug
"update Ventoy ..."
521 oldver
=$(get_disk_ventoy_version $DISK)
522 if [ $?
-ne 0 ]; then
523 if is_disk_contains_ventoy
$DISK; then
526 vtwarn
"$DISK does not contain Ventoy or data corrupted"
528 vtwarn
"Please use -i option if you want to install ventoy to $DISK"
534 #reserve secure boot option
535 if [ -z "$SECUREBOOT" ]; then
536 if check_disk_secure_boot
$DISK; then
543 curver
=$(cat ./ventoy/version)
545 vtinfo
"Upgrade operation is safe, all the data in the 1st partition (iso files and other) will be unchanged!"
548 read -p "Update Ventoy $oldver ===> $curver Continue? (y/n)" Answer
549 if [ "$Answer" != "y" ]; then
550 if [ "$Answer" != "Y" ]; then
555 PART2
=$(get_disk_part_name $DISK 2)
556 SHORT_PART2
=${PART2#/dev/}
557 part2_start
=$(cat /sys/class/block/$SHORT_PART2/start)
559 PART1_TYPE
=$(dd if=$DISK bs=1 count=1 skip=450 status=none | hexdump -n1 -e '1/1 "%02X"')
563 dd status
=none conv
=fsync
if=${DISK} skip
=384 bs
=1 count
=16 of
=.
/diskuuid.bin
565 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=440
566 dd status
=none conv
=fsync
if=.
/diskuuid.bin of
=$DISK bs
=1 count
=16 seek
=384
571 dd status
=none conv
=fsync
if=${DISK} skip
=2040 bs
=512 count
=8 of
=.
/rsvdata.bin
573 if [ "$PART1_TYPE" = "EE" ]; then
574 vtdebug
"This is GPT partition style ..."
575 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
576 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
577 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
579 vtdebug
"This is MBR partition style ..."
581 PART1_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=446 status=none | hexdump -n1 -e '1/1 "%02X"')
582 PART2_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=462 status=none | hexdump -n1 -e '1/1 "%02X"')
584 vtdebug
"PART1_ACTIVE=$PART1_ACTIVE PART2_ACTIVE=$PART2_ACTIVE"
585 if [ "$PART1_ACTIVE" = "00" ] && [ "$PART2_ACTIVE" = "80" ]; then
586 vtdebug
"change 1st partition active, 2nd partition inactive ..."
587 echo -en '\x80' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=446 status
=none
588 echo -en '\x00' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=462 status
=none
590 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
593 dd status
=none conv
=fsync
if=.
/rsvdata.bin seek
=2040 bs
=512 count
=8 of
=${DISK}
596 check_umount_disk
"$DISK"
598 xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start
601 vtinfo
"esp partition processing ..."
602 if [ "$SECUREBOOT" != "YES" ]; then
604 check_umount_disk
"$DISK"
605 vtoycli partresize
-s $DISK $part2_start
609 vtinfo
"Update Ventoy on $DISK successfully finished."