OSDN Git Service

1-install: support formatting ext4 partition
[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                 cfdisk /dev/$choice
64                 if [ $? -eq 0 ]; then
65                         retval=1
66                 else
67                         retval=255
68                 fi
69         fi
70         return $retval
71 }
72
73 select_dev()
74 {
75         fdisk -l | grep ^/dev | cut -b6-12,55- | awk '{
76                 if (!match($2, "Extended")) {
77                         printf("\"%-28s", $0)
78                         system("echo -n `cat /sys/block/*/"$1"/../device/model`")
79                         printf("\" \"\"\n")
80                 }
81         } END {
82                 printf("\"Create/Modify partitions\" \"\"\n\"Detect devices\" \"\"")
83         }' > $menufile
84         choose "Choose Partition" "Please select a partition to install Android-x86:"
85         return $retval
86 }
87
88 progress_bar()
89 {
90         dialog --clear --title " $1 " --gauge "\n $2" 8 70
91 }
92
93 format_fs()
94 {
95         local cmd
96         echo -e '"Do not format" ""\next4 ""\next3 ""\next2 ""\nntfs ""\nfat32 ""' > $menufile
97         choose "Choose filesystem" "Please select a filesystem to format $1:"
98         case "$choice" in
99                 ext4)
100                         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
101                         cmd="make_ext4fs -L"
102                         ;;
103                 ext3)
104                         cmd="mke2fs -jL"
105                         ;;
106                 ext2)
107                         cmd="mke2fs -L"
108                         ;;
109                 ntfs)
110                         cmd="mkntfs -fL"
111                         ;;
112                 fat32)
113                         cmd="mkdosfs -n"
114                         ;;
115                 *)
116                         ;;
117         esac
118         if [ -n "$cmd" ]; then
119                 dialog --title " Confirm " --no-label Skip --yesno \
120                         "\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
121                 [ $? -ne 0 ] && return 1
122                 $cmd Android-x86 /dev/$1 | awk '{
123                         # FIXME: very imprecise progress
124                         if (match($0, "done"))
125                                 printf("%d\n", i+=33)
126                 }' | progress_bar "Formatting" "Formatting partition $1..."
127         fi
128 }
129
130 create_entry()
131 {
132         title=$1
133         shift
134         echo -e "title $title\n\tkernel /$asrc/kernel$vga $@ SRC=/$asrc\n\tinitrd /$asrc/initrd.img\n" >> $menulst
135 }
136
137 create_menulst()
138 {
139         menulst=/hd/grub/menu.lst
140         [ -n "$VESA" ] && vga=" vga=788 modeset=0"
141         echo -e "${GRUB_OPTIONS:-default=0\ntimeout=6\nsplashimage=/grub/android-x86.xpm.gz\n}root (hd0,$1)\n" > $menulst
142         cmdline=`cat /proc/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.*\)||"`
143
144         create_entry "Android-x86 $VER" quiet $cmdline
145         create_entry "Android-x86 $VER (Debug mode)" $cmdline DEBUG=2
146         create_entry "Android-x86 $VER (Debug nomodeset)" nomodeset $cmdline DEBUG=2
147         create_entry "Android-x86 $VER (Debug video=LVDS-1:d)" video=LVDS-1:d $cmdline DEBUG=2
148 }
149
150 create_winitem()
151 {
152         win=`fdisk -l /dev/$(echo $1 | cut -b-3) | grep ^/dev | cut -b6-12,55- | awk '{
153                 if (match($2, "NTFS"))
154                         print $1
155         }' | head -1`
156         if [ -n "$win" ]; then
157                 dialog --title " Confirm " --yesno \
158                         "\nThe installer found a Windows partition in /dev/$win.\n\nDo you want to create a boot item for Windows?" 9 59
159                 [ $? -ne 0 ] && return 1
160                 wp=$((`echo $win | cut -b4-`-1))
161                 echo -e "title Windows\n\trootnoverify (hd$d,$wp)\n\tchainloader +1\n" >> $menulst
162         fi
163 }
164
165 create_img()
166 {
167         bname=`basename $2`
168         if [ -e $2 ]; then
169                 dialog --title " Confirm " --defaultno --yesno \
170                         "\n $bname exists. Overwrite it?" 7 38
171                 [ $? -ne 0 ] && return 255
172                 rm -f $2
173         fi
174         dialog --title " Question " --nook --nocancel --inputbox \
175                 "\nPlease input the size of the $bname in MB (max 2047):" 8 63 $1 2> $tempfile
176         size=`cat $tempfile`
177         [ 0$size -le 0 -o 0$size -gt 2047 ] && size=2047
178         ( dd bs=1M count=$size if=/dev/zero | pv -ns ${size}m | dd of=$2 ) 2>&1 \
179                 | progress_bar "Creating $bname" "Expect to write $size MB..."
180 }
181
182 create_data_img()
183 {
184         dialog --title " Confirm " --yesno \
185                 "\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
186
187         if [ $? -eq 0 ]; then
188                 if create_img 512 data.img; then
189                         losetup /dev/loop6 data.img
190                         make_ext4fs -L /data /dev/loop6 > /dev/tty6
191                 fi
192                 [ $? -ne 0 ] && dialog --msgbox "\n Failed to create data.img." 7 33
193         else
194                 dialog --title " Warning " --msgbox \
195                         "\nOK. So data will be save to a RAMDISK(tmpfs), and lose after power off." 8 49
196         fi
197 }
198
199 try_upgrade()
200 {
201         [ -d $1 ] && return
202         PREV_VERS="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"
203         for v in $PREV_VERS; do
204                 prev=hd/android-$v
205                 if [ -d $prev ]; then
206                         dialog --title " Question " --yesno \
207                                 "\nAn older Android-x86 version $v is detected.\nWould you like to upgrade it?" 8 55
208                         if [ $? -eq 0 ]; then
209                                 mv $prev $1
210                                 rm -rf $1/data/dalvik-cache/* $1/data/system/wpa_supplicant
211                                 sed -i 's/\(ctrl_interface=\)\(.*\)/\1wlan0/' $1/data/misc/wifi/wpa_supplicant.conf
212                                 break
213                         fi
214                 fi
215         done
216 }
217
218 install_to()
219 {
220         cd /
221         mountpoint -q /hd && umount /hd
222         while [ 1 ]; do
223                 format_fs $1
224                 try_mount rw /dev/$1 /hd && break
225                 dialog --clear --title " Error " --defaultno --yesno \
226                         "\n Cannot mount /dev/$1\n Do you want to format it?" 8 37
227                 [ $? -ne 0 ] && return 255
228         done
229
230         fs=`cat /proc/mounts | grep /dev/$1 | awk '{ print $3 }'`
231
232         asrc=android-$VER
233         [ "$fs" != "ext4" ] && dialog --title " Confirm " --no-label Skip --defaultno --yesno \
234                 "\n Do you want to install boot loader GRUB?" 7 47
235         if [ $? -eq 0 ]; then
236                 cp -af /grub /hd
237                 d=0
238                 while [ 1 ]; do
239                         h=`echo $d | awk '{ printf("%c", $1+97) }'`
240                         [ -d /sys/block/[shv]d$h/$1 ] && break
241                         d=$(($d+1))
242                 done
243                 p=$((`echo $1 | cut -b4-`-1))
244                 disk=`echo $1 | cut -b-3`
245                 create_menulst $p
246                 create_winitem $1 $d
247                 rm -f /hd/boot/grub/stage1
248                 echo "(hd$d) /dev/$disk" > /hd/grub/device.map
249                 echo "setup (hd$d) (hd$d,$p)" | grub --device-map /hd/grub/device.map > /dev/tty5
250                 [ $? -ne 0 ] && return 255
251         fi
252
253         dialog --title " Question " --yesno \
254                 "\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
255         instal_rw=$?
256
257         files="mnt/$SRC/kernel mnt/$SRC/initrd.img mnt/$SRC/ramdisk.img"
258         if [ $instal_rw -eq 0 ]; then
259                 if [ "$fs" = "vfat" -o "$fs" = "fuseblk" ]; then
260                         [ -e /sfs/system.img ] && sysimg="/sfs/system.img" || sysimg="mnt/$SRC/system.*"
261                 else
262                         sysimg="android/system"
263                 fi
264         else
265                 sysimg="mnt/$SRC/system.*"
266         fi
267         files="$files $sysimg"
268         size=0
269         for s in `du -sk $files | awk '{print $1}'`; do
270                 size=$(($size+$s))
271         done
272         try_upgrade hd/$asrc
273         mkdir -p hd/$asrc
274         cd hd/$asrc
275         rm -rf system*
276         ( ( cd /; find $files | $CPIO -H newc -o ) | pv -ns ${size}k | ( $CPIO -iud > /dev/null; echo $? > /tmp/result )) 2>&1 \
277                 | progress_bar "Installing Android-x86" "Expect to write $size KB..."
278         result=$((`cat /tmp/result`*255))
279
280         if [ $result -eq 0 ]; then
281                 chmod 644 *
282                 chown 0.0 *
283                 for d in android mnt sfs ./$SRC; do
284                         [ -d $d ] && mv $d/* . && rmdir $d
285                 done
286
287                 case "$fs" in
288                         vfat|fuseblk)
289                                 create_data_img
290                                 ;;
291                         *)
292                                 mkdir -p data
293                                 ;;
294                 esac
295         fi
296
297         sync
298
299         return $result
300 }
301
302 install_hd()
303 {
304         select_dev || rebooting
305         retval=1
306         case "$choice" in
307                 [shvm][dm]*)
308                         install_to $choice
309                         retval=$?
310                         ;;
311                 Create*)
312                         partition_drive
313                         retval=$?
314                         ;;
315                 Detect*)
316                         dialog --title " Detecting... " --nocancel --pause "" 8 41 1
317                         ;;
318         esac
319         return $retval
320 }
321
322 do_install()
323 {
324         until install_hd; do
325                 if [ $retval -eq 255 ]; then
326                         dialog --title ' Error! ' --yes-label Retry --no-label Reboot \
327                                 --yesno '\nInstallation failed! Please check if you have enough free disk space to install Android-x86.' 8 51
328                         [ $? -eq 1 ] && rebooting
329                 fi
330         done
331
332         [ -n "$VESA" ] || runit="Run Android-x86"
333         dialog --clear --title ' Congratulations! ' \
334                 --menu "\n Android-x86 is installed successfully.\n " 11 51 13 \
335                 "$runit" "" "Reboot" "" 2> $tempfile
336         case "`cat $tempfile`" in
337                 Run*)
338                         cd /android
339                         umount system
340                         mountpoint -q /sfs && umount /sfs
341                         if [ -e /hd/$asrc/system.sfs ]; then
342                                 mount -o loop /hd/$asrc/system.sfs /sfs
343                                 mount -o loop /sfs/system.img system
344                         elif [ -e /hd/$asrc/system.img ]; then
345                                 mount -o loop /hd/$asrc/system.img system
346                         else
347                                 mount --bind /hd/$asrc/system system
348                         fi
349                         if [ -d /hd/$asrc/data ]; then
350                                 mount --bind /hd/$asrc/data data
351                         elif [ -e /hd/$asrc/data.img ]; then
352                                 mount -o loop /hd/$asrc/data.img data
353                         fi
354                         ;;
355                 *)
356                         rebooting
357                         ;;
358         esac
359 }