OSDN Git Service

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