]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - INSTALL/Ventoy2Disk.sh
1.0.08beta2
[Ventoy.git] / INSTALL / Ventoy2Disk.sh
1 #!/bin/sh
2
3 OLDDIR=$PWD
4
5 if ! [ -f ./tool/ventoy_lib.sh ]; then
6 cd ${0%Ventoy2Disk.sh}
7 fi
8
9 . ./tool/ventoy_lib.sh
10
11 print_usage() {
12 echo 'Usage: VentoyInstaller.sh OPTION /dev/sdX'
13 echo ' OPTION:'
14 echo ' -i install ventoy to sdX (fail if disk already installed with ventoy)'
15 echo ' -u update ventoy in sdX'
16 echo ' -I force install ventoy to sdX (no matter installed or not)'
17 echo ''
18 }
19
20 echo ''
21 echo '***********************************************************'
22 echo '* Ventoy2Disk Script *'
23 echo '* longpanda admin@ventoy.net *'
24 echo '***********************************************************'
25 echo ''
26
27 vtdebug "############# Ventoy2Disk $0 ################"
28
29 if [ "$1" = "-i" ]; then
30 MODE="install"
31 elif [ "$1" = "-I" ]; then
32 MODE="install"
33 FORCE="Y"
34 elif [ "$1" = "-u" ]; then
35 MODE="update"
36 else
37 print_usage
38 cd $OLDDIR
39 exit 1
40 fi
41
42 if ! [ -b "$2" ]; then
43 print_usage
44 cd $OLDDIR
45 exit 1
46 fi
47
48 if [ -z "$SUDO_USER" ]; then
49 if [ "$USER" != "root" ]; then
50 vterr "EUID is $EUID root permission is required."
51 echo ''
52 cd $OLDDIR
53 exit 1
54 fi
55 fi
56
57 vtdebug "MODE=$MODE FORCE=$FORCE"
58
59 #decompress tool
60 cd tool
61 chmod +x ./xzcat
62 for file in $(ls); do
63 if [ "$file" != "xzcat" ]; then
64 if [ "$file" != "ventoy_lib.sh" ]; then
65 ./xzcat $file > ${file%.xz}
66 chmod +x ${file%.xz}
67 fi
68 fi
69 done
70 cd ../
71
72 if ! check_tool_work_ok; then
73 vterr "Some tools can not run in current system. Please check log.txt for detail."
74 cd $OLDDIR
75 exit 1
76 fi
77
78
79 DISK=$2
80
81 if ! [ -b "$DISK" ]; then
82 vterr "Disk $DISK does not exist"
83 cd $OLDDIR
84 exit 1
85 fi
86
87
88 if [ -e /sys/class/block/${DISK#/dev/}/start ]; then
89 vterr "$DISK is a partition, please use the whole disk"
90 cd $OLDDIR
91 exit 1
92 fi
93
94 grep "^$DISK" /proc/mounts | while read mtline; do
95 mtpnt=$(echo $mtline | awk '{print $2}')
96 vtdebug "Trying to umount $mtpnt ..."
97 umount $mtpnt >/dev/null 2>&1
98 done
99
100 if grep "$DISK" /proc/mounts; then
101 vterr "$DISK is already mounted, please umount it first!"
102 cd $OLDDIR
103 exit 1
104 fi
105
106
107 if [ "$MODE" = "install" ]; then
108 vtdebug "install ventoy ..."
109
110 if ! fdisk -v >/dev/null 2>&1; then
111 vterr "fdisk is needed by ventoy installation, but is not found in the system."
112 cd $OLDDIR
113 exit 1
114 fi
115
116 version=$(get_disk_ventoy_version $DISK)
117 if [ $? -eq 0 ]; then
118 if [ -z "$FORCE" ]; then
119 vtwarn "$DISK already contains a Ventoy with version $version"
120 vtwarn "Use -u option to do a safe upgrade operation."
121 vtwarn "OR if you really want to reinstall ventoy to $DISK, please use -I option."
122 vtwarn ""
123 cd $OLDDIR
124 exit 1
125 fi
126 fi
127
128 disk_sector_num=$(cat /sys/block/${DISK#/dev/}/size)
129 disk_size_gb=$(expr $disk_sector_num / 2097152)
130
131 if [ $disk_sector_num -gt 4294967296 ]; then
132 vterr "$DISK is over 2TB size, MBR will not work on it."
133 cd $OLDDIR
134 exit 1
135 fi
136
137 #Print disk info
138 echo "Disk : $DISK"
139 parted -s $DISK p 2>&1 | grep Model
140 echo "Size : $disk_size_gb GB"
141 echo ''
142
143 vtwarn "Attention:"
144 vtwarn "You will install Ventoy to $DISK."
145 vtwarn "All the data on the disk $DISK will be lost!!!"
146 echo ""
147
148 read -p 'Continue? (y/n)' Answer
149 if [ "$Answer" != "y" ]; then
150 if [ "$Answer" != "Y" ]; then
151 exit 0
152 fi
153 fi
154
155 echo ""
156 vtwarn "All the data on the disk $DISK will be lost!!!"
157 read -p 'Double-check. Continue? (y/n)' Answer
158 if [ "$Answer" != "y" ]; then
159 if [ "$Answer" != "Y" ]; then
160 exit 0
161 fi
162 fi
163
164
165 if [ $disk_sector_num -le $VENTOY_SECTOR_NUM ]; then
166 vterr "No enough space in disk $DISK"
167 exit 1
168 fi
169
170 if ! dd if=/dev/zero of=$DISK bs=1 count=512 status=none conv=fsync; then
171 vterr "Write data to $DISK failed, please check whether it's in use."
172 exit 1
173 fi
174
175 format_ventoy_disk $DISK
176
177 # format part1
178 if ventoy_is_linux64; then
179 cmd=./tool/mkexfatfs_64
180 else
181 cmd=./tool/mkexfatfs_32
182 fi
183
184 chmod +x ./tool/*
185
186 # DiskSize > 32GB Cluster Size use 128KB
187 # DiskSize < 32GB Cluster Size use 32KB
188 if [ $disk_size_gb -gt 32 ]; then
189 cluster_sectors=256
190 else
191 cluster_sectors=64
192 fi
193
194 $cmd -n ventoy -s $cluster_sectors ${DISK}1
195
196 chmod +x ./tool/vtoy_gen_uuid
197
198 dd status=none conv=fsync if=./boot/boot.img of=$DISK bs=1 count=446
199 ./tool/xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2047 seek=1
200 ./tool/xzcat ./ventoy/ventoy.disk.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=$VENTOY_SECTOR_NUM seek=$part2_start_sector
201
202 #disk uuid
203 ./tool/vtoy_gen_uuid | dd status=none conv=fsync of=${DISK} seek=384 bs=1 count=16
204
205 #disk signature
206 ./tool/vtoy_gen_uuid | dd status=none conv=fsync of=${DISK} skip=12 seek=440 bs=1 count=4
207
208 sync
209
210 echo ""
211 vtinfo "Install Ventoy to $DISK successfully finished."
212 echo ""
213
214 else
215 vtdebug "update ventoy ..."
216
217 oldver=$(get_disk_ventoy_version $DISK)
218 if [ $? -ne 0 ]; then
219 vtwarn "$DISK does not contain ventoy or data corupted"
220 echo ""
221 vtwarn "Please use -i option if you want to install ventoy to $DISK"
222 echo ""
223 cd $OLDDIR
224 exit 1
225 fi
226
227 curver=$(cat ./ventoy/version)
228
229 vtinfo "Upgrade operation is safe, all the data in the 1st partition (iso files and other) will be unchanged!"
230 echo ""
231
232 read -p "Update Ventoy $oldver ===> $curver Continue? (y/n)" Answer
233 if [ "$Answer" != "y" ]; then
234 if [ "$Answer" != "Y" ]; then
235 cd $OLDDIR
236 exit 0
237 fi
238 fi
239
240 PART2=$(get_disk_part_name $DISK 2)
241
242 dd status=none conv=fsync if=./boot/boot.img of=$DISK bs=1 count=440
243
244 ./tool/xzcat ./boot/core.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=2047 seek=1
245
246 disk_sector_num=$(cat /sys/block/${DISK#/dev/}/size)
247 part2_start=$(expr $disk_sector_num - $VENTOY_SECTOR_NUM)
248 ./tool/xzcat ./ventoy/ventoy.disk.img.xz | dd status=none conv=fsync of=$DISK bs=512 count=$VENTOY_SECTOR_NUM seek=$part2_start
249
250 sync
251
252 echo ""
253 vtinfo "Update Ventoy to $DISK successfully finished."
254 echo ""
255
256 fi
257
258 cd $OLDDIR
259