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