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)'
21 if [ "$1" = "-i" ]; then
23 elif [ "$1" = "-I" ]; then
26 elif [ "$1" = "-u" ]; then
28 elif [ "$1" = "-s" ]; then
30 elif [ "$1" = "-r" ]; then
35 if ! [ -b "$1" ]; then
36 vterr
"$1 is NOT a valid device"
46 if [ -z "$MODE" ]; then
51 if ! [ -b "$DISK" ]; then
52 vterr
"Disk $DISK does not exist"
56 if [ -e /sys
/class
/block
/${DISK#/dev/}/start
]; then
57 vterr
"$DISK is a partition, please use the whole disk"
61 if [ -n "$RESERVE_SPACE" ]; then
62 if echo $RESERVE_SIZE_MB | grep -q '^[0-9][0-9]*$'; then
63 vtdebug
"User will reserve $RESERVE_SIZE_MB MB disk space"
65 vterr
"$RESERVE_SIZE_MB is invalid for reserved space"
70 if dd if="$DISK" of
=/dev
/null bs
=1 count
=1 >/dev
/null
2>&1; then
71 vtdebug
"root permission check ok ..."
73 vterr
"Failed to access $DISK, maybe root privilege is needed!"
78 vtdebug
"MODE=$MODE FORCE=$FORCE RESERVE_SPACE=$RESERVE_SPACE RESERVE_SIZE_MB=$RESERVE_SIZE_MB"
80 if ! check_tool_work_ok
; then
81 vterr
"Some tools can not run in current system. Please check log.txt for detail."
85 grep "^$DISK" /proc
/mounts
| while read mtline
; do
86 mtpnt
=$(echo $mtline | awk '{print $2}')
87 vtdebug
"Trying to umount $mtpnt ..."
88 umount
$mtpnt >/dev
/null
2>&1
91 if swapon
-s | grep -q "^${DISK}[0-9]"; then
92 swapon
-s | grep "^${DISK}[0-9]" | awk '{print $1}' | while read line
; do
93 vtdebug
"Trying to swapoff $line ..."
99 if grep "$DISK" /proc
/mounts
; then
100 vterr
"$DISK is already mounted, please umount it first!"
104 if swapon
-s | grep -q "^${DISK}[0-9]"; then
105 vterr
"$DISK is used as swap, please swapoff it first!"
110 if [ "$MODE" = "install" ]; then
111 vtdebug
"install ventoy ..."
113 if parted
-v > /dev
/null
2>&1; then
115 elif fdisk
-v >/dev
/null
2>&1; then
118 vterr
"Both parted and fdisk are not found in the sysstem, Ventoy can't create new partition."
122 version
=$(get_disk_ventoy_version $DISK)
123 if [ $?
-eq 0 ]; then
124 if [ -z "$FORCE" ]; then
125 vtwarn
"$DISK already contains a Ventoy with version $version"
126 vtwarn
"Use -u option to do a safe upgrade operation."
127 vtwarn
"OR if you really want to reinstall ventoy to $DISK, please use -I option."
133 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
134 disk_size_gb
=$(expr $disk_sector_num / 2097152)
136 if [ $disk_sector_num -gt 4294967296 ]; then
137 vterr
"$DISK is over 2TB size, MBR will not work on it."
141 if [ -n "$RESERVE_SPACE" ]; then
142 sum_size_mb
=$(expr $RESERVE_SIZE_MB + $VENTOY_PART_SIZE_MB)
143 reserve_sector_num
=$(expr $sum_size_mb \* 2048)
145 if [ $disk_sector_num -le $reserve_sector_num ]; then
146 vterr
"Can't reserve $RESERVE_SIZE_MB MB space from $DISK"
154 parted
-s $DISK p
2>&1 | grep Model
155 echo "Size : $disk_size_gb GB"
158 if [ -n "$RESERVE_SPACE" ]; then
159 echo "You will reserve $RESERVE_SIZE_MB MB disk space "
164 vtwarn
"You will install Ventoy to $DISK."
165 vtwarn
"All the data on the disk $DISK will be lost!!!"
168 read -p 'Continue? (y/n)' Answer
169 if [ "$Answer" != "y" ]; then
170 if [ "$Answer" != "Y" ]; then
176 vtwarn
"All the data on the disk $DISK will be lost!!!"
177 read -p 'Double-check. Continue? (y/n)' Answer
178 if [ "$Answer" != "y" ]; then
179 if [ "$Answer" != "Y" ]; then
185 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
186 vterr
"No enough space in disk $DISK"
190 if ! dd if=/dev
/zero of
=$DISK bs
=1 count
=512 status
=none conv
=fsync
; then
191 vterr
"Write data to $DISK failed, please check whether it's in use."
195 format_ventoy_disk
$RESERVE_SIZE_MB $DISK $PARTTOOL
198 if ventoy_is_linux64
; then
199 cmd
=.
/tool
/mkexfatfs_64
201 cmd
=.
/tool
/mkexfatfs_32
206 # DiskSize > 32GB Cluster Size use 128KB
207 # DiskSize < 32GB Cluster Size use 32KB
208 if [ $disk_size_gb -gt 32 ]; then
214 $cmd -n ventoy
-s $cluster_sectors ${DISK}1
216 chmod +x .
/tool
/vtoy_gen_uuid
218 vtinfo
"writing data to disk ..."
219 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=446
220 .
/tool
/xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
221 .
/tool
/xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start_sector
224 .
/tool
/vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} seek
=384 bs
=1 count
=16
227 .
/tool
/vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} skip
=12 seek
=440 bs
=1 count
=4
229 vtinfo
"sync data ..."
232 vtinfo
"esp partition processing ..."
235 mtpnt
=$(grep "^${DISK}2" /proc/mounts | awk '{print $2}')
236 if [ -n "$mtpnt" ]; then
237 umount
$mtpnt >/dev
/null
2>&1
240 if [ "$SECUREBOOT" != "YES" ]; then
243 vtdebug
"mounting part2 ...."
245 if mount
${DISK}2 .
/tmp_mnt
; then
246 vtdebug
"mounting part2 success"
250 mtpnt
=$(grep "^${DISK}2" /proc/mounts | awk '{print $2}')
251 if [ -n "$mtpnt" ]; then
252 umount
$mtpnt >/dev
/null
2>&1
257 rm -f .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
258 rm -f .
/tmp_mnt
/EFI
/BOOT
/grubx64.efi
259 rm -f .
/tmp_mnt
/EFI
/BOOT
/MokManager.efi
260 rm -f .
/tmp_mnt
/ENROLL_THIS_KEY_IN_MOKMANAGER.cer
261 mv .
/tmp_mnt
/EFI
/BOOT
/grubx64_real.efi .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
268 vtinfo
"Install Ventoy to $DISK successfully finished."
272 vtdebug
"update ventoy ..."
274 oldver
=$(get_disk_ventoy_version $DISK)
275 if [ $?
-ne 0 ]; then
276 vtwarn
"$DISK does not contain ventoy or data corupted"
278 vtwarn
"Please use -i option if you want to install ventoy to $DISK"
283 curver
=$(cat ./ventoy/version)
285 vtinfo
"Upgrade operation is safe, all the data in the 1st partition (iso files and other) will be unchanged!"
288 read -p "Update Ventoy $oldver ===> $curver Continue? (y/n)" Answer
289 if [ "$Answer" != "y" ]; then
290 if [ "$Answer" != "Y" ]; then
295 PART2
=$(get_disk_part_name $DISK 2)
296 SHORT_PART2
=${PART2#/dev/}
297 part2_start
=$(cat /sys/class/block/$SHORT_PART2/start)
299 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=440
301 PART1_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=446 status=none | ./tool/hexdump -n1 -e '1/1 "%02X"')
302 PART2_ACTIVE
=$(dd if=$DISK bs=1 count=1 skip=462 status=none | ./tool/hexdump -n1 -e '1/1 "%02X"')
304 vtdebug
"PART1_ACTIVE=$PART1_ACTIVE PART2_ACTIVE=$PART2_ACTIVE"
305 if [ "$PART1_ACTIVE" = "00" ] && [ "$PART2_ACTIVE" = "80" ]; then
306 vtdebug
"change 1st partition active, 2nd partition inactive ..."
307 echo -en '\x80' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=446 status
=none
308 echo -en '\x00' | dd of
=$DISK conv
=fsync bs
=1 count
=1 seek
=462 status
=none
311 .
/tool
/xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
312 .
/tool
/xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start
316 if [ "$SECUREBOOT" != "YES" ]; then
319 vtdebug
"mounting part2 ...."
321 if mount
${DISK}2 .
/tmp_mnt
; then
322 vtdebug
"mounting part2 success"
328 rm -f .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
329 rm -f .
/tmp_mnt
/EFI
/BOOT
/grubx64.efi
330 rm -f .
/tmp_mnt
/EFI
/BOOT
/MokManager.efi
331 rm -f .
/tmp_mnt
/ENROLL_THIS_KEY_IN_MOKMANAGER.cer
332 mv .
/tmp_mnt
/EFI
/BOOT
/grubx64_real.efi .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
339 vtinfo
"Update Ventoy to $DISK successfully finished."