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