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 ' -u update ventoy in sdX'
10 echo ' -I force install ventoy to sdX (no matter installed or not)'
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)'
22 if [ "$1" = "-i" ]; then
24 elif [ "$1" = "-I" ]; then
27 elif [ "$1" = "-u" ]; then
29 elif [ "$1" = "-s" ]; then
31 elif [ "$1" = "-g" ]; then
33 elif [ "$1" = "-r" ]; then
38 if ! [ -b "$1" ]; then
39 vterr
"$1 is NOT a valid device"
49 if [ -z "$MODE" ]; then
54 if ! [ -b "$DISK" ]; then
55 vterr
"Disk $DISK does not exist"
59 if [ -e /sys
/class
/block
/${DISK#/dev/}/start
]; then
60 vterr
"$DISK is a partition, please use the whole disk"
64 if [ -n "$RESERVE_SPACE" ]; then
65 if echo $RESERVE_SIZE_MB | grep -q '^[0-9][0-9]*$'; then
66 vtdebug
"User will reserve $RESERVE_SIZE_MB MB disk space"
68 vterr
"$RESERVE_SIZE_MB is invalid for reserved space"
73 if dd if="$DISK" of
=/dev
/null bs
=1 count
=1 >/dev
/null
2>&1; then
74 vtdebug
"root permission check ok ..."
76 vterr
"Failed to access $DISK, maybe root privilege is needed!"
81 vtdebug
"MODE=$MODE FORCE=$FORCE RESERVE_SPACE=$RESERVE_SPACE RESERVE_SIZE_MB=$RESERVE_SIZE_MB"
83 if ! check_tool_work_ok
; then
84 vterr
"Some tools can not run in current system. Please check log.txt for detail."
88 grep "^$DISK" /proc
/mounts
| while read mtline
; do
89 mtpnt
=$(echo $mtline | awk '{print $2}')
90 vtdebug
"Trying to umount $mtpnt ..."
91 umount
$mtpnt >/dev
/null
2>&1
94 if swapon
-s | grep -q "^${DISK}[0-9]"; then
95 swapon
-s | grep "^${DISK}[0-9]" | awk '{print $1}' | while read line
; do
96 vtdebug
"Trying to swapoff $line ..."
102 if grep "$DISK" /proc
/mounts
; then
103 vterr
"$DISK is already mounted, please umount it first!"
107 if swapon
-s | grep -q "^${DISK}[0-9]"; then
108 vterr
"$DISK is used as swap, please swapoff it first!"
113 if [ "$MODE" = "install" ]; then
114 vtdebug
"install ventoy ..."
116 if [ -n "$VTGPT" ]; then
117 if parted
-v > /dev
/null
2>&1; then
120 vterr
"parted is not found in the sysstem, Ventoy can't create new partition."
124 if parted
-v > /dev
/null
2>&1; then
126 elif fdisk
-v >/dev
/null
2>&1; then
129 vterr
"Both parted and fdisk are not found in the sysstem, Ventoy can't create new partition."
134 version
=$(get_disk_ventoy_version $DISK)
135 if [ $?
-eq 0 ]; then
136 if [ -z "$FORCE" ]; then
137 vtwarn
"$DISK already contains a Ventoy with version $version"
138 vtwarn
"Use -u option to do a safe upgrade operation."
139 vtwarn
"OR if you really want to reinstall ventoy to $DISK, please use -I option."
145 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
146 disk_size_gb
=$(expr $disk_sector_num / 2097152)
148 if [ $disk_sector_num -gt 4294967296 ]; then
149 vterr
"$DISK is over 2TB size, MBR will not work on it."
153 if [ -n "$RESERVE_SPACE" ]; then
154 sum_size_mb
=$(expr $RESERVE_SIZE_MB + $VENTOY_PART_SIZE_MB)
155 reserve_sector_num
=$(expr $sum_size_mb \* 2048)
157 if [ $disk_sector_num -le $reserve_sector_num ]; then
158 vterr
"Can't reserve $RESERVE_SIZE_MB MB space from $DISK"
165 parted
-s $DISK p
2>&1 | grep Model
166 echo "Size : $disk_size_gb GB"
167 if [ -n "$VTGPT" ]; then
174 if [ -n "$RESERVE_SPACE" ]; then
175 echo "You will reserve $RESERVE_SIZE_MB MB disk space "
180 vtwarn
"You will install Ventoy to $DISK."
181 vtwarn
"All the data on the disk $DISK will be lost!!!"
184 read -p 'Continue? (y/n)' Answer
185 if [ "$Answer" != "y" ]; then
186 if [ "$Answer" != "Y" ]; then
192 vtwarn
"All the data on the disk $DISK will be lost!!!"
193 read -p 'Double-check. Continue? (y/n)' Answer
194 if [ "$Answer" != "y" ]; then
195 if [ "$Answer" != "Y" ]; then
201 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
202 vterr
"No enough space in disk $DISK"
206 if ! dd if=/dev
/zero of
=$DISK bs
=1 count
=512 status
=none conv
=fsync
; then
207 vterr
"Write data to $DISK failed, please check whether it's in use."
211 if [ -n "$VTGPT" ]; then
212 vtdebug
"format_ventoy_disk_gpt $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
213 format_ventoy_disk_gpt
$RESERVE_SIZE_MB $DISK $PARTTOOL
215 vtdebug
"format_ventoy_disk_mbr $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
216 format_ventoy_disk_mbr
$RESERVE_SIZE_MB $DISK $PARTTOOL
220 if ventoy_is_linux64
; then
221 cmd
=.
/tool
/mkexfatfs_64
223 cmd
=.
/tool
/mkexfatfs_32
228 # DiskSize > 32GB Cluster Size use 128KB
229 # DiskSize < 32GB Cluster Size use 32KB
230 if [ $disk_size_gb -gt 32 ]; then
236 $cmd -n ventoy
-s $cluster_sectors ${DISK}1
238 chmod +x .
/tool
/vtoy_gen_uuid
240 vtinfo
"writing data to disk ..."
242 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=446
244 if [ -n "$VTGPT" ]; then
245 echo -en '\x22' | dd status
=none of
=$DISK conv
=fsync bs
=1 count
=1 seek
=92
246 .
/tool
/xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
247 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
249 .
/tool
/xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
252 .
/tool
/xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start_sector
255 .
/tool
/vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} seek
=384 bs
=1 count
=16
258 .
/tool
/vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} skip
=12 seek
=440 bs
=1 count
=4
260 vtinfo
"sync data ..."
263 vtinfo
"esp partition processing ..."
266 mtpnt
=$(grep "^${DISK}2" /proc/mounts | awk '{print $2}')
267 if [ -n "$mtpnt" ]; then
268 umount
$mtpnt >/dev
/null
2>&1
271 if [ "$SECUREBOOT" != "YES" ]; then
274 vtdebug
"mounting part2 ...."
276 if mount
${DISK}2 .
/tmp_mnt
; then
277 vtdebug
"mounting part2 success"
281 mtpnt
=$(grep "^${DISK}2" /proc/mounts | awk '{print $2}')
282 if [ -n "$mtpnt" ]; then
283 umount
$mtpnt >/dev
/null
2>&1
288 rm -f .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
289 rm -f .
/tmp_mnt
/EFI
/BOOT
/grubx64.efi
290 rm -f .
/tmp_mnt
/EFI
/BOOT
/MokManager.efi
291 rm -f .
/tmp_mnt
/ENROLL_THIS_KEY_IN_MOKMANAGER.cer
292 mv .
/tmp_mnt
/EFI
/BOOT
/grubx64_real.efi .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
299 vtinfo
"Install Ventoy to $DISK successfully finished."
303 vtdebug
"update ventoy ..."
305 oldver
=$(get_disk_ventoy_version $DISK)
306 if [ $?
-ne 0 ]; then
307 vtwarn
"$DISK does not contain ventoy or data corupted"
309 vtwarn
"Please use -i option if you want to install ventoy to $DISK"
314 curver
=$(cat ./ventoy/version)
316 vtinfo
"Upgrade operation is safe, all the data in the 1st partition (iso files and other) will be unchanged!"
319 read -p "Update Ventoy $oldver ===> $curver Continue? (y/n)" Answer
320 if [ "$Answer" != "y" ]; then
321 if [ "$Answer" != "Y" ]; then
326 PART2
=$(get_disk_part_name $DISK 2)
327 SHORT_PART2
=${PART2#/dev/}
328 part2_start
=$(cat /sys/class/block/$SHORT_PART2/start)
330 PART1_TYPE
=$(dd if=$DISK bs=1 count=1 skip=450 status=none | ./tool/hexdump -n1 -e '1/1 "%02X"')
332 if [ "$PART1_TYPE" = "EE" ]; then
333 vtdebug
"This is GPT partition style ..."
334 .
/tool
/xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2014 seek
=34
335 echo -en '\x23' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=17908 status
=none
337 vtdebug
"This is MBR partition style ..."
338 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=440
340 PART1_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=446 status=none | ./tool/hexdump -n1 -e '1/1 "%02X"')
341 PART2_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=462 status=none | ./tool/hexdump -n1 -e '1/1 "%02X"')
343 vtdebug
"PART1_ACTIVE=$PART1_ACTIVE PART2_ACTIVE=$PART2_ACTIVE"
344 if [ "$PART1_ACTIVE" = "00" ] && [ "$PART2_ACTIVE" = "80" ]; then
345 vtdebug
"change 1st partition active, 2nd partition inactive ..."
346 echo -en '\x80' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=446 status
=none
347 echo -en '\x00' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=462 status
=none
349 .
/tool
/xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
352 .
/tool
/xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start
356 if [ "$SECUREBOOT" != "YES" ]; then
359 vtdebug
"mounting part2 ...."
361 if mount
${DISK}2 .
/tmp_mnt
; then
362 vtdebug
"mounting part2 success"
368 rm -f .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
369 rm -f .
/tmp_mnt
/EFI
/BOOT
/grubx64.efi
370 rm -f .
/tmp_mnt
/EFI
/BOOT
/MokManager.efi
371 rm -f .
/tmp_mnt
/ENROLL_THIS_KEY_IN_MOKMANAGER.cer
372 mv .
/tmp_mnt
/EFI
/BOOT
/grubx64_real.efi .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
379 vtinfo
"Update Ventoy to $DISK successfully finished."