5 if ! [ -f .
/tool
/ventoy_lib.sh
]; then
12 echo 'Usage: Ventoy2Disk.sh CMD [ OPTION ] /dev/sdX'
14 echo ' -i install ventoy to sdX (fail if disk already installed with ventoy)'
15 echo ' -u update ventoy in sdX'
16 echo ' -I force install ventoy to sdX (no matter installed or not)'
18 echo ' OPTION: (optional)'
19 echo ' -s enable secure boot support (default is disabled)'
25 echo '***********************************************************'
26 echo '* Ventoy2Disk Script *'
27 echo '* longpanda admin@ventoy.net *'
28 echo '***********************************************************'
31 vtdebug
"############# Ventoy2Disk $0 ################"
34 if [ "$1" = "-i" ]; then
36 elif [ "$1" = "-I" ]; then
39 elif [ "$1" = "-u" ]; then
41 elif [ "$1" = "-s" ]; then
44 if ! [ -b "$1" ]; then
45 vterr
"$1 is NOT a valid device"
56 if [ -z "$MODE" ]; then
62 if ! [ -b "$DISK" ]; then
63 vterr
"Disk $DISK does not exist"
68 if [ -e /sys
/class
/block
/${DISK#/dev/}/start
]; then
69 vterr
"$DISK is a partition, please use the whole disk"
74 if dd if="$DISK" of
=/dev
/null bs
=1 count
=1 >/dev
/null
2>&1; then
75 vtdebug
"root permission check ok ..."
77 vterr
"Failed to access $DISK, maybe root privilege is needed!"
83 vtdebug
"MODE=$MODE FORCE=$FORCE"
85 if ! [ -f .
/boot
/boot.img
]; then
86 if [ -d .
/grub
]; then
87 vterr
"Don't run me here, please download the released install package, and run there."
89 vterr
"Please run under the right directory!"
98 if [ "$file" != "xzcat" ]; then
99 if [ "$file" != "ventoy_lib.sh" ]; then
100 .
/xzcat
$file > ${file%.xz}
107 if ! check_tool_work_ok
; then
108 vterr
"Some tools can not run in current system. Please check log.txt for detail."
113 grep "^$DISK" /proc
/mounts
| while read mtline
; do
114 mtpnt
=$(echo $mtline | awk '{print $2}')
115 vtdebug
"Trying to umount $mtpnt ..."
116 umount
$mtpnt >/dev
/null
2>&1
119 if swapon
-s | grep -q "^${DISK}[0-9]"; then
120 swapon
-s | grep "^${DISK}[0-9]" | awk '{print $1}' | while read line
; do
121 vtdebug
"Trying to swapoff $line ..."
127 if grep "$DISK" /proc
/mounts
; then
128 vterr
"$DISK is already mounted, please umount it first!"
133 if swapon
-s | grep -q "^${DISK}[0-9]"; then
134 vterr
"$DISK is used as swap, please swapoff it first!"
140 if [ "$MODE" = "install" ]; then
141 vtdebug
"install ventoy ..."
143 if ! fdisk
-v >/dev
/null
2>&1; then
144 vterr
"fdisk is needed by ventoy installation, but is not found in the system."
149 version
=$(get_disk_ventoy_version $DISK)
150 if [ $?
-eq 0 ]; then
151 if [ -z "$FORCE" ]; then
152 vtwarn
"$DISK already contains a Ventoy with version $version"
153 vtwarn
"Use -u option to do a safe upgrade operation."
154 vtwarn
"OR if you really want to reinstall ventoy to $DISK, please use -I option."
161 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
162 disk_size_gb
=$(expr $disk_sector_num / 2097152)
164 if [ $disk_sector_num -gt 4294967296 ]; then
165 vterr
"$DISK is over 2TB size, MBR will not work on it."
172 parted
-s $DISK p
2>&1 | grep Model
173 echo "Size : $disk_size_gb GB"
177 vtwarn
"You will install Ventoy to $DISK."
178 vtwarn
"All the data on the disk $DISK will be lost!!!"
181 read -p 'Continue? (y/n)' Answer
182 if [ "$Answer" != "y" ]; then
183 if [ "$Answer" != "Y" ]; then
189 vtwarn
"All the data on the disk $DISK will be lost!!!"
190 read -p 'Double-check. Continue? (y/n)' Answer
191 if [ "$Answer" != "y" ]; then
192 if [ "$Answer" != "Y" ]; then
198 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
199 vterr
"No enough space in disk $DISK"
203 if ! dd if=/dev
/zero of
=$DISK bs
=1 count
=512 status
=none conv
=fsync
; then
204 vterr
"Write data to $DISK failed, please check whether it's in use."
208 format_ventoy_disk
$DISK
211 if ventoy_is_linux64
; then
212 cmd
=.
/tool
/mkexfatfs_64
214 cmd
=.
/tool
/mkexfatfs_32
219 # DiskSize > 32GB Cluster Size use 128KB
220 # DiskSize < 32GB Cluster Size use 32KB
221 if [ $disk_size_gb -gt 32 ]; then
227 $cmd -n ventoy
-s $cluster_sectors ${DISK}1
229 chmod +x .
/tool
/vtoy_gen_uuid
231 vtinfo
"writing data to disk ..."
232 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=446
233 .
/tool
/xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
234 .
/tool
/xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start_sector
237 .
/tool
/vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} seek
=384 bs
=1 count
=16
240 .
/tool
/vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} skip
=12 seek
=440 bs
=1 count
=4
242 vtinfo
"sync data ..."
245 vtinfo
"esp partition processing ..."
248 mtpnt
=$(grep "^${DISK}2" /proc/mounts | awk '{print $2}')
249 if [ -n "$mtpnt" ]; then
250 umount
$mtpnt >/dev
/null
2>&1
253 if [ "$SECUREBOOT" != "YES" ]; then
256 vtdebug
"mounting part2 ...."
258 if mount
${DISK}2 .
/tmp_mnt
; then
259 vtdebug
"mounting part2 success"
263 mtpnt
=$(grep "^${DISK}2" /proc/mounts | awk '{print $2}')
264 if [ -n "$mtpnt" ]; then
265 umount
$mtpnt >/dev
/null
2>&1
270 rm -f .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
271 rm -f .
/tmp_mnt
/EFI
/BOOT
/grubx64.efi
272 rm -f .
/tmp_mnt
/EFI
/BOOT
/MokManager.efi
273 rm -f .
/tmp_mnt
/ENROLL_THIS_KEY_IN_MOKMANAGER.cer
274 mv .
/tmp_mnt
/EFI
/BOOT
/grubx64_real.efi .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
281 vtinfo
"Install Ventoy to $DISK successfully finished."
285 vtdebug
"update ventoy ..."
287 oldver
=$(get_disk_ventoy_version $DISK)
288 if [ $?
-ne 0 ]; then
289 vtwarn
"$DISK does not contain ventoy or data corupted"
291 vtwarn
"Please use -i option if you want to install ventoy to $DISK"
297 curver
=$(cat ./ventoy/version)
299 vtinfo
"Upgrade operation is safe, all the data in the 1st partition (iso files and other) will be unchanged!"
302 read -p "Update Ventoy $oldver ===> $curver Continue? (y/n)" Answer
303 if [ "$Answer" != "y" ]; then
304 if [ "$Answer" != "Y" ]; then
310 PART2
=$(get_disk_part_name $DISK 2)
312 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=440
314 .
/tool
/xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
316 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
317 part2_start
=$(expr $disk_sector_num - $VENTOY_SECTOR_NUM)
318 .
/tool
/xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start
322 if [ "$SECUREBOOT" != "YES" ]; then
325 vtdebug
"mounting part2 ...."
327 if mount
${DISK}2 .
/tmp_mnt
; then
328 vtdebug
"mounting part2 success"
334 rm -f .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
335 rm -f .
/tmp_mnt
/EFI
/BOOT
/grubx64.efi
336 rm -f .
/tmp_mnt
/EFI
/BOOT
/MokManager.efi
337 rm -f .
/tmp_mnt
/ENROLL_THIS_KEY_IN_MOKMANAGER.cer
338 mv .
/tmp_mnt
/EFI
/BOOT
/grubx64_real.efi .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
345 vtinfo
"Update Ventoy to $DISK successfully finished."