OSDN Git Service

1-install: find EFI system partition smarter
[android-x86/bootable-newinstaller.git] / install / scripts / 1-install
1 #
2 # By Chih-Wei Huang <cwhuang@linux.org.tw>
3 # Last updated 2018/01/16
4 #
5 # License: GNU Public License
6 # We explicitely grant the right to use the scripts
7 # with Android-x86 project.
8 #
9
10 tempfile=/tmp/temp-$$
11 menufile=/tmp/menu-$$
12
13 CPIO=cpio
14
15 rebooting()
16 {
17         dialog --title " Rebooting... " --nocancel --pause "" 8 41 1
18         sync
19         umount -a
20         reboot -f
21 }
22
23 choose()
24 {
25         dialog --clear --title " $1 " \
26                 --menu "$2" 21 79 13 --file $menufile 2> $tempfile
27
28         retval=$?
29         choice=`cat $tempfile`
30 }
31
32 size_gb()
33 {
34         printf %0.2fGB $(dc `cat $1/size` 2097152 / p)
35 }
36
37 find_partition()
38 {
39         grep -H ^$2$ /sys/block/$1/*/partition 2> /dev/null | cut -d/ -f5
40 }
41
42 list_disks()
43 {
44         for b in /sys/block/[shv]d[a-z] /sys/block/mmcblk? /sys/block/nvme*; do
45                 [ -d $b ] && echo $b
46         done
47 }
48
49 partition_drive()
50 {
51         echo -n > $menufile
52         for i in `list_disks`; do
53                 echo -n `basename $i` >> $menufile
54                 if [ -f $i/removable -a `cat $i/removable` -eq 0 ]; then
55                         echo -n ' "Harddisk ' >> $menufile
56                 else
57                         echo -n ' "Removable' >> $menufile
58                 fi
59                 if [ -f $i/size ]; then
60                         sz=$(size_gb $i)
61                         [ "$sz" = "0.00GB" ] && sz="<0.01GB"
62                         printf " %10s" $sz >> $menufile
63                 fi
64                 for f in $i/device/model $i/*/name; do
65                         [ -e $f ] && echo -n " `sed $'s/\x04//g' $f`" >> $menufile && break
66                 done
67                 [ "`basename $i`" = "$booted_from" -o -d $i/$booted_from ] && echo -n " *" >> $menufile
68                 echo '"' >> $menufile
69         done
70         count=`wc -l $menufile | awk '{ print $1 }'`
71         if [ $count -eq 0 ]; then
72                 dialog --title " Error " --msgbox \
73                         "\nOK. There is no hard drive to edit partitions." 8 49
74                 return 255
75         fi
76         if [ $count -eq 1 ]; then
77                 choice=`awk '{ print $1 }' $menufile`
78                 retval=0
79         else
80                 choose "Choose Drive" "Please select a drive to edit partitions:\n\n* - Installer source"
81         fi
82         if [ $retval -eq 0 ]; then
83                 dialog --title " Confirm " --defaultno --yesno "\n Do you want to use GPT?" 7 29
84                 if [ $? -eq 0 ]; then
85                         cgdisk /dev/$choice
86                 else
87                         cfdisk /dev/$choice
88                 fi
89                 if [ $? -eq 0 ]; then
90                         retval=1
91                 else
92                         retval=255
93                 fi
94         fi
95         return $retval
96 }
97
98 select_dev()
99 {
100         blkid | grep -v -E "^/dev/block/|^/dev/loop" | cut -b6- | sort | awk '{
101                 l=""
102                 t="unknown"
103                 sub(/:/, "", $1)
104                 for (i = NF; i > 1; --i)
105                         if (match($i, "^TYPE")) {
106                                 t=$i
107                                 gsub(/TYPE=|"/, "", t)
108                         } else if (match($i, "^LABEL")) {
109                                 l=$i
110                                 gsub(/LABEL=|"/, "", l)
111                         }
112                 printf("%-11s%-12s%-18s\n", $1, t, l)
113         }' > $tempfile
114
115         for d in `ls /sys/block`; do
116                 for i in /sys/block/$d/$d*; do
117                         [ -d $i ] || continue
118                         echo $i | grep -qE "loop|ram|sr|boot|rpmb" && continue
119                         f=$(grep "`basename $i`" $tempfile || printf "%-11s%-29s" `basename $i` unknown)
120                         sz=$(size_gb $i)
121                         [ "$sz" = "0.00GB" ] || printf "$f%10s\n" $sz
122                 done
123         done | awk -v b=$booted_from '{
124                 if (!match($1, b)) {
125                         printf("\"%s\" \"", $0)
126                         system("cd /sys/block/*/"$1"; for f in ../device/model ../device/name; do [ -e $f ] && printf %-17s \"`cat $f`\" && break; done")
127                         printf("\"\n")
128                 }
129         } END {
130                 printf("\"\" \"\"\n\"Create/Modify partitions\" \"\"\n\"Detect devices\" \"\"")
131         }' > $menufile
132         choose "Choose Partition" "Please select a partition to install Android-x86:\n\nRecommended minimum free space - 4GB  |  Optimum free space >= 8GB\n\nPartition | Filesystem | Label            | Size     | Drive name/model"
133         return $retval
134 }
135
136 progress_bar()
137 {
138         dialog --clear --title " $1 " --gauge "\n $2" 8 70
139 }
140
141 convert_fs()
142 {
143         if blkid /dev/$1 | grep -q ext2; then
144                 /system/bin/tune2fs -j /dev/$1
145                 e2fsck -fy /dev/$1
146         fi
147         if blkid /dev/$1 | grep -q ext3; then
148                 /system/bin/tune2fs -O extents,uninit_bg /dev/$1
149                 e2fsck -fy /dev/$1
150         fi
151 }
152
153 format_fs()
154 {
155         local cmd
156         echo -e '"Do not format" ""\next4 ""\nntfs ""\nfat32 ""' > $menufile
157         choose "Choose filesystem" "Please select a filesystem to format $1:"
158         case "$choice" in
159                 ext4)
160                         cmd="make_ext4fs -L"
161                         ;;
162                 ntfs)
163                         cmd="mkntfs -fL"
164                         ;;
165                 fat32)
166                         cmd="mkdosfs -n"
167                         ;;
168                 *)
169                         ;;
170         esac
171         if [ -n "$cmd" ]; then
172                 dialog --title " Confirm " --defaultno --yesno \
173                         "\n You chose to format $1 to $choice.\n All data in that partition will be LOST.\n\n Are you sure to format the partition $1?" 10 51
174                 [ $? -ne 0 ] && return 1
175                 $cmd Android-x86 /dev/$1 | awk '{
176                         # FIXME: very imprecise progress
177                         if (match($0, "done"))
178                                 printf("%d\n", i+=33)
179                 }' | progress_bar "Formatting" "Formatting partition $1..."
180         elif blkid /dev/$1 | grep -q ext[23]; then
181                 dialog --clear --title " Warning " --yesno \
182                         "\nYou chose to install android-x86 to an ext2/3 filesystem. We suggest you convert it to ext4 for better reliability and performance." 9 62
183                 [ $? -eq 0 ] && convert_fs $1
184         fi
185 }
186
187 create_entry()
188 {
189         title=$1
190         shift
191         echo -e "title $title\n\tkernel /$asrc/kernel$vga $@ SRC=/$asrc\n\tinitrd /$asrc/initrd.img\n" >> $menulst
192 }
193
194 create_menulst()
195 {
196         menulst=/hd/grub/menu.lst
197         [ -n "$VESA" ] && vga=" vga=788 modeset=0"
198         echo -e "${GRUB_OPTIONS:-default=0\ntimeout=6\nsplashimage=/grub/android-x86.xpm.gz\n}root (hd0,$1)\n" > $menulst
199
200         create_entry "Android-x86 $VER" quiet $cmdline
201         create_entry "Android-x86 $VER (Debug mode)" $cmdline DEBUG=2
202         create_entry "Android-x86 $VER (Debug nomodeset)" nomodeset $cmdline DEBUG=2
203         create_entry "Android-x86 $VER (Debug video=LVDS-1:d)" video=LVDS-1:d $cmdline DEBUG=2
204 }
205
206 create_winitem()
207 {
208         win=`fdisk -l /dev/$(echo $1 | cut -b-3) | grep ^/dev | cut -b6-12,55- | awk '{
209                 if (match($2, "NTFS"))
210                         print $1
211         }' | head -1`
212         if [ -n "$win" ]; then
213                 dialog --title " Confirm " --yesno \
214                         "\nThe installer found a Windows partition in /dev/$win.\n\nDo you want to create a boot item for Windows?" 9 59
215                 [ $? -ne 0 ] && return 1
216                 wp=$((`echo $win | cut -b4-`-1))
217                 echo -e "title Windows\n\trootnoverify (hd$d,$wp)\n\tchainloader +1\n" >> $menulst
218         fi
219 }
220
221 check_data_img()
222 {
223         losetup /dev/loop7 data.img
224         if blkid /dev/loop7 | grep -q ext[23]; then
225                 dialog --clear --title " Warning " --yesno \
226                         "\nYour data.img is an ext2/3 filesystem. We suggest you convert it to ext4 for better reliability." 8 58
227                 [ $? -eq 0 ] && convert_fs loop7
228         fi
229         losetup -d /dev/loop7
230 }
231
232 create_img()
233 {
234         bname=`basename $2`
235         if [ -e $2 ]; then
236                 dialog --title " Confirm " --defaultno --yesno \
237                         "\n $bname exists. Overwrite it?" 7 38
238                 [ $? -ne 0 ] && return 255
239                 rm -f $2
240         fi
241         dialog --title " Question " --nook --nocancel --inputbox \
242                 "\nPlease input the size of the $bname in MB:" 8 63 $1 2> $tempfile
243         size=`cat $tempfile`
244         [ 0$size -le 0 ] && size=1024
245         ( dd bs=1M count=$size if=/dev/zero | pv -ns ${size}m | dd of=$2 ) 2>&1 \
246                 | progress_bar "Creating $bname" "Expect to write $size MB..."
247 }
248
249 create_data_img()
250 {
251         dialog --title " Confirm " --yesno \
252                 "\nThe installer is going to create a disk image to save the user data. At least 512MB free disk space is recommended.\n\nAre you sure to create the image?" 11 62
253
254         if [ $? -eq 0 ]; then
255                 if create_img 512 data.img; then
256                         losetup /dev/loop6 data.img
257                         make_ext4fs -L /data /dev/loop6 > /dev/tty6
258                 fi
259                 [ $? -ne 0 ] && dialog --msgbox "\n Failed to create data.img." 7 33
260         else
261                 dialog --title " Warning " --msgbox \
262                         "\nOK. So data will be save to a RAMDISK(tmpfs), and lose after power off." 8 49
263         fi
264 }
265
266 try_upgrade()
267 {
268         [ -d $1 ] && return
269         PREV_VERS="$PREV_VERS 7.1-rc2 7.1-rc1 6.0-r3 6.0-r2 6.0-r1 6.0-rc2 6.0-rc1 5.1-rc1 4.4-r5 4.4-r4 4.4-r3 4.4-r2 4.4-r1 4.4-RC2 4.4-RC1 4.4-test 4.3-test 4.2-test 4.0-r1 4.0-RC2 4.0-RC1"
270         for v in $PREV_VERS; do
271                 local prev
272                 if [ -d hd/$v ]; then
273                         prev=hd/$v
274                 elif [ -d hd/android-$v ]; then
275                         prev=hd/android-$v
276                 else
277                         continue
278                 fi
279                 dialog --title " Question " --yesno \
280                         "\nAn older version $v is detected.\nWould you like to upgrade it?" 8 51
281                 if [ $? -eq 0 ]; then
282                         mv $prev $1
283                         rm -rf $1/data/dalvik-cache/* $1/data/system/wpa_supplicant
284                         sed -i 's/\(ctrl_interface=\)\(.*\)/\1wlan0/' $1/data/misc/wifi/wpa_supplicant.conf
285                         break
286                 fi
287         done
288 }
289
290 get_part_info()
291 {
292         d=0
293         while [ 1 ]; do
294                 h=`echo $d | awk '{ printf("%c", $1+97) }'`
295                 for part in /sys/block/[shv]d$h/$1 /sys/block/mmcblk$d/$1 /sys/block/nvme0n$(($d+1))/$1; do
296                         [ -d $part ] && break 2
297                 done
298                 d=$(($d+1))
299         done
300         p=`cat $part/partition`
301         disk=$(basename `dirname $part`)
302 }
303
304 install_to()
305 {
306         local t=`echo /sys/block/*/$1/partition`
307         [ -f "$t" ] || return 1
308         until [ -b /dev/$1 ]; do
309                 echo add > `dirname $t`/uevent
310                 sleep 1
311         done
312         cd /
313         mountpoint -q /hd && umount /hd
314         while [ 1 ]; do
315                 format_fs $1
316                 try_mount rw /dev/$1 /hd && break
317                 dialog --clear --title " Error " --defaultno --yesno \
318                         "\n Cannot mount /dev/$1\n Do you want to format it?" 8 37
319                 [ $? -ne 0 ] && return 255
320         done
321
322         fs=`cat /proc/mounts | grep /dev/$1 | awk '{ print $3 }'`
323         cmdline=`sed "s|\(initrd.*img\s*\)||; s|quiet\s*||; s|\(vga=\w\+\?\s*\)||; s|\(DPI=\w\+\?\s*\)||; s|\(INSTALL=\w\+\?\s*\)||; s|\(SRC=\S\+\?\s*\)||; s|\(DEBUG=\w\+\?\s*\)||; s|\(BOOT_IMAGE=\S\+\?\s*\)||; s|\(iso-scan/filename=\S\+\?\s*\)||; s|[[:space:]]*$||" /proc/cmdline`
324
325         [ -n "$INSTALL_PREFIX" ] && asrc=$INSTALL_PREFIX || asrc=android-$VER
326         [ -z "$efi" ] && dialog --title " Confirm " --no-label Skip --defaultno --yesno \
327                 "\n Do you want to install boot loader GRUB?" 7 47
328         if [ $? -eq 0 ]; then
329                 cp -af /grub /hd
330                 get_part_info $1
331                 p=$(($p-1))
332                 create_menulst $p
333                 create_winitem $1 $d
334                 rm -f /hd/boot/grub/stage1
335                 echo "(hd$d) /dev/$disk" > /hd/grub/device.map
336                 echo "setup (hd$d) (hd$d,$p)" | grub --device-map /hd/grub/device.map > /dev/tty5
337                 [ $? -ne 0 ] && return 255
338         fi
339
340         [ -n "$efi" ] && dialog --title " Confirm " --no-label Skip --defaultno --yesno \
341                 "\n Do you want to install EFI GRUB2?" 7 39
342         if [ $? -eq 0 ]; then
343                 for i in `list_disks`; do
344                         disk=`basename $i`
345                         esp=`sgdisk --print /dev/$disk 2> /dev/null | grep EF00 | awk '{print $1}'`
346                         [ -n "$esp" ] && boot=`find_partition $disk $esp` && break
347                 done
348                 if [ -z "$esp" ]; then
349                         get_part_info $1
350                         boot=`basename $(blkid /dev/$disk* | grep vfat | cut -d: -f1 | head -1)`
351                         [ -z "$boot" ] && boot=`find_partition $disk 1`
352                         esp=`cat /sys/block/$disk/$boot/partition`
353                 fi
354                 mkdir -p efi
355                 mountpoint -q efi && umount efi
356                 while [ 1 ]; do
357                         try_mount rw /dev/$boot efi && break
358                         dialog --title " Confirm " --defaultno --yesno "\n Cannot mount /dev/$boot.\n Do you want to format it?" 8 37
359                         [ $? -eq 0 ] && mkdosfs -n EFI /dev/$boot
360                 done
361                 if [ "$efi" = "32" ]; then
362                         grubcfg=efi/boot/grub/i386-efi/grub.cfg
363                         bootefi=bootia32.efi
364                 else
365                         grubcfg=efi/boot/grub/x86_64-efi/grub.cfg
366                         bootefi=BOOTx64.EFI
367                 fi
368                 mkdir -p `dirname $grubcfg` efi/efi/Android
369                 cp -af grub2/efi/boot/* efi/efi/Android
370                 sed -i "s|VER|$VER|; s|CMDLINE|$cmdline|" efi/efi/Android/android.cfg
371                 [ -s efi/boot/grub/grubenv ] || ( printf %-1024s "# GRUB Environment Block%" | sed 's/k%/k\n/; s/   /###/g' > efi/boot/grub/grubenv )
372
373                 echo -e 'set timeout=5\nset debug_mode="(DEBUG mode)"' > $grubcfg
374                 # Our grub-efi doesn't support ntfs directly.
375                 # Copy boot files to ESP so grub-efi could read them
376                 if [ "$fs" = "fuseblk" ]; then
377                         cp -f src/kernel src/initrd.img efi/efi/Android
378                         echo -e "set kdir=/efi/Android\nset src=SRC=/$asrc" >> $grubcfg
379                 else
380                         echo -e "set kdir=/$asrc" >> $grubcfg
381                 fi
382                 echo -e '\nsource $cmdpath/android.cfg' >> $grubcfg
383
384                 # Checking for old EFI entries, removing them and adding new depending on bitness
385                 efibootmgr | grep -Eo ".{0,6}Android-x86" | cut -c1-4 > /tmp/efientries
386                 if [ -s /tmp/efientries ]; then
387                         dialog --title " Question " --defaultno --yesno "\nEFI boot entries for previous Android-x86 installations were found.\n\nDo you wish to delete them?" 10 61
388                         [ $? -eq 0 ] && while read entry; do efibootmgr -Bb "$entry" > /dev/tty4 2>&1; done < /tmp/efientries
389                 fi
390                 efibootmgr -v -c -d /dev/$disk -p $esp -L "Android-x86 $VER" -l /efi/Android/$bootefi > /dev/tty4 2>&1
391         fi
392
393         dialog --title " Question " --defaultno --yesno \
394                 "\nDo you want to install /system directory as read-write?\n\nMaking /system be read-write is easier for debugging, but it needs more disk space and longer installation time." 10 61
395         instal_rw=$?
396
397         files="mnt/$SRC/kernel mnt/$SRC/initrd.img mnt/$SRC/$RAMDISK"
398         if [ $instal_rw -eq 0 ]; then
399                 if [ "$fs" = "vfat" -o "$fs" = "fuseblk" ]; then
400                         [ -e /sfs/system.img ] && sysimg="/sfs/system.img" || sysimg="mnt/$SRC/system.*"
401                 else
402                         sysimg="android/system"
403                 fi
404         else
405                 sysimg="mnt/$SRC/system.*"
406         fi
407         files="$files $sysimg"
408         size=0
409         for s in `du -sk $files | awk '{print $1}'`; do
410                 size=$(($size+$s))
411         done
412         try_upgrade hd/$asrc
413         mkdir -p hd/$asrc
414         cd hd/$asrc
415         rm -rf system*
416         ( ( cd /; find $files | $CPIO -H newc -o ) | pv -ns ${size}k | ( $CPIO -iud > /dev/null; echo $? > /tmp/result )) 2>&1 \
417                 | progress_bar "Installing Android-x86" "Expect to write $size KB..."
418         result=$((`cat /tmp/result`*255))
419
420         if [ $result -eq 0 ]; then
421                 for d in android mnt sfs ./$SRC; do
422                         [ -d $d ] && mv $d/* . && rmdir $d
423                 done
424                 chown 0.0 *
425                 for f in *; do
426                         [ -d $f ] || chmod 644 $f
427                 done
428
429                 case "$fs" in
430                         vfat|fuseblk)
431                                 [ -e data.img ] && check_data_img || create_data_img
432                                 ;;
433                         *)
434                                 mkdir -p data
435                                 ;;
436                 esac
437         fi
438
439         dialog --infobox "\n Syncing to disk..." 5 27
440         sync
441
442         return $result
443 }
444
445 install_hd()
446 {
447         select_dev || rebooting
448         retval=1
449         case "$choice" in
450                 Create*)
451                         partition_drive
452                         retval=$?
453                         ;;
454                 Detect*)
455                         dialog --title " Detecting... " --nocancel --pause "" 8 41 1
456                         ;;
457                 *)
458                         install_to $choice
459                         retval=$?
460                         ;;
461         esac
462         return $retval
463 }
464
465 do_install()
466 {
467         booted_from=`basename $dev`
468         efi=$(cat /sys/firmware/efi/fw_platform_size 2> /dev/null)
469         [ -n "$efi" ] && mount -t efivarfs none /sys/firmware/efi/efivars
470
471         until install_hd; do
472                 if [ $retval -eq 255 ]; then
473                         dialog --title ' Error! ' --yes-label Retry --no-label Reboot \
474                                 --yesno '\nInstallation failed! Please check if you have enough free disk space to install Android-x86.' 8 51
475                         [ $? -eq 1 ] && rebooting
476                 fi
477         done
478
479         [ -n "$VESA" ] || runit="Run Android-x86"
480         dialog --clear --title ' Congratulations! ' \
481                 --menu "\n Android-x86 is installed successfully.\n " 11 51 13 \
482                 "$runit" "" "Reboot" "" 2> $tempfile
483         case "`cat $tempfile`" in
484                 Run*)
485                         cd /android
486                         umount system
487                         mountpoint -q /sfs && umount /sfs
488                         if [ -e /hd/$asrc/system.sfs ]; then
489                                 mount -o loop /hd/$asrc/system.sfs /sfs
490                                 mount -o loop /sfs/system.img system
491                         elif [ -e /hd/$asrc/system.img ]; then
492                                 mount -o loop /hd/$asrc/system.img system
493                         else
494                                 mount --bind /hd/$asrc/system system
495                         fi
496                         if [ -d /hd/$asrc/data ]; then
497                                 mount --bind /hd/$asrc/data data
498                         elif [ -e /hd/$asrc/data.img ]; then
499                                 mount -o loop /hd/$asrc/data.img data
500                         fi
501                         ;;
502                 *)
503                         rebooting
504                         ;;
505         esac
506 }