]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - INSTALL/tool/VentoyWorker.sh
Fix the missing program issue for EasyU
[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 (fails 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 on current system. Please check log.txt for details."
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 check_umount_disk "$DISK"
129
130 if grep "$DISK" /proc/mounts; then
131 vterr "$DISK is already mounted, please umount it first!"
132 exit 1
133 fi
134
135 #check swap partition
136 if swapon --help 2>&1 | grep -q '^ \-s,'; then
137 if swapon -s | grep -q "^${DISK}[0-9]"; then
138 vterr "$DISK is used as swap, please swapoff it first!"
139 exit 1
140 fi
141 fi
142
143 #check access
144 if dd if="$DISK" of=/dev/null bs=1 count=1 >/dev/null 2>&1; then
145 vtdebug "root permission check ok ..."
146 else
147 vterr "Failed to access $DISK, maybe root privilege is needed!"
148 echo ''
149 exit 1
150 fi
151
152
153 #check tmp_mnt directory
154 if [ -d ./tmp_mnt ]; then
155 vtdebug "There is a tmp_mnt directory, now delete it."
156 umount ./tmp_mnt >/dev/null 2>&1
157 rm -rf ./tmp_mnt
158 if [ -d ./tmp_mnt ]; then
159 vterr "tmp_mnt directory exits, please delete it first."
160 exit 1
161 fi
162 fi
163
164
165 if [ "$MODE" = "install" ]; then
166 vtdebug "install Ventoy ..."
167
168 if [ -n "$VTGPT" ]; then
169 if parted -v > /dev/null 2>&1; then
170 PARTTOOL='parted'
171 else
172 vterr "parted is not found in the system, Ventoy can't create new partitions without it."
173 vterr "You should install \"GNU parted\" first."
174 exit 1
175 fi
176 else
177 if parted -v > /dev/null 2>&1; then
178 PARTTOOL='parted'
179 elif fdisk -v >/dev/null 2>&1; then
180 PARTTOOL='fdisk'
181 else
182 vterr "Both parted and fdisk are not found in the system, Ventoy can't create new partitions."
183 exit 1
184 fi
185 fi
186
187 version=$(get_disk_ventoy_version $DISK)
188 if [ $? -eq 0 ]; then
189 if [ -z "$FORCE" ]; then
190 vtwarn "$DISK already contains a Ventoy with version $version"
191 vtwarn "Use -u option to do a safe upgrade operation."
192 vtwarn "OR if you really want to reinstall Ventoy to $DISK, please use -I option."
193 vtwarn ""
194 exit 1
195 fi
196 fi
197
198 disk_sector_num=$(cat /sys/block/${DISK#/dev/}/size)
199 disk_size_gb=$(expr $disk_sector_num / 2097152)
200
201 if [ $disk_sector_num -gt 4294967296 ] && [ -z "$VTGPT" ]; then
202 vterr "$DISK is over 2TB size, MBR will not work on it."
203 exit 1
204 fi
205
206 if [ -n "$RESERVE_SPACE" ]; then
207 sum_size_mb=$(expr $RESERVE_SIZE_MB + $VENTOY_PART_SIZE_MB)
208 reserve_sector_num=$(expr $sum_size_mb \* 2048)
209
210 if [ $disk_sector_num -le $reserve_sector_num ]; then
211 vterr "Can't reserve $RESERVE_SIZE_MB MB space from $DISK"
212 exit 1
213 fi
214 fi
215
216 #Print disk info
217 echo "Disk : $DISK"
218 parted -s $DISK p 2>&1 | grep Model
219 echo "Size : $disk_size_gb GB"
220 if [ -n "$VTGPT" ]; then
221 echo "Style: GPT"
222 else
223 echo "Style: MBR"
224 fi
225 echo ''
226
227 if [ -n "$RESERVE_SPACE" ]; then
228 echo "You will reserve $RESERVE_SIZE_MB MB disk space "
229 fi
230 echo ''
231
232 vtwarn "Attention:"
233 vtwarn "You will install Ventoy to $DISK."
234 vtwarn "All the data on the disk $DISK will be lost!!!"
235 echo ""
236
237 read -p 'Continue? (y/n) ' Answer
238 if [ "$Answer" != "y" ]; then
239 if [ "$Answer" != "Y" ]; then
240 exit 0
241 fi
242 fi
243
244 echo ""
245 vtwarn "All the data on the disk $DISK will be lost!!!"
246 read -p 'Double-check. Continue? (y/n) ' Answer
247 if [ "$Answer" != "y" ]; then
248 if [ "$Answer" != "Y" ]; then
249 exit 0
250 fi
251 fi
252
253 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
254 vterr "No enough space in disk $DISK"
255 exit 1
256 fi
257
258 if ! dd if=/dev/zero of=$DISK bs=1 count=512 status=none conv=fsync; then
259 vterr "Write data to $DISK failed, please check whether it's in use."
260 exit 1
261 fi
262
263 if [ -n "$VTGPT" ]; then
264 vtdebug "format_ventoy_disk_gpt $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
265 format_ventoy_disk_gpt $RESERVE_SIZE_MB $DISK $PARTTOOL
266 else
267 vtdebug "format_ventoy_disk_mbr $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
268 format_ventoy_disk_mbr $RESERVE_SIZE_MB $DISK $PARTTOOL
269 fi
270
271 # format part1
272
273 # DiskSize > 32GB Cluster Size use 128KB
274 # DiskSize < 32GB Cluster Size use 32KB
275 if [ $disk_size_gb -gt 32 ]; then
276 cluster_sectors=256
277 else
278 cluster_sectors=64
279 fi
280
281 PART1=$(get_disk_part_name $DISK 1)
282 PART2=$(get_disk_part_name $DISK 2)
283
284 mkexfatfs -n "$VTNEW_LABEL" -s $cluster_sectors ${PART1}
285
286 vtinfo "writing data to disk ..."
287
288 dd status=none conv=fsync if=./boot/boot.img of=$DISK bs=1 count=446
289
290 if [ -n "$VTGPT" ]; then
291 echo -en '\x22' | dd status=none of=$DISK conv=fsync bs=1 count=1 seek=92
292 xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2014 seek=34
293 echo -en '\x23' | dd of=$DISK conv=fsync bs=1 count=1 seek=17908 status=none
294 else
295 xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2047 seek=1
296 fi
297
298 # check and umount
299 check_umount_disk "$DISK"
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 check_umount_disk "$DISK"
320
321 if [ "$SECUREBOOT" != "YES" ]; then
322 mkdir ./tmp_mnt
323
324 vtdebug "mounting part2 ...."
325 for tt in 1 2 3 4 5; do
326 if mount ${PART2} ./tmp_mnt > /dev/null 2>&1; then
327 vtdebug "mounting part2 success"
328 break
329 fi
330
331 check_umount_disk "$DISK"
332 sleep 2
333 done
334
335 rm -f ./tmp_mnt/EFI/BOOT/BOOTX64.EFI
336 rm -f ./tmp_mnt/EFI/BOOT/grubx64.efi
337 rm -f ./tmp_mnt/EFI/BOOT/BOOTIA32.EFI
338 rm -f ./tmp_mnt/EFI/BOOT/grubia32.efi
339 rm -f ./tmp_mnt/EFI/BOOT/MokManager.efi
340 rm -f ./tmp_mnt/EFI/BOOT/mmia32.efi
341 rm -f ./tmp_mnt/ENROLL_THIS_KEY_IN_MOKMANAGER.cer
342 mv ./tmp_mnt/EFI/BOOT/grubx64_real.efi ./tmp_mnt/EFI/BOOT/BOOTX64.EFI
343 mv ./tmp_mnt/EFI/BOOT/grubia32_real.efi ./tmp_mnt/EFI/BOOT/BOOTIA32.EFI
344
345 sync
346
347 for tt in 1 2 3; do
348 if umount ./tmp_mnt; then
349 vtdebug "umount part2 success"
350 rm -rf ./tmp_mnt
351 break
352 else
353 vtdebug "umount part2 failed, now retry..."
354 sleep 1
355 fi
356 done
357 fi
358
359 echo ""
360 vtinfo "Install Ventoy to $DISK successfully finished."
361 echo ""
362
363 else
364 vtdebug "update Ventoy ..."
365
366 oldver=$(get_disk_ventoy_version $DISK)
367 if [ $? -ne 0 ]; then
368 if is_disk_contains_ventoy $DISK; then
369 oldver="Unknown"
370 else
371 vtwarn "$DISK does not contain Ventoy or data corrupted"
372 echo ""
373 vtwarn "Please use -i option if you want to install ventoy to $DISK"
374 echo ""
375 exit 1
376 fi
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 check_umount_disk "$DISK"
442
443 xzcat ./ventoy/ventoy.disk.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=$VENTOY_SECTOR_NUM seek=$part2_start
444
445 sync
446
447 if [ "$SECUREBOOT" != "YES" ]; then
448 mkdir ./tmp_mnt
449
450 vtdebug "mounting part2 ...."
451 for tt in 1 2 3 4 5; do
452 check_umount_disk "$DISK"
453
454 if mount ${PART2} ./tmp_mnt > /dev/null 2>&1; then
455 vtdebug "mounting part2 success"
456 break
457 else
458 vtdebug "mounting part2 failed, now wait and retry..."
459 fi
460 sleep 2
461 done
462
463 rm -f ./tmp_mnt/EFI/BOOT/BOOTX64.EFI
464 rm -f ./tmp_mnt/EFI/BOOT/grubx64.efi
465 rm -f ./tmp_mnt/EFI/BOOT/BOOTIA32.EFI
466 rm -f ./tmp_mnt/EFI/BOOT/grubia32.efi
467 rm -f ./tmp_mnt/EFI/BOOT/MokManager.efi
468 rm -f ./tmp_mnt/EFI/BOOT/mmia32.efi
469 rm -f ./tmp_mnt/ENROLL_THIS_KEY_IN_MOKMANAGER.cer
470 mv ./tmp_mnt/EFI/BOOT/grubx64_real.efi ./tmp_mnt/EFI/BOOT/BOOTX64.EFI
471 mv ./tmp_mnt/EFI/BOOT/grubia32_real.efi ./tmp_mnt/EFI/BOOT/BOOTIA32.EFI
472
473 sync
474
475 for tt in 1 2 3; do
476 if umount ./tmp_mnt > /dev/null 2>&1; then
477 vtdebug "umount part2 success"
478 rm -rf ./tmp_mnt
479 break
480 else
481 vtdebug "umount part2 failed, now retry..."
482 sleep 1
483 fi
484 done
485 fi
486
487 echo ""
488 vtinfo "Update Ventoy on $DISK successfully finished."
489 echo ""
490
491 fi
492
493