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