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