7 echo 'Usage: Ventoy2Disk.sh CMD [ OPTION ] /dev/sdX'
9 echo ' -i install ventoy to sdX (fail 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 enable 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)'
26 if [ "$1" = "-i" ]; then
28 elif [ "$1" = "-I" ]; then
31 elif [ "$1" = "-u" ]; then
33 elif [ "$1" = "-l" ]; then
35 elif [ "$1" = "-s" ]; then
37 elif [ "$1" = "-g" ]; then
39 elif [ "$1" = "-L" ]; then
42 elif [ "$1" = "-r" ]; then
46 elif [ "$1" = "-V" ] || [ "$1" = "--version" ]; then
48 elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
52 if ! [ -b "$1" ]; then
53 vterr
"$1 is NOT a valid device"
63 if [ -z "$MODE" ]; then
68 if ! [ -b "$DISK" ]; then
69 vterr
"Disk $DISK does not exist"
73 if [ -e /sys
/class
/block
/${DISK#/dev/}/start
]; then
74 vterr
"$DISK is a partition, please use the whole disk."
76 vterr
" sudo sh Ventoy2Disk.sh -i /dev/sdX1 <=== This is wrong"
77 vtinfo
" sudo sh Ventoy2Disk.sh -i /dev/sdX <=== This is right"
82 if [ -n "$RESERVE_SPACE" -a "$MODE" = "install" ]; then
83 if echo $RESERVE_SIZE_MB | grep -q '^[0-9][0-9]*$'; then
84 vtdebug
"User will reserve $RESERVE_SIZE_MB MB disk space"
86 vterr
"$RESERVE_SIZE_MB is invalid for reserved space"
92 if dd if="$DISK" of
=/dev
/null bs
=1 count
=1 >/dev
/null
2>&1; then
93 vtdebug
"root permission check ok ..."
95 vterr
"Failed to access $DISK, maybe root privilege is needed!"
100 vtdebug
"MODE=$MODE FORCE=$FORCE RESERVE_SPACE=$RESERVE_SPACE RESERVE_SIZE_MB=$RESERVE_SIZE_MB"
103 if check_tool_work_ok
; then
104 vtdebug
"check tool work ok"
106 vterr
"Some tools can not run in current system. Please check log.txt for detail."
110 if [ "$MODE" = "list" ]; then
111 version
=$(get_disk_ventoy_version $DISK)
112 if [ $?
-eq 0 ]; then
113 echo "Ventoy Version in Disk: $version"
115 vtPart1Type
=$(dd if=$DISK bs=1 count=1 skip=450 status=none | hexdump -n1 -e '1/1 "%02X"')
116 if [ "$vtPart1Type" = "EE" ]; then
117 echo "Disk Partition Style : GPT"
119 echo "Disk Partition Style : MBR"
122 vtPART2
=$(get_disk_part_name $DISK 2)
123 rm -rf .
/tmpmntp2
&& mkdir .
/tmpmntp2
124 mount
$vtPART2 .
/tmpmntp2
> /dev
/null
2>&1
126 if [ -e .
/tmpmntp
2/EFI
/BOOT
/MokManager.efi
]; then
127 echo "Secure Boot Support : YES"
129 echo "Secure Boot Support : NO"
131 umount .
/tmpmntp2
> /dev
/null
2>&1
134 echo "Ventoy Version: NA"
141 grep "^$DISK" /proc
/mounts
| while read mtline
; do
142 mtpnt
=$(echo $mtline | awk '{print $2}')
143 vtdebug
"Trying to umount $mtpnt ..."
144 umount
$mtpnt >/dev
/null
2>&1
147 if grep "$DISK" /proc
/mounts
; then
148 vterr
"$DISK is already mounted, please umount it first!"
152 #check swap partition
153 if swapon
--help 2>&1 | grep -q '^ \-s,'; then
154 if swapon
-s | grep -q "^${DISK}[0-9]"; then
155 vterr
"$DISK is used as swap, please swapoff it first!"
161 if [ "$MODE" = "install" ]; then
162 vtdebug
"install ventoy ..."
164 if [ -n "$VTGPT" ]; then
165 if parted
-v > /dev
/null
2>&1; then
168 vterr
"parted is not found in the system, Ventoy can't create new partitions without it."
169 vterr
"You should install \"GNU parted\" first."
173 if parted
-v > /dev
/null
2>&1; then
175 elif fdisk
-v >/dev
/null
2>&1; then
178 vterr
"Both parted and fdisk are not found in the system, Ventoy can't create new partitions."
183 version
=$(get_disk_ventoy_version $DISK)
184 if [ $?
-eq 0 ]; then
185 if [ -z "$FORCE" ]; then
186 vtwarn
"$DISK already contains a Ventoy with version $version"
187 vtwarn
"Use -u option to do a safe upgrade operation."
188 vtwarn
"OR if you really want to reinstall ventoy to $DISK, please use -I option."
194 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
195 disk_size_gb
=$(expr $disk_sector_num / 2097152)
197 if [ $disk_sector_num -gt 4294967296 ] && [ -z "$VTGPT" ]; then
198 vterr
"$DISK is over 2TB size, MBR will not work on it."
202 if [ -n "$RESERVE_SPACE" ]; then
203 sum_size_mb
=$(expr $RESERVE_SIZE_MB + $VENTOY_PART_SIZE_MB)
204 reserve_sector_num
=$(expr $sum_size_mb \* 2048)
206 if [ $disk_sector_num -le $reserve_sector_num ]; then
207 vterr
"Can't reserve $RESERVE_SIZE_MB MB space from $DISK"
214 parted
-s $DISK p
2>&1 | grep Model
215 echo "Size : $disk_size_gb GB"
216 if [ -n "$VTGPT" ]; then
223 if [ -n "$RESERVE_SPACE" ]; then
224 echo "You will reserve $RESERVE_SIZE_MB MB disk space "
229 vtwarn
"You will install Ventoy to $DISK."
230 vtwarn
"All the data on the disk $DISK will be lost!!!"
233 read -p 'Continue? (y/n) ' Answer
234 if [ "$Answer" != "y" ]; then
235 if [ "$Answer" != "Y" ]; then
241 vtwarn
"All the data on the disk $DISK will be lost!!!"
242 read -p 'Double-check. Continue? (y/n) ' Answer
243 if [ "$Answer" != "y" ]; then
244 if [ "$Answer" != "Y" ]; then
249 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
250 vterr
"No enough space in disk $DISK"
254 if ! dd if=/dev
/zero of
=$DISK bs
=1 count
=512 status
=none conv
=fsync
; then
255 vterr
"Write data to $DISK failed, please check whether it's in use."
259 if [ -n "$VTGPT" ]; then
260 vtdebug
"format_ventoy_disk_gpt $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
261 format_ventoy_disk_gpt
$RESERVE_SIZE_MB $DISK $PARTTOOL
263 vtdebug
"format_ventoy_disk_mbr $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
264 format_ventoy_disk_mbr
$RESERVE_SIZE_MB $DISK $PARTTOOL
268 if ventoy_is_linux64
; then
269 cmd
=.
/tool
/mkexfatfs_64
271 cmd
=.
/tool
/mkexfatfs_32
274 if [ -d .
/tool
/ ]; then
278 # DiskSize > 32GB Cluster Size use 128KB
279 # DiskSize < 32GB Cluster Size use 32KB
280 if [ $disk_size_gb -gt 32 ]; then
286 PART1
=$(get_disk_part_name $DISK 1)
287 PART2
=$(get_disk_part_name $DISK 2)
289 $cmd -n "$VTNEW_LABEL" -s $cluster_sectors ${PART1}
291 vtinfo
"writing data to disk ..."
293 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=446
295 if [ -n "$VTGPT" ]; then
296 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
297 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
298 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
300 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
303 xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start_sector
306 .
/tool
/vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} seek
=384 bs
=1 count
=16
309 .
/tool
/vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} skip
=12 seek
=440 bs
=1 count
=4
311 vtinfo
"sync data ..."
314 vtinfo
"esp partition processing ..."
317 mtpnt
=$(grep "^${PART2}" /proc/mounts | awk '{print $2}')
318 if [ -n "$mtpnt" ]; then
319 umount
$mtpnt >/dev
/null
2>&1
322 if [ "$SECUREBOOT" != "YES" ]; then
325 vtdebug
"mounting part2 ...."
327 if mount
${PART2} .
/tmp_mnt
; then
328 vtdebug
"mounting part2 success"
332 mtpnt
=$(grep "^${PART2}" /proc/mounts | awk '{print $2}')
333 if [ -n "$mtpnt" ]; then
334 umount
$mtpnt >/dev
/null
2>&1
339 rm -f .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
340 rm -f .
/tmp_mnt
/EFI
/BOOT
/grubx64.efi
341 rm -f .
/tmp_mnt
/EFI
/BOOT
/MokManager.efi
342 rm -f .
/tmp_mnt
/ENROLL_THIS_KEY_IN_MOKMANAGER.cer
343 mv .
/tmp_mnt
/EFI
/BOOT
/grubx64_real.efi .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
350 vtinfo
"Install Ventoy to $DISK successfully finished."
354 vtdebug
"update ventoy ..."
356 oldver
=$(get_disk_ventoy_version $DISK)
357 if [ $?
-ne 0 ]; then
358 vtwarn
"$DISK does not contain ventoy or data corupted"
360 vtwarn
"Please use -i option if you want to install ventoy to $DISK"
365 curver
=$(cat ./ventoy/version)
367 vtinfo
"Upgrade operation is safe, all the data in the 1st partition (iso files and other) will be unchanged!"
370 read -p "Update Ventoy $oldver ===> $curver Continue? (y/n)" Answer
371 if [ "$Answer" != "y" ]; then
372 if [ "$Answer" != "Y" ]; then
377 PART2
=$(get_disk_part_name $DISK 2)
378 SHORT_PART2
=${PART2#/dev/}
379 part2_start
=$(cat /sys/class/block/$SHORT_PART2/start)
381 PART1_TYPE
=$(dd if=$DISK bs=1 count=1 skip=450 status=none | hexdump -n1 -e '1/1 "%02X"')
383 if [ "$PART1_TYPE" = "EE" ]; then
384 vtdebug
"This is GPT partition style ..."
385 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
386 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
388 vtdebug
"This is MBR partition style ..."
389 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=440
391 PART1_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=446 status=none | hexdump -n1 -e '1/1 "%02X"')
392 PART2_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=462 status=none | hexdump -n1 -e '1/1 "%02X"')
394 vtdebug
"PART1_ACTIVE=$PART1_ACTIVE PART2_ACTIVE=$PART2_ACTIVE"
395 if [ "$PART1_ACTIVE" = "00" ] && [ "$PART2_ACTIVE" = "80" ]; then
396 vtdebug
"change 1st partition active, 2nd partition inactive ..."
397 echo -en '\x80' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=446 status
=none
398 echo -en '\x00' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=462 status
=none
400 xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
403 xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start
407 if [ "$SECUREBOOT" != "YES" ]; then
410 vtdebug
"mounting part2 ...."
412 if mount
${PART2} .
/tmp_mnt
; then
413 vtdebug
"mounting part2 success"
419 rm -f .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
420 rm -f .
/tmp_mnt
/EFI
/BOOT
/grubx64.efi
421 rm -f .
/tmp_mnt
/EFI
/BOOT
/MokManager.efi
422 rm -f .
/tmp_mnt
/ENROLL_THIS_KEY_IN_MOKMANAGER.cer
423 mv .
/tmp_mnt
/EFI
/BOOT
/grubx64_real.efi .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
430 vtinfo
"Update Ventoy to $DISK successfully finished."