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