OSDN Git Service

1-install: add 6.0-r3 to the upgrade list
[android-x86/bootable-newinstaller.git] / install / scripts / 1-install
1 #
2 # By Chih-Wei Huang <cwhuang@linux.org.tw>
3 # Last updated 2016/07/21
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" 20 71 13 --file $menufile 2> $tempfile
27
28         retval=$?
29         choice=`cat $tempfile`
30 }
31
32 partition_drive()
33 {
34         echo -n > $menufile
35         for i in /sys/block/[shv]d[a-z] /sys/block/mmcblk?; do
36                 if [ ! -d $i ]; then  # pathname expansion failed
37                         continue
38                 fi
39                 echo -n `basename $i` >> $menufile
40                 if [ -f $i/removable -a `cat $i/removable` -eq 0 ]; then
41                         echo -n ' "Harddisk ' >> $menufile
42                 else
43                         echo -n ' "Removable' >> $menufile
44                 fi
45                 if [ -f $i/size ]; then
46                         echo -n " (" `cat $i/size` "blocks)" >> $menufile
47                 fi
48                 echo '"' >> $menufile
49         done
50         count=`wc -l $menufile | awk '{ print $1 }'`
51         if [ $count -eq 0 ]; then
52                 dialog --title " Error " --msgbox \
53                         "\nOK. There is no hard drive to edit partitions." 8 49
54                 return 255
55         fi
56         if [ $count -eq 1 ]; then
57                 choice=`awk '{ print $1 }' $menufile`
58                 retval=0
59         else
60                 choose "Choose Drive" "Please select a drive to edit partitions:"
61         fi
62         if [ $retval -eq 0 ]; then
63                 dialog --title " Confirm " --defaultno --yesno "\n Do you want to use GPT?" 7 29
64                 if [ $? -eq 0 ]; then
65                         cgdisk /dev/$choice
66                 else
67                         cfdisk /dev/$choice
68                 fi
69                 if [ $? -eq 0 ]; then
70                         retval=1
71                 else
72                         retval=255
73                 fi
74         fi
75         return $retval
76 }
77
78 select_dev()
79 {
80         blkid | grep -v -E "^/dev/block/|^/dev/loop" | cut -b6- | sort | awk '{
81                 t="unknown"
82                 for (i = NF; i > 1; --i)
83                         if (match($i, "^TYPE")) {
84                                 t=$i
85                                 break
86                         }
87                 gsub(/TYPE=|"/, "", t)
88                 printf("%s\t%s\n", $1, t)
89         }' > $tempfile
90
91         lsblk=`ls /sys/block | grep -v -E "loop|ram|sr|boot|rpmb"`
92         for d in $lsblk; do
93                 for i in /sys/block/$d/$d*; do
94                         echo $i | grep -q -E "boot|rpmb" && continue
95                         [ -d $i ] && ( grep "`basename $i:`" $tempfile || echo "`basename $i` unknown" )
96                 done
97         done | awk '{
98                 sub(/:/, "", $1)
99                 printf("\"%-13s%-17s", $1, $2)
100                 system("cd /sys/block; for f in "$1"/device/model "$1"/device/name */"$1"/../device/model */"$1"/../device/name; do [ -e $f ] && echo -n `cat $f` && break; done")
101                 printf("\" \"\"\n")
102         } END {
103                 printf("\"Create/Modify partitions\" \"\"\n\"Detect devices\" \"\"")
104         }' > $menufile
105         choose "Choose Partition" "Please select a partition to install Android-x86:"
106         return $retval
107 }
108
109 progress_bar()
110 {
111         dialog --clear --title " $1 " --gauge "\n $2" 8 70
112 }
113
114 convert_fs()
115 {
116         if blkid /dev/$1 | grep -q ext2; then
117                 /system/bin/tune2fs -j /dev/$1
118                 e2fsck -fy /dev/$1
119         fi
120         if blkid /dev/$1 | grep -q ext3; then
121                 /system/bin/tune2fs -O extents,uninit_bg /dev/$1
122                 e2fsck -fy /dev/$1
123         fi
124 }
125
126 format_fs()
127 {
128         local cmd
129         echo -e '"Do not format" ""\next4 ""\nntfs ""\nfat32 ""' > $menufile
130         choose "Choose filesystem" "Please select a filesystem to format $1:"
131         case "$choice" in
132                 ext4)
133                         cmd="make_ext4fs -L"
134                         ;;
135                 ntfs)
136                         cmd="mkntfs -fL"
137                         ;;
138                 fat32)
139                         cmd="mkdosfs -n"
140                         ;;
141                 *)
142                         ;;
143         esac
144         if [ -n "$cmd" ]; then
145                 dialog --title " Confirm " --defaultno --yesno \
146                         "\n You chose to format $1 to $choice.\n All data in that partition will LOSE.\n\n Are you sure to format the partition $1?" 10 51
147                 [ $? -ne 0 ] && return 1
148                 $cmd Android-x86 /dev/$1 | awk '{
149                         # FIXME: very imprecise progress
150                         if (match($0, "done"))
151                                 printf("%d\n", i+=33)
152                 }' | progress_bar "Formatting" "Formatting partition $1..."
153         elif blkid /dev/$1 | grep -q ext[23]; then
154                 dialog --clear --title " Warning " --yesno \
155                         "\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
156                 [ $? -eq 0 ] && convert_fs $1
157         fi
158 }
159
160 create_entry()
161 {
162         title=$1
163         shift
164         echo -e "title $title\n\tkernel /$asrc/kernel$vga $@ SRC=/$asrc\n\tinitrd /$asrc/initrd.img\n" >> $menulst
165 }
166
167 create_menulst()
168 {
169         menulst=/hd/grub/menu.lst
170         [ -n "$VESA" ] && vga=" vga=788 modeset=0"
171         echo -e "${GRUB_OPTIONS:-default=0\ntimeout=6\nsplashimage=/grub/android-x86.xpm.gz\n}root (hd0,$1)\n" > $menulst
172
173         create_entry "Android-x86 $VER" quiet $cmdline
174         create_entry "Android-x86 $VER (Debug mode)" $cmdline DEBUG=2
175         create_entry "Android-x86 $VER (Debug nomodeset)" nomodeset $cmdline DEBUG=2
176         create_entry "Android-x86 $VER (Debug video=LVDS-1:d)" video=LVDS-1:d $cmdline DEBUG=2
177 }
178
179 create_winitem()
180 {
181         win=`fdisk -l /dev/$(echo $1 | cut -b-3) | grep ^/dev | cut -b6-12,55- | awk '{
182                 if (match($2, "NTFS"))
183                         print $1
184         }' | head -1`
185         if [ -n "$win" ]; then
186                 dialog --title " Confirm " --yesno \
187                         "\nThe installer found a Windows partition in /dev/$win.\n\nDo you want to create a boot item for Windows?" 9 59
188                 [ $? -ne 0 ] && return 1
189                 wp=$((`echo $win | cut -b4-`-1))
190                 echo -e "title Windows\n\trootnoverify (hd$d,$wp)\n\tchainloader +1\n" >> $menulst
191         fi
192 }
193
194 check_data_img()
195 {
196         losetup /dev/loop7 data.img
197         if blkid /dev/loop7 | grep -q ext[23]; then
198                 dialog --clear --title " Warning " --yesno \
199                         "\nYour data.img is an ext2/3 filesystem. We suggest you convert it to ext4 for better reliability." 8 58
200                 [ $? -eq 0 ] && convert_fs loop7
201         fi
202         losetup -d /dev/loop7
203 }
204
205 create_img()
206 {
207         bname=`basename $2`
208         if [ -e $2 ]; then
209                 dialog --title " Confirm " --defaultno --yesno \
210                         "\n $bname exists. Overwrite it?" 7 38
211                 [ $? -ne 0 ] && return 255
212                 rm -f $2
213         fi
214         dialog --title " Question " --nook --nocancel --inputbox \
215                 "\nPlease input the size of the $bname in MB:" 8 63 $1 2> $tempfile
216         size=`cat $tempfile`
217         [ 0$size -le 0 ] && size=1024
218         ( dd bs=1M count=$size if=/dev/zero | pv -ns ${size}m | dd of=$2 ) 2>&1 \
219                 | progress_bar "Creating $bname" "Expect to write $size MB..."
220 }
221
222 create_data_img()
223 {
224         dialog --title " Confirm " --yesno \
225                 "\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
226
227         if [ $? -eq 0 ]; then
228                 if create_img 512 data.img; then
229                         losetup /dev/loop6 data.img
230                         make_ext4fs -L /data /dev/loop6 > /dev/tty6
231                 fi
232                 [ $? -ne 0 ] && dialog --msgbox "\n Failed to create data.img." 7 33
233         else
234                 dialog --title " Warning " --msgbox \
235                         "\nOK. So data will be save to a RAMDISK(tmpfs), and lose after power off." 8 49
236         fi
237 }
238
239 try_upgrade()
240 {
241         [ -d $1 ] && return
242         PREV_VERS="$PREV_VERS 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"
243         for v in $PREV_VERS; do
244                 local prev
245                 if [ -d hd/$v ]; then
246                         prev=hd/$v
247                 elif [ -d hd/android-$v ]; then
248                         prev=hd/android-$v
249                 else
250                         continue
251                 fi
252                 dialog --title " Question " --yesno \
253                         "\nAn older version $v is detected.\nWould you like to upgrade it?" 8 51
254                 if [ $? -eq 0 ]; then
255                         mv $prev $1
256                         rm -rf $1/data/dalvik-cache/* $1/data/system/wpa_supplicant
257                         sed -i 's/\(ctrl_interface=\)\(.*\)/\1wlan0/' $1/data/misc/wifi/wpa_supplicant.conf
258                         break
259                 fi
260         done
261 }
262
263 get_part_info()
264 {
265         d=0
266         while [ 1 ]; do
267                 h=`echo $d | awk '{ printf("%c", $1+97) }'`
268                 for part in /sys/block/[shv]d$h/$1 /sys/block/mmcblk$d/$1; do
269                         [ -d $part ] && break 2
270                 done
271                 d=$(($d+1))
272         done
273         p=`cat $part/partition`
274         disk=$(basename `dirname $part`)
275 }
276
277 install_to()
278 {
279         cd /
280         mountpoint -q /hd && umount /hd
281         while [ 1 ]; do
282                 format_fs $1
283                 try_mount rw /dev/$1 /hd && break
284                 dialog --clear --title " Error " --defaultno --yesno \
285                         "\n Cannot mount /dev/$1\n Do you want to format it?" 8 37
286                 [ $? -ne 0 ] && return 255
287         done
288
289         fs=`cat /proc/mounts | grep /dev/$1 | awk '{ print $3 }'`
290         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*\)||;" /proc/cmdline`
291
292         [ -n "$INSTALL_PREFIX" ] && asrc=$INSTALL_PREFIX || asrc=android-$VER
293         efi="`dmesg | grep EFI.VGA`"
294         [ -z "$efi" ] && dialog --title " Confirm " --no-label Skip --defaultno --yesno \
295                 "\n Do you want to install boot loader GRUB?" 7 47
296         if [ $? -eq 0 ]; then
297                 cp -af /grub /hd
298                 get_part_info $1
299                 p=$(($p-1))
300                 create_menulst $p
301                 create_winitem $1 $d
302                 rm -f /hd/boot/grub/stage1
303                 echo "(hd$d) /dev/$disk" > /hd/grub/device.map
304                 echo "setup (hd$d) (hd$d,$p)" | grub --device-map /hd/grub/device.map > /dev/tty5
305                 [ $? -ne 0 ] && return 255
306         fi
307
308         [ -n "$efi" ] && dialog --title " Confirm " --no-label Skip --defaultno --yesno \
309                 "\n Do you want to install EFI GRUB2?" 7 39
310         if [ $? -eq 0 ]; then
311                 get_part_info $1
312                 for i in /sys/block/$disk/$disk*; do
313                         [ 0`cat $i/partition` -eq 1 ] && b=$i
314                         [ $(blkid /dev/`basename $i` | grep -c vfat) -ne 0 ] && b=$i && break
315                 done
316                 boot=`basename $b`
317                 mountpoint -q /hd && umount /hd
318                 dialog --title " Confirm " --defaultno --yesno \
319                         "\n Do you want to format the boot partition\n /dev/$boot?" 8 45
320                 [ $? -eq 0 ] && mkdosfs -n EFI /dev/$boot
321                 while [ 1 ]; do
322                         try_mount rw /dev/$boot /hd && break
323                         dialog --title " Confirm " --yesno \
324                         "\n Cannot mount /dev/$boot\n Do you want to format it?" 8 37
325                         [ $? -eq 0 ] && mkdosfs -n EFI /dev/$boot
326                 done
327                 cp -af /grub2/efi /hd
328                 mkdir -p /hd/boot/grub
329                 grubcfg=/hd/boot/grub/grub.cfg
330                 echo -e "set timeout=5\n\n" > $grubcfg
331                 echo -e "menuentry \"Android-x86 $VER\" {\n\tsearch --set=root --file /$asrc/kernel\n\tlinux /$asrc/kernel quiet $cmdline \n\tinitrd /$asrc/initrd.img\n}" >> $grubcfg
332                 echo -e "menuentry \"Android-x86 $VER (DEBUG mode)\" {\n\tsearch --set=root --file /$asrc/kernel\n\tlinux /$asrc/kernel $cmdline DEBUG=2\n\tinitrd /$asrc/initrd.img\n}" >> $grubcfg
333                 if [ -e /hd/EFI/Microsoft/Boot/bootmgfw.efi ]; then
334                         echo -e "menuentry \"Windows (UEFI)\" {\n\tsearch --set=root --file /EFI/Microsoft/Boot/bootmgfw.efi\n\tchainloader /EFI/Microsoft/Boot/bootmgfw.efi\n}" >> $grubcfg
335                 fi
336                 mountpoint -q /hd && umount /hd
337                 try_mount rw /dev/$1 /hd
338         fi
339
340         dialog --title " Question " --defaultno --yesno \
341                 "\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
342         instal_rw=$?
343
344         files="mnt/$SRC/kernel mnt/$SRC/initrd.img mnt/$SRC/$RAMDISK"
345         if [ $instal_rw -eq 0 ]; then
346                 if [ "$fs" = "vfat" -o "$fs" = "fuseblk" ]; then
347                         [ -e /sfs/system.img ] && sysimg="/sfs/system.img" || sysimg="mnt/$SRC/system.*"
348                 else
349                         sysimg="android/system"
350                 fi
351         else
352                 sysimg="mnt/$SRC/system.*"
353         fi
354         files="$files $sysimg"
355         size=0
356         for s in `du -sk $files | awk '{print $1}'`; do
357                 size=$(($size+$s))
358         done
359         try_upgrade hd/$asrc
360         mkdir -p hd/$asrc
361         cd hd/$asrc
362         rm -rf system*
363         ( ( cd /; find $files | $CPIO -H newc -o ) | pv -ns ${size}k | ( $CPIO -iud > /dev/null; echo $? > /tmp/result )) 2>&1 \
364                 | progress_bar "Installing Android-x86" "Expect to write $size KB..."
365         result=$((`cat /tmp/result`*255))
366
367         if [ $result -eq 0 ]; then
368                 for d in android mnt sfs ./$SRC; do
369                         [ -d $d ] && mv $d/* . && rmdir $d
370                 done
371                 chown 0.0 *
372                 for f in *; do
373                         [ -d $f ] || chmod 644 $f
374                 done
375
376                 case "$fs" in
377                         vfat|fuseblk)
378                                 [ -e data.img ] && check_data_img || create_data_img
379                                 ;;
380                         *)
381                                 mkdir -p data
382                                 ;;
383                 esac
384         fi
385
386         dialog --infobox "\n Syncing to disk..." 5 27
387         sync
388
389         return $result
390 }
391
392 install_hd()
393 {
394         select_dev || rebooting
395         retval=1
396         case "$choice" in
397                 [shvm][dm]*)
398                         install_to $choice
399                         retval=$?
400                         ;;
401                 Create*)
402                         partition_drive
403                         retval=$?
404                         ;;
405                 Detect*)
406                         dialog --title " Detecting... " --nocancel --pause "" 8 41 1
407                         ;;
408         esac
409         return $retval
410 }
411
412 do_install()
413 {
414         until install_hd; do
415                 if [ $retval -eq 255 ]; then
416                         dialog --title ' Error! ' --yes-label Retry --no-label Reboot \
417                                 --yesno '\nInstallation failed! Please check if you have enough free disk space to install Android-x86.' 8 51
418                         [ $? -eq 1 ] && rebooting
419                 fi
420         done
421
422         [ -n "$VESA" ] || runit="Run Android-x86"
423         dialog --clear --title ' Congratulations! ' \
424                 --menu "\n Android-x86 is installed successfully.\n " 11 51 13 \
425                 "$runit" "" "Reboot" "" 2> $tempfile
426         case "`cat $tempfile`" in
427                 Run*)
428                         cd /android
429                         umount system
430                         mountpoint -q /sfs && umount /sfs
431                         if [ -e /hd/$asrc/system.sfs ]; then
432                                 mount -o loop /hd/$asrc/system.sfs /sfs
433                                 mount -o loop /sfs/system.img system
434                         elif [ -e /hd/$asrc/system.img ]; then
435                                 mount -o loop /hd/$asrc/system.img system
436                         else
437                                 mount --bind /hd/$asrc/system system
438                         fi
439                         if [ -d /hd/$asrc/data ]; then
440                                 mount --bind /hd/$asrc/data data
441                         elif [ -e /hd/$asrc/data.img ]; then
442                                 mount -o loop /hd/$asrc/data.img data
443                         fi
444                         ;;
445                 *)
446                         rebooting
447                         ;;
448         esac
449 }