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 testEF
=$(echo -en '\xEF' | ./tool/hexdump -n 1 -e '1/1 "%02X"')
114 if [ "$testEF" != "EF" ]; then
115 vtdebug
"testEF=##${testEF}##"
116 vterr
"There is something wrong with the interpreter !"
120 grep "^$DISK" /proc
/mounts
| while read mtline
; do
121 mtpnt
=$(echo $mtline | awk '{print $2}')
122 vtdebug
"Trying to umount $mtpnt ..."
123 umount
$mtpnt >/dev
/null
2>&1
126 if swapon
-s | grep -q "^${DISK}[0-9]"; then
127 swapon
-s | grep "^${DISK}[0-9]" | awk '{print $1}' | while read line
; do
128 vtdebug
"Trying to swapoff $line ..."
134 if grep "$DISK" /proc
/mounts
; then
135 vterr
"$DISK is already mounted, please umount it first!"
140 if swapon
-s | grep -q "^${DISK}[0-9]"; then
141 vterr
"$DISK is used as swap, please swapoff it first!"
147 if [ "$MODE" = "install" ]; then
148 vtdebug
"install ventoy ..."
150 if parted
-v > /dev
/null
2>&1; then
152 elif fdisk
-v >/dev
/null
2>&1; then
155 vterr
"Both parted and fdisk are not found in the sysstem, Ventoy can't create new partition."
160 version
=$(get_disk_ventoy_version $DISK)
161 if [ $?
-eq 0 ]; then
162 if [ -z "$FORCE" ]; then
163 vtwarn
"$DISK already contains a Ventoy with version $version"
164 vtwarn
"Use -u option to do a safe upgrade operation."
165 vtwarn
"OR if you really want to reinstall ventoy to $DISK, please use -I option."
172 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
173 disk_size_gb
=$(expr $disk_sector_num / 2097152)
175 if [ $disk_sector_num -gt 4294967296 ]; then
176 vterr
"$DISK is over 2TB size, MBR will not work on it."
183 parted
-s $DISK p
2>&1 | grep Model
184 echo "Size : $disk_size_gb GB"
188 vtwarn
"You will install Ventoy to $DISK."
189 vtwarn
"All the data on the disk $DISK will be lost!!!"
192 read -p 'Continue? (y/n)' Answer
193 if [ "$Answer" != "y" ]; then
194 if [ "$Answer" != "Y" ]; then
200 vtwarn
"All the data on the disk $DISK will be lost!!!"
201 read -p 'Double-check. Continue? (y/n)' Answer
202 if [ "$Answer" != "y" ]; then
203 if [ "$Answer" != "Y" ]; then
209 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
210 vterr
"No enough space in disk $DISK"
214 if ! dd if=/dev
/zero of
=$DISK bs
=1 count
=512 status
=none conv
=fsync
; then
215 vterr
"Write data to $DISK failed, please check whether it's in use."
219 format_ventoy_disk
$DISK $PARTTOOL
222 if ventoy_is_linux64
; then
223 cmd
=.
/tool
/mkexfatfs_64
225 cmd
=.
/tool
/mkexfatfs_32
230 # DiskSize > 32GB Cluster Size use 128KB
231 # DiskSize < 32GB Cluster Size use 32KB
232 if [ $disk_size_gb -gt 32 ]; then
238 $cmd -n ventoy
-s $cluster_sectors ${DISK}1
240 chmod +x .
/tool
/vtoy_gen_uuid
242 vtinfo
"writing data to disk ..."
243 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=446
244 .
/tool
/xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
245 .
/tool
/xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start_sector
248 .
/tool
/vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} seek
=384 bs
=1 count
=16
251 .
/tool
/vtoy_gen_uuid
| dd status
=none conv
=fsync of
=${DISK} skip
=12 seek
=440 bs
=1 count
=4
253 vtinfo
"sync data ..."
256 vtinfo
"esp partition processing ..."
259 mtpnt
=$(grep "^${DISK}2" /proc/mounts | awk '{print $2}')
260 if [ -n "$mtpnt" ]; then
261 umount
$mtpnt >/dev
/null
2>&1
264 if [ "$SECUREBOOT" != "YES" ]; then
267 vtdebug
"mounting part2 ...."
269 if mount
${DISK}2 .
/tmp_mnt
; then
270 vtdebug
"mounting part2 success"
274 mtpnt
=$(grep "^${DISK}2" /proc/mounts | awk '{print $2}')
275 if [ -n "$mtpnt" ]; then
276 umount
$mtpnt >/dev
/null
2>&1
281 rm -f .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
282 rm -f .
/tmp_mnt
/EFI
/BOOT
/grubx64.efi
283 rm -f .
/tmp_mnt
/EFI
/BOOT
/MokManager.efi
284 rm -f .
/tmp_mnt
/ENROLL_THIS_KEY_IN_MOKMANAGER.cer
285 mv .
/tmp_mnt
/EFI
/BOOT
/grubx64_real.efi .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
292 vtinfo
"Install Ventoy to $DISK successfully finished."
296 vtdebug
"update ventoy ..."
298 oldver
=$(get_disk_ventoy_version $DISK)
299 if [ $?
-ne 0 ]; then
300 vtwarn
"$DISK does not contain ventoy or data corupted"
302 vtwarn
"Please use -i option if you want to install ventoy to $DISK"
308 curver
=$(cat ./ventoy/version)
310 vtinfo
"Upgrade operation is safe, all the data in the 1st partition (iso files and other) will be unchanged!"
313 read -p "Update Ventoy $oldver ===> $curver Continue? (y/n)" Answer
314 if [ "$Answer" != "y" ]; then
315 if [ "$Answer" != "Y" ]; then
321 PART2
=$(get_disk_part_name $DISK 2)
323 dd status
=none conv
=fsync
if=.
/boot
/boot.img of
=$DISK bs
=1 count
=440
325 .
/tool
/xzcat .
/boot
/core.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=2047 seek
=1
327 disk_sector_num
=$(cat /sys/block/${DISK#/dev/}/size)
328 part2_start
=$(expr $disk_sector_num - $VENTOY_SECTOR_NUM)
329 .
/tool
/xzcat .
/ventoy
/ventoy.disk.img.xz
| dd status
=none conv
=fsync of
=$DISK bs
=512 count
=$VENTOY_SECTOR_NUM seek
=$part2_start
333 if [ "$SECUREBOOT" != "YES" ]; then
336 vtdebug
"mounting part2 ...."
338 if mount
${DISK}2 .
/tmp_mnt
; then
339 vtdebug
"mounting part2 success"
345 rm -f .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
346 rm -f .
/tmp_mnt
/EFI
/BOOT
/grubx64.efi
347 rm -f .
/tmp_mnt
/EFI
/BOOT
/MokManager.efi
348 rm -f .
/tmp_mnt
/ENROLL_THIS_KEY_IN_MOKMANAGER.cer
349 mv .
/tmp_mnt
/EFI
/BOOT
/grubx64_real.efi .
/tmp_mnt
/EFI
/BOOT
/BOOTX64.EFI
356 vtinfo
"Update Ventoy to $DISK successfully finished."