6 echo 'Usage: Ventoy2Disk.sh CMD [ OPTION ] /dev/sdX'
8 echo ' -i install ventoy to sdX (fail if disk already installed with ventoy)'
9 echo ' -I force install ventoy to sdX (no matter installed or not)'
10 echo ' -u update ventoy in sdX'
12 echo ' OPTION: (optional)'
13 echo ' -r SIZE_MB preserve some space at the bottom of the disk (only for install)'
14 echo ' -s enable secure boot support (default is disabled)'
15 echo ' -g use GPT partition style, default is MBR (only for install)'
21 if [ "$1" = "-i" ]; then
23 elif [ "$1" = "-I" ]; then
26 elif [ "$1" = "-u" ]; then
28 elif [ "$1" = "-s" ]; then
30 elif [ "$1" = "-g" ]; then
32 elif [ "$1" = "-r" ]; then
37 if ! [ -b "$1" ]; then
38 vterr
"$1 is NOT a valid device"
48 if [ -z "$MODE" ]; then
53 if ! [ -b "$DISK" ]; then
54 vterr
"Disk $DISK does not exist"
58 if [ -e /sys
/class
/block
/${DISK#/dev/}/start
]; then
59 vterr
"$DISK is a partition, please use the whole disk"
63 if [ -n "$RESERVE_SPACE" ]; then
64 if echo $RESERVE_SIZE_MB | grep -q '^[0-9][0-9]*$'; then
65 vtdebug
"User will reserve $RESERVE_SIZE_MB MB disk space"
67 vterr
"$RESERVE_SIZE_MB is invalid for reserved space"
72 if dd if="$DISK" of
=/dev
/null bs
=1 count
=1 >/dev
/null
2>&1; then
73 vtdebug
"root permission check ok ..."
75 vterr
"Failed to access $DISK, maybe root privilege is needed!"
80 vtdebug
"MODE=$MODE FORCE=$FORCE RESERVE_SPACE=$RESERVE_SPACE RESERVE_SIZE_MB=$RESERVE_SIZE_MB"
82 if ! check_tool_work_ok
; then
83 vterr
"Some tools can not run in current system. Please check log.txt for detail."
87 grep "^$DISK" /proc
/mounts
| while read mtline
; do
88 mtpnt
=$(echo $mtline | awk '{print $2}')
89 vtdebug
"Trying to umount $mtpnt ..."
90 umount
$mtpnt >/dev
/null
2>&1
93 if swapon
-s | grep -q "^${DISK}[0-9]"; then
94 swapon
-s | grep "^${DISK}[0-9]" | awk '{print $1}' | while read line
; do
95 vtdebug
"Trying to swapoff $line ..."
101 if grep "$DISK" /proc
/mounts
; then
102 vterr
"$DISK is already mounted, please umount it first!"
106 if swapon
-s | grep -q "^${DISK}[0-9]"; then
107 vterr
"$DISK is used as swap, please swapoff it first!"
112 if [ "$MODE" = "install" ]; then
113 vtdebug
"install ventoy ..."
115 if [ -n "$VTGPT" ]; then
116 if parted
-v > /dev
/null
2>&1; then
119 vterr
"parted is not found in the sysstem, Ventoy can't create new partition."
123 if parted
-v > /dev
/null
2>&1; then
125 elif fdisk
-v >/dev
/null
2>&1; then
128 vterr
"Both parted and fdisk are not found in the sysstem, Ventoy can't create new partition."
133 version
=$(get_disk_ventoy_version $DISK)
134 if [ $?
-eq 0 ]; then
135 if [ -z "$FORCE" ]; then
136 vtwarn
"$DISK already contains a Ventoy with version $version"
137 vtwarn
"Use -u option to do a safe upgrade operation."
138 vtwarn
"OR if you really want to reinstall ventoy to $DISK, please use -I option."
144 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
145 disk_size_gb
=$(expr $disk_sector_num / 2097152)
147 if [ $disk_sector_num -gt 4294967296 ]; then
148 vterr
"$DISK is over 2TB size, MBR will not work on it."
152 if [ -n "$RESERVE_SPACE" ]; then
153 sum_size_mb
=$(expr $RESERVE_SIZE_MB + $VENTOY_PART_SIZE_MB)
154 reserve_sector_num
=$(expr $sum_size_mb \* 2048)
156 if [ $disk_sector_num -le $reserve_sector_num ]; then
157 vterr
"Can't reserve $RESERVE_SIZE_MB MB space from $DISK"
164 parted
-s $DISK p
2>&1 | grep Model
165 echo "Size : $disk_size_gb GB"
166 if [ -n "$VTGPT" ]; then
173 if [ -n "$RESERVE_SPACE" ]; then
174 echo "You will reserve $RESERVE_SIZE_MB MB disk space "
179 vtwarn
"You will install Ventoy to $DISK."
180 vtwarn
"All the data on the disk $DISK will be lost!!!"
183 read -p 'Continue? (y/n)' Answer
184 if [ "$Answer" != "y" ]; then
185 if [ "$Answer" != "Y" ]; then
191 vtwarn
"All the data on the disk $DISK will be lost!!!"
192 read -p 'Double-check. Continue? (y/n)' Answer
193 if [ "$Answer" != "y" ]; then
194 if [ "$Answer" != "Y" ]; then
200 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
201 vterr
"No enough space in disk $DISK"
205 if ! dd if=/dev
/zero of
=$DISK bs
=1 count
=512 status
=none conv
=fsync
; then
206 vterr
"Write data to $DISK failed, please check whether it's in use."
210 if [ -n "$VTGPT" ]; then
211 vtdebug
"format_ventoy_disk_gpt $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
212 format_ventoy_disk_gpt
$RESERVE_SIZE_MB $DISK $PARTTOOL
214 vtdebug
"format_ventoy_disk_mbr $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
215 format_ventoy_disk_mbr
$RESERVE_SIZE_MB $DISK $PARTTOOL
219 if ventoy_is_linux64
; then
220 cmd
=.
/tool
/mkexfatfs_64
222 cmd
=.
/tool
/mkexfatfs_32
225 if [ -d .
/tool
/ ]; then
229 # DiskSize > 32GB Cluster Size use 128KB
230 # DiskSize < 32GB Cluster Size use 32KB
231 if [ $disk_size_gb -gt 32 ]; then
237 $cmd -n ventoy
-s $cluster_sectors ${DISK}1
239 vtinfo
"writing data to disk ..."
241 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=446
243 if [ -n "$VTGPT" ]; then
244 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
245 .
/tool
/xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
246 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
248 .
/tool
/xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
251 .
/tool
/xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start_sector
254 .
/tool
/vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} seek
=384 bs
=1 count
=16
257 .
/tool
/vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} skip
=12 seek
=440 bs
=1 count
=4
259 vtinfo
"sync data ..."
262 vtinfo
"esp partition processing ..."
265 mtpnt
=$(grep "^${DISK}2" /proc/mounts | awk '{print $2}')
266 if [ -n "$mtpnt" ]; then
267 umount
$mtpnt >/dev
/null
2>&1
270 if [ "$SECUREBOOT" != "YES" ]; then
273 vtdebug
"mounting part2 ...."
275 if mount
${DISK}2 .
/tmp_mnt
; then
276 vtdebug
"mounting part2 success"
280 mtpnt
=$(grep "^${DISK}2" /proc/mounts | awk '{print $2}')
281 if [ -n "$mtpnt" ]; then
282 umount
$mtpnt >/dev
/null
2>&1
287 rm -f .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
288 rm -f .
/tmp_mnt
/EFI
/BOOT
/grubx64.efi
289 rm -f .
/tmp_mnt
/EFI
/BOOT
/MokManager.efi
290 rm -f .
/tmp_mnt
/ENROLL_THIS_KEY_IN_MOKMANAGER.cer
291 mv .
/tmp_mnt
/EFI
/BOOT
/grubx64_real.efi .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
298 vtinfo
"Install Ventoy to $DISK successfully finished."
302 vtdebug
"update ventoy ..."
304 oldver
=$(get_disk_ventoy_version $DISK)
305 if [ $?
-ne 0 ]; then
306 vtwarn
"$DISK does not contain ventoy or data corupted"
308 vtwarn
"Please use -i option if you want to install ventoy to $DISK"
313 curver
=$(cat ./ventoy/version)
315 vtinfo
"Upgrade operation is safe, all the data in the 1st partition (iso files and other) will be unchanged!"
318 read -p "Update Ventoy $oldver ===> $curver Continue? (y/n)" Answer
319 if [ "$Answer" != "y" ]; then
320 if [ "$Answer" != "Y" ]; then
325 PART2
=$(get_disk_part_name $DISK 2)
326 SHORT_PART2
=${PART2#/dev/}
327 part2_start
=$(cat /sys/class/block/$SHORT_PART2/start)
329 PART1_TYPE
=$(dd if=$DISK bs=1 count=1 skip=450 status=none | ./tool/hexdump -n1 -e '1/1 "%02X"')
331 if [ "$PART1_TYPE" = "EE" ]; then
332 vtdebug
"This is GPT partition style ..."
333 .
/tool
/xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
334 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
336 vtdebug
"This is MBR partition style ..."
337 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=440
339 PART1_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=446 status=none | ./tool/hexdump -n1 -e '1/1 "%02X"')
340 PART2_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=462 status=none | ./tool/hexdump -n1 -e '1/1 "%02X"')
342 vtdebug
"PART1_ACTIVE=$PART1_ACTIVE PART2_ACTIVE=$PART2_ACTIVE"
343 if [ "$PART1_ACTIVE" = "00" ] && [ "$PART2_ACTIVE" = "80" ]; then
344 vtdebug
"change 1st partition active, 2nd partition inactive ..."
345 echo -en '\x80' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=446 status
=none
346 echo -en '\x00' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=462 status
=none
348 .
/tool
/xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
351 .
/tool
/xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start
355 if [ "$SECUREBOOT" != "YES" ]; then
358 vtdebug
"mounting part2 ...."
360 if mount
${DISK}2 .
/tmp_mnt
; then
361 vtdebug
"mounting part2 success"
367 rm -f .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
368 rm -f .
/tmp_mnt
/EFI
/BOOT
/grubx64.efi
369 rm -f .
/tmp_mnt
/EFI
/BOOT
/MokManager.efi
370 rm -f .
/tmp_mnt
/ENROLL_THIS_KEY_IN_MOKMANAGER.cer
371 mv .
/tmp_mnt
/EFI
/BOOT
/grubx64_real.efi .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
378 vtinfo
"Update Ventoy to $DISK successfully finished."