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 ' -s enable secure boot support (default is disabled)'
20 if [ "$1" = "-i" ]; then
22 elif [ "$1" = "-I" ]; then
25 elif [ "$1" = "-u" ]; then
27 elif [ "$1" = "-s" ]; then
30 if ! [ -b "$1" ]; then
31 vterr
"$1 is NOT a valid device"
41 if [ -z "$MODE" ]; then
46 if ! [ -b "$DISK" ]; then
47 vterr
"Disk $DISK does not exist"
51 if [ -e /sys
/class
/block
/${DISK#/dev/}/start
]; then
52 vterr
"$DISK is a partition, please use the whole disk"
56 if dd if="$DISK" of
=/dev
/null bs
=1 count
=1 >/dev
/null
2>&1; then
57 vtdebug
"root permission check ok ..."
59 vterr
"Failed to access $DISK, maybe root privilege is needed!"
64 vtdebug
"MODE=$MODE FORCE=$FORCE"
66 if ! check_tool_work_ok
; then
67 vterr
"Some tools can not run in current system. Please check log.txt for detail."
71 grep "^$DISK" /proc
/mounts
| while read mtline
; do
72 mtpnt
=$(echo $mtline | awk '{print $2}')
73 vtdebug
"Trying to umount $mtpnt ..."
74 umount
$mtpnt >/dev
/null
2>&1
77 if swapon
-s | grep -q "^${DISK}[0-9]"; then
78 swapon
-s | grep "^${DISK}[0-9]" | awk '{print $1}' | while read line
; do
79 vtdebug
"Trying to swapoff $line ..."
85 if grep "$DISK" /proc
/mounts
; then
86 vterr
"$DISK is already mounted, please umount it first!"
90 if swapon
-s | grep -q "^${DISK}[0-9]"; then
91 vterr
"$DISK is used as swap, please swapoff it first!"
96 if [ "$MODE" = "install" ]; then
97 vtdebug
"install ventoy ..."
99 if parted
-v > /dev
/null
2>&1; then
101 elif fdisk
-v >/dev
/null
2>&1; then
104 vterr
"Both parted and fdisk are not found in the sysstem, Ventoy can't create new partition."
108 version
=$(get_disk_ventoy_version $DISK)
109 if [ $?
-eq 0 ]; then
110 if [ -z "$FORCE" ]; then
111 vtwarn
"$DISK already contains a Ventoy with version $version"
112 vtwarn
"Use -u option to do a safe upgrade operation."
113 vtwarn
"OR if you really want to reinstall ventoy to $DISK, please use -I option."
119 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
120 disk_size_gb
=$(expr $disk_sector_num / 2097152)
122 if [ $disk_sector_num -gt 4294967296 ]; then
123 vterr
"$DISK is over 2TB size, MBR will not work on it."
129 parted
-s $DISK p
2>&1 | grep Model
130 echo "Size : $disk_size_gb GB"
134 vtwarn
"You will install Ventoy to $DISK."
135 vtwarn
"All the data on the disk $DISK will be lost!!!"
138 read -p 'Continue? (y/n)' Answer
139 if [ "$Answer" != "y" ]; then
140 if [ "$Answer" != "Y" ]; then
146 vtwarn
"All the data on the disk $DISK will be lost!!!"
147 read -p 'Double-check. Continue? (y/n)' Answer
148 if [ "$Answer" != "y" ]; then
149 if [ "$Answer" != "Y" ]; then
155 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
156 vterr
"No enough space in disk $DISK"
160 if ! dd if=/dev
/zero of
=$DISK bs
=1 count
=512 status
=none conv
=fsync
; then
161 vterr
"Write data to $DISK failed, please check whether it's in use."
165 format_ventoy_disk
$DISK $PARTTOOL
168 if ventoy_is_linux64
; then
169 cmd
=.
/tool
/mkexfatfs_64
171 cmd
=.
/tool
/mkexfatfs_32
176 # DiskSize > 32GB Cluster Size use 128KB
177 # DiskSize < 32GB Cluster Size use 32KB
178 if [ $disk_size_gb -gt 32 ]; then
184 $cmd -n ventoy
-s $cluster_sectors ${DISK}1
186 chmod +x .
/tool
/vtoy_gen_uuid
188 vtinfo
"writing data to disk ..."
189 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=446
190 .
/tool
/xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
191 .
/tool
/xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start_sector
194 .
/tool
/vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} seek
=384 bs
=1 count
=16
197 .
/tool
/vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} skip
=12 seek
=440 bs
=1 count
=4
199 vtinfo
"sync data ..."
202 vtinfo
"esp partition processing ..."
205 mtpnt
=$(grep "^${DISK}2" /proc/mounts | awk '{print $2}')
206 if [ -n "$mtpnt" ]; then
207 umount
$mtpnt >/dev
/null
2>&1
210 if [ "$SECUREBOOT" != "YES" ]; then
213 vtdebug
"mounting part2 ...."
215 if mount
${DISK}2 .
/tmp_mnt
; then
216 vtdebug
"mounting part2 success"
220 mtpnt
=$(grep "^${DISK}2" /proc/mounts | awk '{print $2}')
221 if [ -n "$mtpnt" ]; then
222 umount
$mtpnt >/dev
/null
2>&1
227 rm -f .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
228 rm -f .
/tmp_mnt
/EFI
/BOOT
/grubx64.efi
229 rm -f .
/tmp_mnt
/EFI
/BOOT
/MokManager.efi
230 rm -f .
/tmp_mnt
/ENROLL_THIS_KEY_IN_MOKMANAGER.cer
231 mv .
/tmp_mnt
/EFI
/BOOT
/grubx64_real.efi .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
238 vtinfo
"Install Ventoy to $DISK successfully finished."
242 vtdebug
"update ventoy ..."
244 oldver
=$(get_disk_ventoy_version $DISK)
245 if [ $?
-ne 0 ]; then
246 vtwarn
"$DISK does not contain ventoy or data corupted"
248 vtwarn
"Please use -i option if you want to install ventoy to $DISK"
253 curver
=$(cat ./ventoy/version)
255 vtinfo
"Upgrade operation is safe, all the data in the 1st partition (iso files and other) will be unchanged!"
258 read -p "Update Ventoy $oldver ===> $curver Continue? (y/n)" Answer
259 if [ "$Answer" != "y" ]; then
260 if [ "$Answer" != "Y" ]; then
265 PART2
=$(get_disk_part_name $DISK 2)
267 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=440
269 .
/tool
/xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
271 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
272 part2_start
=$(expr $disk_sector_num - $VENTOY_SECTOR_NUM)
273 .
/tool
/xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start
277 if [ "$SECUREBOOT" != "YES" ]; then
280 vtdebug
"mounting part2 ...."
282 if mount
${DISK}2 .
/tmp_mnt
; then
283 vtdebug
"mounting part2 success"
289 rm -f .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
290 rm -f .
/tmp_mnt
/EFI
/BOOT
/grubx64.efi
291 rm -f .
/tmp_mnt
/EFI
/BOOT
/MokManager.efi
292 rm -f .
/tmp_mnt
/ENROLL_THIS_KEY_IN_MOKMANAGER.cer
293 mv .
/tmp_mnt
/EFI
/BOOT
/grubx64_real.efi .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
300 vtinfo
"Update Ventoy to $DISK successfully finished."