]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - INSTALL/tool/VentoyWorker.sh
Ventoy2Disk.exe add a star to identify secure boot
[Ventoy.git] / INSTALL / tool / VentoyWorker.sh
1 #!/bin/sh
2
3 . ./tool/ventoy_lib.sh
4
5 print_usage() {
6
7 echo 'Usage: Ventoy2Disk.sh CMD [ OPTION ] /dev/sdX'
8 echo ' CMD:'
9 echo ' -i install ventoy to sdX (fail if disk already installed with ventoy)'
10 echo ' -I force install ventoy to sdX (no matter installed or not)'
11 echo ' -u update ventoy in sdX'
12 echo ' -l list Ventoy information in sdX'
13 echo ''
14 echo ' OPTION: (optional)'
15 echo ' -r SIZE_MB preserve some space at the bottom of the disk (only for install)'
16 echo ' -s/-S enable/disable secure boot support (default is disabled)'
17 echo ' -g use GPT partition style, default is MBR (only for install)'
18 echo ' -L Label of the 1st exfat partition (default is Ventoy)'
19 echo ''
20 }
21
22
23 VTNEW_LABEL='Ventoy'
24 RESERVE_SIZE_MB=0
25 while [ -n "$1" ]; do
26 if [ "$1" = "-i" ]; then
27 MODE="install"
28 elif [ "$1" = "-I" ]; then
29 MODE="install"
30 FORCE="Y"
31 elif [ "$1" = "-u" ]; then
32 MODE="update"
33 elif [ "$1" = "-l" ]; then
34 MODE="list"
35 elif [ "$1" = "-s" ]; then
36 SECUREBOOT="YES"
37 elif [ "$1" = "-S" ]; then
38 SECUREBOOT="NO"
39 elif [ "$1" = "-g" ]; then
40 VTGPT="YES"
41 elif [ "$1" = "-L" ]; then
42 shift
43 VTNEW_LABEL=$1
44 elif [ "$1" = "-r" ]; then
45 RESERVE_SPACE="YES"
46 shift
47 RESERVE_SIZE_MB=$1
48 elif [ "$1" = "-V" ] || [ "$1" = "--version" ]; then
49 exit 0
50 elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
51 print_usage
52 exit 0
53 else
54 if ! [ -b "$1" ]; then
55 vterr "$1 is NOT a valid device"
56 print_usage
57 exit 1
58 fi
59 DISK=$1
60 fi
61
62 shift
63 done
64
65 if [ -z "$MODE" ]; then
66 print_usage
67 exit 1
68 fi
69
70 if ! [ -b "$DISK" ]; then
71 vterr "Disk $DISK does not exist"
72 exit 1
73 fi
74
75 if [ -e /sys/class/block/${DISK#/dev/}/start ]; then
76 vterr "$DISK is a partition, please use the whole disk."
77 echo "For example:"
78 vterr " sudo sh Ventoy2Disk.sh -i /dev/sdb1 <=== This is wrong"
79 vtinfo " sudo sh Ventoy2Disk.sh -i /dev/sdb <=== This is right"
80 echo ""
81 exit 1
82 fi
83
84 if [ -n "$RESERVE_SPACE" -a "$MODE" = "install" ]; then
85 if echo $RESERVE_SIZE_MB | grep -q '^[0-9][0-9]*$'; then
86 vtdebug "User will reserve $RESERVE_SIZE_MB MB disk space"
87 else
88 vterr "$RESERVE_SIZE_MB is invalid for reserved space"
89 exit 1
90 fi
91 fi
92
93 vtdebug "MODE=$MODE FORCE=$FORCE RESERVE_SPACE=$RESERVE_SPACE RESERVE_SIZE_MB=$RESERVE_SIZE_MB"
94
95 #check tools
96 if check_tool_work_ok; then
97 vtdebug "check tool work ok"
98 else
99 vterr "Some tools can not run in current system. Please check log.txt for detail."
100 exit 1
101 fi
102
103 if [ "$MODE" = "list" ]; then
104 version=$(get_disk_ventoy_version $DISK)
105 if [ $? -eq 0 ]; then
106 echo "Ventoy Version in Disk: $version"
107
108 vtPart1Type=$(dd if=$DISK bs=1 count=1 skip=450 status=none | hexdump -n1 -e '1/1 "%02X"')
109 if [ "$vtPart1Type" = "EE" ]; then
110 echo "Disk Partition Style : GPT"
111 else
112 echo "Disk Partition Style : MBR"
113 fi
114
115 if check_disk_secure_boot $DISK; then
116 echo "Secure Boot Support : YES"
117 else
118 echo "Secure Boot Support : NO"
119 fi
120 else
121 echo "Ventoy Version: NA"
122 fi
123 echo ""
124 exit 0
125 fi
126
127 #check mountpoint
128 grep "^$DISK" /proc/mounts | while read mtline; do
129 mtpnt=$(echo $mtline | awk '{print $2}')
130 vtdebug "Trying to umount $mtpnt ..."
131 umount $mtpnt >/dev/null 2>&1
132 done
133
134 if grep "$DISK" /proc/mounts; then
135 vterr "$DISK is already mounted, please umount it first!"
136 exit 1
137 fi
138
139 #check swap partition
140 if swapon --help 2>&1 | grep -q '^ \-s,'; then
141 if swapon -s | grep -q "^${DISK}[0-9]"; then
142 vterr "$DISK is used as swap, please swapoff it first!"
143 exit 1
144 fi
145 fi
146
147 #check access
148 if dd if="$DISK" of=/dev/null bs=1 count=1 >/dev/null 2>&1; then
149 vtdebug "root permission check ok ..."
150 else
151 vterr "Failed to access $DISK, maybe root privilege is needed!"
152 echo ''
153 exit 1
154 fi
155
156
157 #check tmp_mnt directory
158 if [ -d ./tmp_mnt ]; then
159 vtdebug "There is a tmp_mnt directory, now delete it."
160 umount ./tmp_mnt >/dev/null 2>&1
161 rm -rf ./tmp_mnt
162 if [ -d ./tmp_mnt ]; then
163 vterr "tmp_mnt directory exit, please delete it first."
164 exit 1
165 fi
166 fi
167
168
169 if [ "$MODE" = "install" ]; then
170 vtdebug "install ventoy ..."
171
172 if [ -n "$VTGPT" ]; then
173 if parted -v > /dev/null 2>&1; then
174 PARTTOOL='parted'
175 else
176 vterr "parted is not found in the system, Ventoy can't create new partitions without it."
177 vterr "You should install \"GNU parted\" first."
178 exit 1
179 fi
180 else
181 if parted -v > /dev/null 2>&1; then
182 PARTTOOL='parted'
183 elif fdisk -v >/dev/null 2>&1; then
184 PARTTOOL='fdisk'
185 else
186 vterr "Both parted and fdisk are not found in the system, Ventoy can't create new partitions."
187 exit 1
188 fi
189 fi
190
191 version=$(get_disk_ventoy_version $DISK)
192 if [ $? -eq 0 ]; then
193 if [ -z "$FORCE" ]; then
194 vtwarn "$DISK already contains a Ventoy with version $version"
195 vtwarn "Use -u option to do a safe upgrade operation."
196 vtwarn "OR if you really want to reinstall ventoy to $DISK, please use -I option."
197 vtwarn ""
198 exit 1
199 fi
200 fi
201
202 disk_sector_num=$(cat /sys/block/${DISK#/dev/}/size)
203 disk_size_gb=$(expr $disk_sector_num / 2097152)
204
205 if [ $disk_sector_num -gt 4294967296 ] && [ -z "$VTGPT" ]; then
206 vterr "$DISK is over 2TB size, MBR will not work on it."
207 exit 1
208 fi
209
210 if [ -n "$RESERVE_SPACE" ]; then
211 sum_size_mb=$(expr $RESERVE_SIZE_MB + $VENTOY_PART_SIZE_MB)
212 reserve_sector_num=$(expr $sum_size_mb \* 2048)
213
214 if [ $disk_sector_num -le $reserve_sector_num ]; then
215 vterr "Can't reserve $RESERVE_SIZE_MB MB space from $DISK"
216 exit 1
217 fi
218 fi
219
220 #Print disk info
221 echo "Disk : $DISK"
222 parted -s $DISK p 2>&1 | grep Model
223 echo "Size : $disk_size_gb GB"
224 if [ -n "$VTGPT" ]; then
225 echo "Style: GPT"
226 else
227 echo "Style: MBR"
228 fi
229 echo ''
230
231 if [ -n "$RESERVE_SPACE" ]; then
232 echo "You will reserve $RESERVE_SIZE_MB MB disk space "
233 fi
234 echo ''
235
236 vtwarn "Attention:"
237 vtwarn "You will install Ventoy to $DISK."
238 vtwarn "All the data on the disk $DISK will be lost!!!"
239 echo ""
240
241 read -p 'Continue? (y/n) ' Answer
242 if [ "$Answer" != "y" ]; then
243 if [ "$Answer" != "Y" ]; then
244 exit 0
245 fi
246 fi
247
248 echo ""
249 vtwarn "All the data on the disk $DISK will be lost!!!"
250 read -p 'Double-check. Continue? (y/n) ' Answer
251 if [ "$Answer" != "y" ]; then
252 if [ "$Answer" != "Y" ]; then
253 exit 0
254 fi
255 fi
256
257 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
258 vterr "No enough space in disk $DISK"
259 exit 1
260 fi
261
262 if ! dd if=/dev/zero of=$DISK bs=1 count=512 status=none conv=fsync; then
263 vterr "Write data to $DISK failed, please check whether it's in use."
264 exit 1
265 fi
266
267 if [ -n "$VTGPT" ]; then
268 vtdebug "format_ventoy_disk_gpt $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
269 format_ventoy_disk_gpt $RESERVE_SIZE_MB $DISK $PARTTOOL
270 else
271 vtdebug "format_ventoy_disk_mbr $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
272 format_ventoy_disk_mbr $RESERVE_SIZE_MB $DISK $PARTTOOL
273 fi
274
275 # format part1
276
277 # DiskSize > 32GB Cluster Size use 128KB
278 # DiskSize < 32GB Cluster Size use 32KB
279 if [ $disk_size_gb -gt 32 ]; then
280 cluster_sectors=256
281 else
282 cluster_sectors=64
283 fi
284
285 PART1=$(get_disk_part_name $DISK 1)
286 PART2=$(get_disk_part_name $DISK 2)
287
288 mkexfatfs -n "$VTNEW_LABEL" -s $cluster_sectors ${PART1}
289
290 vtinfo "writing data to disk ..."
291
292 dd status=none conv=fsync if=./boot/boot.img of=$DISK bs=1 count=446
293
294 if [ -n "$VTGPT" ]; then
295 echo -en '\x22' | dd status=none of=$DISK conv=fsync bs=1 count=1 seek=92
296 xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2014 seek=34
297 echo -en '\x23' | dd of=$DISK conv=fsync bs=1 count=1 seek=17908 status=none
298 else
299 xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2047 seek=1
300 fi
301
302 xzcat ./ventoy/ventoy.disk.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=$VENTOY_SECTOR_NUM seek=$part2_start_sector
303
304 #test UUID
305 testUUIDStr=$(vtoy_gen_uuid | hexdump -C)
306 vtdebug "test uuid: $testUUIDStr"
307
308 #disk uuid
309 vtoy_gen_uuid | dd status=none conv=fsync of=${DISK} seek=384 bs=1 count=16
310
311 #disk signature
312 vtoy_gen_uuid | dd status=none conv=fsync of=${DISK} skip=12 seek=440 bs=1 count=4
313
314 vtinfo "sync data ..."
315 sync
316
317 vtinfo "esp partition processing ..."
318
319 sleep 1
320 mtpnt=$(grep "^${PART2}" /proc/mounts | awk '{print $2}')
321 if [ -n "$mtpnt" ]; then
322 umount $mtpnt >/dev/null 2>&1
323 fi
324
325 if [ "$SECUREBOOT" != "YES" ]; then
326 mkdir ./tmp_mnt
327
328 vtdebug "mounting part2 ...."
329 for tt in 1 2 3 4 5; do
330 if mount ${PART2} ./tmp_mnt > /dev/null 2>&1; then
331 vtdebug "mounting part2 success"
332 break
333 fi
334
335 mtpnt=$(grep "^${PART2}" /proc/mounts | awk '{print $2}')
336 if [ -n "$mtpnt" ]; then
337 umount $mtpnt >/dev/null 2>&1
338 fi
339 sleep 2
340 done
341
342 rm -f ./tmp_mnt/EFI/BOOT/BOOTX64.EFI
343 rm -f ./tmp_mnt/EFI/BOOT/grubx64.efi
344 rm -f ./tmp_mnt/EFI/BOOT/BOOTIA32.EFI
345 rm -f ./tmp_mnt/EFI/BOOT/grubia32.efi
346 rm -f ./tmp_mnt/EFI/BOOT/MokManager.efi
347 rm -f ./tmp_mnt/EFI/BOOT/mmia32.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
350 mv ./tmp_mnt/EFI/BOOT/grubia32_real.efi ./tmp_mnt/EFI/BOOT/BOOTIA32.EFI
351
352 for tt in 1 2 3; do
353 if umount ./tmp_mnt; then
354 vtdebug "umount part2 success"
355 rm -rf ./tmp_mnt
356 break
357 else
358 vtdebug "umount part2 failed, now retry..."
359 sleep 1
360 fi
361 done
362 fi
363
364 echo ""
365 vtinfo "Install Ventoy to $DISK successfully finished."
366 echo ""
367
368 else
369 vtdebug "update ventoy ..."
370
371 oldver=$(get_disk_ventoy_version $DISK)
372 if [ $? -ne 0 ]; then
373 vtwarn "$DISK does not contain ventoy or data corupted"
374 echo ""
375 vtwarn "Please use -i option if you want to install ventoy to $DISK"
376 echo ""
377 exit 1
378 fi
379
380 #reserve secure boot option
381 if [ -z "$SECUREBOOT" ]; then
382 if check_disk_secure_boot $DISK; then
383 SECUREBOOT="YES"
384 else
385 SECUREBOOT="NO"
386 fi
387 fi
388
389 curver=$(cat ./ventoy/version)
390
391 vtinfo "Upgrade operation is safe, all the data in the 1st partition (iso files and other) will be unchanged!"
392 echo ""
393
394 read -p "Update Ventoy $oldver ===> $curver Continue? (y/n)" Answer
395 if [ "$Answer" != "y" ]; then
396 if [ "$Answer" != "Y" ]; then
397 exit 0
398 fi
399 fi
400
401 PART2=$(get_disk_part_name $DISK 2)
402 SHORT_PART2=${PART2#/dev/}
403 part2_start=$(cat /sys/class/block/$SHORT_PART2/start)
404
405 PART1_TYPE=$(dd if=$DISK bs=1 count=1 skip=450 status=none | hexdump -n1 -e '1/1 "%02X"')
406
407 #reserve disk uuid
408 rm -f ./diskuuid.bin
409 dd status=none conv=fsync if=${DISK} skip=384 bs=1 count=16 of=./diskuuid.bin
410
411 dd status=none conv=fsync if=./boot/boot.img of=$DISK bs=1 count=440
412 dd status=none conv=fsync if=./diskuuid.bin of=$DISK bs=1 count=16 seek=384
413 rm -f ./diskuuid.bin
414
415 #reserve data
416 rm -f ./rsvdata.bin
417 dd status=none conv=fsync if=${DISK} skip=2040 bs=512 count=8 of=./rsvdata.bin
418
419 if [ "$PART1_TYPE" = "EE" ]; then
420 vtdebug "This is GPT partition style ..."
421 echo -en '\x22' | dd status=none of=$DISK conv=fsync bs=1 count=1 seek=92
422 xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2014 seek=34
423 echo -en '\x23' | dd of=$DISK conv=fsync bs=1 count=1 seek=17908 status=none
424 else
425 vtdebug "This is MBR partition style ..."
426
427 PART1_ACTIVE=$(dd if=$DISK bs=1 count=1 skip=446 status=none | hexdump -n1 -e '1/1 "%02X"')
428 PART2_ACTIVE=$(dd if=$DISK bs=1 count=1 skip=462 status=none | hexdump -n1 -e '1/1 "%02X"')
429
430 vtdebug "PART1_ACTIVE=$PART1_ACTIVE PART2_ACTIVE=$PART2_ACTIVE"
431 if [ "$PART1_ACTIVE" = "00" ] && [ "$PART2_ACTIVE" = "80" ]; then
432 vtdebug "change 1st partition active, 2nd partition inactive ..."
433 echo -en '\x80' | dd of=$DISK conv=fsync bs=1 count=1 seek=446 status=none
434 echo -en '\x00' | dd of=$DISK conv=fsync bs=1 count=1 seek=462 status=none
435 fi
436 xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2047 seek=1
437 fi
438
439 dd status=none conv=fsync if=./rsvdata.bin seek=2040 bs=512 count=8 of=${DISK}
440 rm -f ./rsvdata.bin
441
442 xzcat ./ventoy/ventoy.disk.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=$VENTOY_SECTOR_NUM seek=$part2_start
443
444 sync
445
446 if [ "$SECUREBOOT" != "YES" ]; then
447 mkdir ./tmp_mnt
448
449 vtdebug "mounting part2 ...."
450 for tt in 1 2 3 4 5; do
451 if mount ${PART2} ./tmp_mnt > /dev/null 2>&1; then
452 vtdebug "mounting part2 success"
453 break
454 else
455 vtdebug "mounting part2 failed, now wait and retry..."
456 fi
457 sleep 2
458 done
459
460 rm -f ./tmp_mnt/EFI/BOOT/BOOTX64.EFI
461 rm -f ./tmp_mnt/EFI/BOOT/grubx64.efi
462 rm -f ./tmp_mnt/EFI/BOOT/BOOTIA32.EFI
463 rm -f ./tmp_mnt/EFI/BOOT/grubia32.efi
464 rm -f ./tmp_mnt/EFI/BOOT/MokManager.efi
465 rm -f ./tmp_mnt/EFI/BOOT/mmia32.efi
466 rm -f ./tmp_mnt/ENROLL_THIS_KEY_IN_MOKMANAGER.cer
467 mv ./tmp_mnt/EFI/BOOT/grubx64_real.efi ./tmp_mnt/EFI/BOOT/BOOTX64.EFI
468 mv ./tmp_mnt/EFI/BOOT/grubia32_real.efi ./tmp_mnt/EFI/BOOT/BOOTIA32.EFI
469
470
471 for tt in 1 2 3; do
472 if umount ./tmp_mnt > /dev/null 2>&1; then
473 vtdebug "umount part2 success"
474 rm -rf ./tmp_mnt
475 break
476 else
477 vtdebug "umount part2 failed, now retry..."
478 sleep 1
479 fi
480 done
481 fi
482
483 echo ""
484 vtinfo "Update Ventoy to $DISK successfully finished."
485 echo ""
486
487 fi
488
489