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