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