]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - INSTALL/tool/VentoyWorker.sh
Update Hindi Translation (#1941)
[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 if 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 enabled)'
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 ' -n try non-destructive installation (only for install)'
20 echo ''
21 }
22
23
24 SECUREBOOT="YES"
25 VTNEW_LABEL='Ventoy'
26 RESERVE_SIZE_MB=0
27 while [ -n "$1" ]; do
28 if [ "$1" = "-i" ]; then
29 MODE="install"
30 elif [ "$1" = "-I" ]; then
31 MODE="install"
32 FORCE="Y"
33 elif [ "$1" = "-n" ]; then
34 NONDESTRUCTIVE="Y"
35 elif [ "$1" = "-u" ]; then
36 MODE="update"
37 elif [ "$1" = "-l" ]; then
38 MODE="list"
39 elif [ "$1" = "-s" ]; then
40 SECUREBOOT="YES"
41 elif [ "$1" = "-S" ]; then
42 SECUREBOOT="NO"
43 elif [ "$1" = "-g" ]; then
44 VTGPT="YES"
45 elif [ "$1" = "-L" ]; then
46 shift
47 VTNEW_LABEL=$1
48 elif [ "$1" = "-r" ]; then
49 RESERVE_SPACE="YES"
50 shift
51 RESERVE_SIZE_MB=$1
52 elif [ "$1" = "-V" ] || [ "$1" = "--version" ]; then
53 exit 0
54 elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
55 print_usage
56 exit 0
57 else
58 if ! [ -b "$1" ]; then
59 vterr "$1 is NOT a valid device"
60 print_usage
61 exit 1
62 fi
63 DISK=$1
64 # Resolve symlinks now, will be needed to look up information about the device in
65 # the /sys/ filesystem, for example /sys/class/block/${DISK#/dev/}/start
66 # The main use case is supporting /dev/disk/by-id/ symlinks instead of raw devices
67 if [ -L "$DISK" ]; then
68 DISK=$(readlink -e -n "$DISK")
69 fi
70 fi
71
72 shift
73 done
74
75 if [ -z "$MODE" ]; then
76 print_usage
77 exit 1
78 fi
79
80 if ! [ -b "$DISK" ]; then
81 vterr "Disk $DISK does not exist"
82 exit 1
83 fi
84
85 if [ -e /sys/class/block/${DISK#/dev/}/start ]; then
86 vterr "$DISK is a partition, please use the whole disk."
87 echo "For example:"
88 vterr " sudo sh Ventoy2Disk.sh -i /dev/sdb1 <=== This is wrong"
89 vtinfo " sudo sh Ventoy2Disk.sh -i /dev/sdb <=== This is right"
90 echo ""
91 exit 1
92 fi
93
94 if [ -n "$RESERVE_SPACE" -a "$MODE" = "install" ]; then
95 if echo $RESERVE_SIZE_MB | grep -q '^[0-9][0-9]*$'; then
96 vtdebug "User will reserve $RESERVE_SIZE_MB MB disk space"
97 else
98 vterr "$RESERVE_SIZE_MB is invalid for reserved space"
99 exit 1
100 fi
101 fi
102
103 vtdebug "MODE=$MODE FORCE=$FORCE RESERVE_SPACE=$RESERVE_SPACE RESERVE_SIZE_MB=$RESERVE_SIZE_MB"
104
105 #check tools
106 if check_tool_work_ok; then
107 vtdebug "check tool work ok"
108 else
109 vterr "Some tools can not run on current system. Please check log.txt for details."
110 exit 1
111 fi
112
113 if [ "$MODE" = "list" ]; then
114 version=$(get_disk_ventoy_version $DISK)
115 if [ $? -eq 0 ]; then
116 echo "Ventoy Version in Disk: $version"
117
118 vtPart1Type=$(dd if=$DISK bs=1 count=1 skip=450 status=none | hexdump -n1 -e '1/1 "%02X"')
119 if [ "$vtPart1Type" = "EE" ]; then
120 echo "Disk Partition Style : GPT"
121 else
122 echo "Disk Partition Style : MBR"
123 fi
124
125 if check_disk_secure_boot $DISK; then
126 echo "Secure Boot Support : YES"
127 else
128 echo "Secure Boot Support : NO"
129 fi
130 else
131 echo "Ventoy Version: NA"
132 fi
133 echo ""
134 exit 0
135 fi
136
137 #check mountpoint
138 check_umount_disk "$DISK"
139
140 if grep "$DISK" /proc/mounts; then
141 vterr "$DISK is already mounted, please umount it first!"
142 exit 1
143 fi
144
145 #check swap partition
146 if swapon --help 2>&1 | grep -q '^ \-s,'; then
147 if swapon -s | grep -q "^${DISK}[0-9]"; then
148 vterr "$DISK is used as swap, please swapoff it first!"
149 exit 1
150 fi
151 fi
152
153 #check access
154 if dd if="$DISK" of=/dev/null bs=1 count=1 >/dev/null 2>&1; then
155 vtdebug "root permission check ok ..."
156 else
157 vterr "Failed to access $DISK, maybe root privilege is needed!"
158 echo ''
159 exit 1
160 fi
161
162
163 #check tmp_mnt directory
164 if [ -d ./tmp_mnt ]; then
165 vtdebug "There is a tmp_mnt directory, now delete it."
166 umount ./tmp_mnt >/dev/null 2>&1
167 rm -rf ./tmp_mnt
168 if [ -d ./tmp_mnt ]; then
169 vterr "tmp_mnt directory exists, please delete it first."
170 exit 1
171 fi
172 fi
173
174
175 if [ "$MODE" = "install" -a -z "$NONDESTRUCTIVE" ]; then
176 vtdebug "install Ventoy ..."
177
178 if [ -n "$VTGPT" ]; then
179 if parted -v > /dev/null 2>&1; then
180 PARTTOOL='parted'
181 else
182 vterr "parted is not found in the system, Ventoy can't create new partitions without it."
183 vterr "You should install \"GNU parted\" first."
184 exit 1
185 fi
186 else
187 if parted -v > /dev/null 2>&1; then
188 PARTTOOL='parted'
189 elif fdisk -v >/dev/null 2>&1; then
190 PARTTOOL='fdisk'
191 else
192 vterr "Both parted and fdisk are not found in the system, Ventoy can't create new partitions."
193 exit 1
194 fi
195 fi
196
197 version=$(get_disk_ventoy_version $DISK)
198 if [ $? -eq 0 ]; then
199 if [ -z "$FORCE" ]; then
200 vtwarn "$DISK already contains a Ventoy with version $version"
201 vtwarn "Use -u option to do a safe upgrade operation."
202 vtwarn "OR if you really want to reinstall Ventoy to $DISK, please use -I option."
203 vtwarn ""
204 exit 1
205 fi
206 fi
207
208 disk_sector_num=$(cat /sys/block/${DISK#/dev/}/size)
209 disk_size_gb=$(expr $disk_sector_num / 2097152)
210
211 if [ $disk_sector_num -gt 4294967296 ] && [ -z "$VTGPT" ]; then
212 vterr "$DISK is over 2TB size, MBR will not work on it."
213 exit 1
214 fi
215
216 if [ -n "$RESERVE_SPACE" ]; then
217 sum_size_mb=$(expr $RESERVE_SIZE_MB + $VENTOY_PART_SIZE_MB)
218 reserve_sector_num=$(expr $sum_size_mb \* 2048)
219
220 if [ $disk_sector_num -le $reserve_sector_num ]; then
221 vterr "Can't reserve $RESERVE_SIZE_MB MB space from $DISK"
222 exit 1
223 fi
224 fi
225
226 #Print disk info
227 echo "Disk : $DISK"
228 parted -s $DISK p 2>&1 | grep Model
229 echo "Size : $disk_size_gb GB"
230 if [ -n "$VTGPT" ]; then
231 echo "Style: GPT"
232 else
233 echo "Style: MBR"
234 fi
235 echo ''
236
237 if [ -n "$RESERVE_SPACE" ]; then
238 echo "You will reserve $RESERVE_SIZE_MB MB disk space "
239 fi
240 echo ''
241
242 vtwarn "Attention:"
243 vtwarn "You will install Ventoy to $DISK."
244 vtwarn "All the data on the disk $DISK will be lost!!!"
245 echo ""
246
247 read -p 'Continue? (y/n) ' Answer
248 if [ "$Answer" != "y" ]; then
249 if [ "$Answer" != "Y" ]; then
250 exit 0
251 fi
252 fi
253
254 echo ""
255 vtwarn "All the data on the disk $DISK will be lost!!!"
256 read -p 'Double-check. Continue? (y/n) ' Answer
257 if [ "$Answer" != "y" ]; then
258 if [ "$Answer" != "Y" ]; then
259 exit 0
260 fi
261 fi
262
263 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
264 vterr "No enough space in disk $DISK"
265 exit 1
266 fi
267
268 # check and umount
269 check_umount_disk "$DISK"
270
271 if ! dd if=/dev/zero of=$DISK bs=64 count=512 status=none conv=fsync; then
272 vterr "Write data to $DISK failed, please check whether it's in use."
273 exit 1
274 fi
275
276 if [ -n "$VTGPT" ]; then
277 vtdebug "format_ventoy_disk_gpt $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
278 format_ventoy_disk_gpt $RESERVE_SIZE_MB $DISK $PARTTOOL
279 else
280 vtdebug "format_ventoy_disk_mbr $RESERVE_SIZE_MB $DISK $PARTTOOL ..."
281 format_ventoy_disk_mbr $RESERVE_SIZE_MB $DISK $PARTTOOL
282 fi
283
284 # format part1
285
286 # DiskSize > 32GB Cluster Size use 128KB
287 # DiskSize < 32GB Cluster Size use 32KB
288 if [ $disk_size_gb -gt 32 ]; then
289 cluster_sectors=256
290 else
291 cluster_sectors=64
292 fi
293
294 PART1=$(get_disk_part_name $DISK 1)
295 PART2=$(get_disk_part_name $DISK 2)
296
297 #clean part2
298 dd status=none conv=fsync if=/dev/zero of=$DISK bs=512 count=32 seek=$part2_start_sector
299
300 #format part1
301 wait_and_create_part ${PART1} ${PART2}
302 if [ -b ${PART1} ]; then
303 vtinfo "Format partition 1 ${PART1} ..."
304 mkexfatfs -n "$VTNEW_LABEL" -s $cluster_sectors ${PART1}
305 if [ $? -ne 0 ]; then
306 echo "mkexfatfs failed, now retry..."
307 mkexfatfs -n "$VTNEW_LABEL" -s $cluster_sectors ${PART1}
308 if [ $? -ne 0 ]; then
309 echo "######### mkexfatfs failed, exit ########"
310 exit 1
311 fi
312 else
313 echo "mkexfatfs success"
314 fi
315 else
316 vterr "${PART1} NOT exist"
317 fi
318
319 vtinfo "writing data to disk ..."
320 dd status=none conv=fsync if=./boot/boot.img of=$DISK bs=1 count=446
321
322 if [ -n "$VTGPT" ]; then
323 echo -en '\x22' | dd status=none of=$DISK conv=fsync bs=1 count=1 seek=92
324 xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2014 seek=34
325 echo -en '\x23' | dd of=$DISK conv=fsync bs=1 count=1 seek=17908 status=none
326 else
327 xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2047 seek=1
328 fi
329
330 # check and umount
331 check_umount_disk "$DISK"
332
333 xzcat ./ventoy/ventoy.disk.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=$VENTOY_SECTOR_NUM seek=$part2_start_sector
334
335 #test UUID
336 testUUIDStr=$(vtoy_gen_uuid | hexdump -C)
337 vtdebug "test uuid: $testUUIDStr"
338
339 #disk uuid
340 vtoy_gen_uuid | dd status=none conv=fsync of=${DISK} seek=384 bs=1 count=16
341
342 #disk signature
343 vtoy_gen_uuid | dd status=none conv=fsync of=${DISK} skip=12 seek=440 bs=1 count=4
344
345 vtinfo "sync data ..."
346 sync
347
348 vtinfo "esp partition processing ..."
349
350 if [ "$SECUREBOOT" != "YES" ]; then
351 sleep 2
352 check_umount_disk "$DISK"
353 vtoycli partresize -s $DISK $part2_start_sector
354 fi
355
356 echo ""
357 vtinfo "Install Ventoy to $DISK successfully finished."
358 echo ""
359
360 elif [ "$MODE" = "install" -a -n "$NONDESTRUCTIVE" ]; then
361 vtdebug "non-destructive install Ventoy ..."
362
363 version=$(get_disk_ventoy_version $DISK)
364 if [ $? -eq 0 ]; then
365 if [ -z "$FORCE" ]; then
366 vtwarn "$DISK already contains a Ventoy with version $version."
367 vtwarn "You can not do and don not need non-destructive installation."
368 vtwarn ""
369 exit 1
370 fi
371 fi
372
373 disk_sector_num=$(cat /sys/block/${DISK#/dev/}/size)
374 disk_size_gb=$(expr $disk_sector_num / 2097152)
375
376 if vtoycli partresize -t $DISK; then
377 OldStyle="GPT"
378 else
379 OldStyle="MBR"
380 fi
381
382 #Print disk info
383 echo "Disk : $DISK"
384 parted -s $DISK p 2>&1 | grep Model
385 echo "Size : $disk_size_gb GB"
386 echo "Style: $OldStyle"
387 echo ''
388
389 vtwarn "Attention:"
390 vtwarn "Ventoy will try non-destructive installation on $DISK if possible."
391 echo ""
392
393 read -p 'Continue? (y/n) ' Answer
394 if [ "$Answer" != "y" ]; then
395 if [ "$Answer" != "Y" ]; then
396 exit 0
397 fi
398 fi
399
400 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
401 vterr "No enough space in disk $DISK"
402 exit 1
403 fi
404
405 PART1=$(get_disk_part_name $DISK 1)
406 PART2=$(get_disk_part_name $DISK 2)
407
408 #Part1 size in MB aligned with 4KB
409 PART1_SECTORS=$(cat /sys/class/block/${PART1#/dev/}/size)
410 PART1_4K=$(expr $PART1_SECTORS / 8)
411 PART1_MB=$(expr $PART1_4K / 256)
412 PART1_NEW_MB=$(expr $PART1_MB - 32)
413
414 echo "$PART1 is ${PART1_MB}MB"
415
416 #check partition layout
417 echo "check partition layout ..."
418 vtoycli partresize -c $DISK
419 vtRet=$?
420 if [ $vtRet -eq 0 ]; then
421 exit 1
422 else
423 # check and umount
424 check_umount_disk "$DISK"
425 sleep 1
426 check_umount_disk "$DISK"
427
428 if [ $vtRet -eq 1 ]; then
429 echo "Free space enough, start install..."
430 part2_start_sector=$(expr $PART1_SECTORS + 2048)
431 elif [ $vtRet -eq 2 ]; then
432 echo "We need to shrink partition 1 firstly ..."
433
434 PART1_BLKID=$(blkid $PART1)
435 blkid $PART1
436
437 if echo $PART1_BLKID | egrep -q -i 'TYPE=ntfs|TYPE=.ntfs'; then
438 echo "Partition 1 contains NTFS filesystem"
439
440 which ntfsresize
441 if [ $? -ne 0 ]; then
442 echo "###[FAIL] ntfsresize not found. Please install ntfs-3g package."
443 exit 1
444 fi
445
446 echo "ntfsfix -b -d $PART1 ..."
447 ntfsfix -b -d $PART1
448
449 echo "ntfsresize --size ${PART1_NEW_MB}Mi $PART1 ..."
450 ntfsresize -f --size ${PART1_NEW_MB}Mi $PART1
451 if [ $? -ne 0 ]; then
452 echo "###[FAIL] ntfsresize failed."
453 exit 1
454 fi
455 elif echo $PART1_BLKID | egrep -q -i 'TYPE=ext[2-4]|TYPE=.ext[2-4]'; then
456 echo "Partition 1 contains EXT filesystem"
457
458 which resize2fs
459 if [ $? -ne 0 ]; then
460 echo "###[FAIL] resize2fs not found. Please install e2fsprogs package."
461 exit 1
462 fi
463
464 echo "e2fsck -f $PART1 ..."
465 e2fsck -f $PART1
466
467 echo "resize2fs $PART1 ${PART1_NEW_MB}M ..."
468 resize2fs $PART1 ${PART1_NEW_MB}M
469 if [ $? -ne 0 ]; then
470 echo "###[FAIL] resize2fs failed."
471 exit 1
472 fi
473 else
474 echo "###[FAIL] Unsupported filesystem in partition 1."
475 exit 1
476 fi
477
478 sync
479 PART1_NEW_END_MB=$(expr $PART1_NEW_MB + 1)
480 part2_start_sector=$(expr $PART1_NEW_END_MB \* 2048)
481 fi
482 fi
483
484 vtinfo "writing data to disk part2_start=$part2_start_sector ..."
485
486 dd status=none conv=fsync if=./boot/boot.img of=$DISK bs=1 count=440
487
488 if [ "$OldStyle" = "GPT" ]; then
489 echo -en '\x22' | dd status=none of=$DISK conv=fsync bs=1 count=1 seek=92
490 xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2014 seek=34
491 echo -en '\x23' | dd of=$DISK conv=fsync bs=1 count=1 seek=17908 status=none
492 else
493 xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2047 seek=1
494 fi
495
496 xzcat ./ventoy/ventoy.disk.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=$VENTOY_SECTOR_NUM seek=$part2_start_sector
497
498 #test UUID
499 testUUIDStr=$(vtoy_gen_uuid | hexdump -C)
500 vtdebug "test uuid: $testUUIDStr"
501
502 #disk uuid
503 vtoy_gen_uuid | dd status=none conv=fsync of=${DISK} seek=384 bs=1 count=16
504
505 vtinfo "sync data ..."
506 sync
507
508 vtinfo "esp partition processing ..."
509 if [ "$SECUREBOOT" != "YES" ]; then
510 vtoycli partresize -s $DISK $part2_start_sector
511 fi
512
513 vtinfo "update partition table $DISK $part2_start_sector ..."
514 vtoycli partresize -p $DISK $part2_start_sector
515 if [ $? -eq 0 ]; then
516 sync
517 echo ""
518 vtinfo "Ventoy non-destructive installation on $DISK successfully finished."
519 echo ""
520 else
521 echo ""
522 vterr "Ventoy non-destructive installation on $DISK failed."
523 echo ""
524 fi
525
526 else
527 vtdebug "update Ventoy ..."
528
529 oldver=$(get_disk_ventoy_version $DISK)
530 if [ $? -ne 0 ]; then
531 if is_disk_contains_ventoy $DISK; then
532 oldver="Unknown"
533 else
534 vtwarn "$DISK does not contain Ventoy or data corrupted"
535 echo ""
536 vtwarn "Please use -i option if you want to install ventoy to $DISK"
537 echo ""
538 exit 1
539 fi
540 fi
541
542 #reserve secure boot option
543 if [ -z "$SECUREBOOT" ]; then
544 if check_disk_secure_boot $DISK; then
545 SECUREBOOT="YES"
546 else
547 SECUREBOOT="NO"
548 fi
549 fi
550
551 curver=$(cat ./ventoy/version)
552
553 vtinfo "Upgrade operation is safe, all the data in the 1st partition (iso files and other) will be unchanged!"
554 echo ""
555
556 read -p "Update Ventoy $oldver ===> $curver Continue? (y/n) " Answer
557 if [ "$Answer" != "y" ]; then
558 if [ "$Answer" != "Y" ]; then
559 exit 0
560 fi
561 fi
562
563 PART2=$(get_disk_part_name $DISK 2)
564 SHORT_PART2=${PART2#/dev/}
565 part2_start=$(cat /sys/class/block/$SHORT_PART2/start)
566
567 PART1_TYPE=$(dd if=$DISK bs=1 count=1 skip=450 status=none | hexdump -n1 -e '1/1 "%02X"')
568
569 #reserve disk uuid
570 rm -f ./diskuuid.bin
571 dd status=none conv=fsync if=${DISK} skip=384 bs=1 count=16 of=./diskuuid.bin
572
573 dd status=none conv=fsync if=./boot/boot.img of=$DISK bs=1 count=440
574 dd status=none conv=fsync if=./diskuuid.bin of=$DISK bs=1 count=16 seek=384
575 rm -f ./diskuuid.bin
576
577 #reserve data
578 rm -f ./rsvdata.bin
579 dd status=none conv=fsync if=${DISK} skip=2040 bs=512 count=8 of=./rsvdata.bin
580
581 if [ "$PART1_TYPE" = "EE" ]; then
582 vtdebug "This is GPT partition style ..."
583 echo -en '\x22' | dd status=none of=$DISK conv=fsync bs=1 count=1 seek=92
584 xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2014 seek=34
585 echo -en '\x23' | dd of=$DISK conv=fsync bs=1 count=1 seek=17908 status=none
586 else
587 vtdebug "This is MBR partition style ..."
588
589 PART1_ACTIVE=$(dd if=$DISK bs=1 count=1 skip=446 status=none | hexdump -n1 -e '1/1 "%02X"')
590 PART2_ACTIVE=$(dd if=$DISK bs=1 count=1 skip=462 status=none | hexdump -n1 -e '1/1 "%02X"')
591
592 vtdebug "PART1_ACTIVE=$PART1_ACTIVE PART2_ACTIVE=$PART2_ACTIVE"
593 if [ "$PART1_ACTIVE" = "00" ] && [ "$PART2_ACTIVE" = "80" ]; then
594 vtdebug "change 1st partition active, 2nd partition inactive ..."
595 echo -en '\x80' | dd of=$DISK conv=fsync bs=1 count=1 seek=446 status=none
596 echo -en '\x00' | dd of=$DISK conv=fsync bs=1 count=1 seek=462 status=none
597 fi
598 xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2047 seek=1
599 fi
600
601 dd status=none conv=fsync if=./rsvdata.bin seek=2040 bs=512 count=8 of=${DISK}
602 rm -f ./rsvdata.bin
603
604 check_umount_disk "$DISK"
605
606 xzcat ./ventoy/ventoy.disk.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=$VENTOY_SECTOR_NUM seek=$part2_start
607 sync
608
609 vtinfo "esp partition processing ..."
610 if [ "$SECUREBOOT" != "YES" ]; then
611 sleep 2
612 check_umount_disk "$DISK"
613 vtoycli partresize -s $DISK $part2_start
614 fi
615
616 echo ""
617 vtinfo "Update Ventoy on $DISK successfully finished."
618 echo ""
619
620 fi
621
622