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