OSDN Git Service

init: use mdev as the hotplug tool
[android-x86/bootable-newinstaller.git] / install / scripts / 1-install
1 #
2 # By Chih-Wei Huang <cwhuang@linux.org.tw>
3 # Last updated 2011/01/09
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=/system/xbin/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 select_dev()
33 {
34         fdisk -l | grep ^/dev | cut -b6-12,55- | awk '{
35                 if (!match($2, "Extended")) {
36                         printf("\"%-28s", $0)
37                         system("echo -n `cat /sys/block/*/"$1"/../device/model`")
38                         printf("\" \"\"\n")
39                 }
40         } END {
41                 printf("\"Create/Modify partitions\" \"\"\n\"Detect devices\" \"\"")
42         }' > $menufile
43         choose "Choose Partition" "Please select a partition to install Android-x86:"
44         return $retval
45 }
46
47 progress_bar()
48 {
49         dialog --clear --title " $1 " --gauge "\n $2" 8 70
50 }
51
52 format_fs()
53 {
54         local cmd
55         echo -e '"Do not format" ""\next3 ""\next2 ""\nntfs ""\nfat32 ""' > $menufile
56         choose "Choose filesystem" "Please select a filesystem to format $1:"
57         case "$choice" in
58                 ext3)
59                         cmd="mke2fs -jL"
60                         ;;
61                 ext2)
62                         cmd="mke2fs -L"
63                         ;;
64                 ntfs)
65                         cmd="mkntfs -fL"
66                         ;;
67                 fat32)
68                         cmd="newfs_msdos -L"
69                         ;;
70                 *)
71                         ;;
72         esac
73         if [ -n "$cmd" ]; then
74                 dialog --title " Confirm " --no-label Skip --yesno \
75                         "\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
76                 [ $? -ne 0 ] && return 1
77                 $cmd Android-x86 /dev/$1 | awk '{
78                         # FIXME: very imprecise progress
79                         if (match($0, "done"))
80                                 printf("%d\n", i+=33)
81                 }' | progress_bar "Formatting" "Formatting partition $1..."
82         fi
83 }
84
85 create_entry()
86 {
87         title=$1
88         shift
89         echo -e "title $title\n\tkernel /$asrc/kernel$vga $@ SRC=/$asrc\n\tinitrd /$asrc/initrd.img\n" >> $menulst
90 }
91
92 create_menulst()
93 {
94         menulst=/hd/grub/menu.lst
95         [ -n "$VESA" ] && vga=" vga=788 modeset=0"
96         echo -e "default=0\ntimeout=6\nroot (hd0,$1)\nsplashimage=/grub/android-x86.xpm.gz\n" > $menulst
97         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.*\)||"`
98
99         if grep -q ^ro.sf.lcd_density /android/default.prop /android/system/build.prop; then
100                 create_entry "Android-x86 $VER" quiet $cmdline
101         else
102                 create_entry "Android-x86 $VER (HDPI)" quiet $cmdline DPI=240
103                 create_entry "Android-x86 $VER (MDPI)" quiet $cmdline DPI=160
104         fi
105         create_entry "Android-x86 $VER (Debug mode)" $cmdline DEBUG=1
106 }
107
108 create_winitem()
109 {
110         win=`fdisk -l /dev/$(echo $1 | cut -b-3) | grep ^/dev | cut -b6-12,55- | awk '{
111                 if (match($2, "NTFS"))
112                         print $1
113         }' | head -1`
114         if [ -n "$win" ]; then
115                 dialog --title " Confirm " --yesno \
116                         "\nThe installer found a Windows partition in /dev/$win.\n\nDo you want to create a boot item for Windows?" 9 59
117                 [ $? -ne 0 ] && return 1
118                 wp=$((`echo $win | cut -b4-`-1))
119                 echo -e "title Windows\n\trootnoverify (hd$d,$wp)\n\tchainloader +1\n" >> $menulst
120         fi
121 }
122
123 create_img()
124 {
125         bname=`basename $2`
126         if [ -e $2 ]; then
127                 dialog --title " Confirm " --defaultno --yesno \
128                         "\n $bname exists. Overwrite it?" 7 38
129                 [ $? -ne 0 ] && return 255
130                 rm -f $2
131         fi
132         dialog --title " Question " --nook --nocancel --inputbox \
133                 "\nPlease input the size of the $bname in MB (max 2047):" 8 63 $1 2> $tempfile
134         size=`cat $tempfile`
135         [ 0$size -le 0 -o 0$size -gt 2047 ] && size=2047
136         ( dd bs=1M count=$size if=/dev/zero | pv -ns ${size}m | dd of=$2 ) 2>&1 \
137                 | progress_bar "Creating $bname" "Expect to write $size MB..."
138 }
139
140 create_data_img()
141 {
142         dialog --title " Confirm " --yesno \
143                 "\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
144
145         if [ $? -eq 0 ]; then
146                 if create_img 512 data.img; then
147                         losetup /dev/loop6 data.img
148                         mke2fs -jL /data /dev/loop6 > /dev/tty6
149                 fi
150                 [ $? -ne 0 ] && dialog --msgbox "\n Failed to create data.img." 7 33
151         else
152                 dialog --title " Warning " --msgbox \
153                         "\nOK. So data will be save to a RAMDISK(tmpfs), and lose after power off." 8 49
154         fi
155 }
156
157 create_sdcard_img()
158 {
159         case "$fs" in
160                 vfat|fuseblk)
161                         mkdir sd
162                         sdcard=sd/sdcard.img
163                         ;;
164                 *)
165                         sdcard=data/sdcard.img
166                         ;;
167         esac
168
169         if create_img 2047 $sdcard; then
170                 losetup /dev/loop7 $sdcard
171                 newfs_msdos -L sdcard /dev/loop7 > /dev/tty7 2>&1
172         fi
173         if [ $? -eq 0 ]; then
174                 sed -i "s|\(SDCARD=\S\+\?\s*\)||; s|\(SRC=\S\+\?\s*\)|\1 SDCARD=/$sdcard|" $menulst
175                 msg="The fake SD card is created successfully"
176         else
177                 msg="Failed to create a fake SD card"
178         fi
179         cd /
180         dialog --ok-label Reboot --msgbox "\n $msg." 7 47
181 }
182
183 install_to()
184 {
185         cd /
186         mountpoint -q /hd && umount /hd
187         while [ 1 ]; do
188                 format_fs $1
189                 try_mount rw /dev/$1 /hd && break
190                 dialog --clear --title " Error " --defaultno --yesno \
191                         "\n Cannot mount /dev/$1\n Do you want to format it?" 8 37
192                 [ $? -ne 0 ] && return 255
193         done
194
195         fs=`cat /proc/mounts | grep /dev/$1 | awk '{ print $3 }'`
196
197         asrc=android-$VER
198         dialog --title " Confirm " --no-label Skip --yesno \
199                 "\n Do you want to install boot loader GRUB?" 7 47
200         if [ $? -eq 0 ]; then
201                 cp -af /grub /hd
202                 d=0
203                 while [ 1 ]; do
204                         h=`echo $d | awk '{ printf("%c", $1+97) }'`
205                         [ -d /sys/block/[sh]d$h/$1 ] && break
206                         d=$(($d+1))
207                 done
208                 p=$((`echo $1 | cut -b4-`-1))
209                 create_menulst $p
210                 create_winitem $1 $d
211                 rm -f /hd/boot/grub/stage1
212                 echo "setup (hd$d) (hd$d,$p)" | grub > /dev/tty5
213                 [ $? -ne 0 ] && return 255
214         fi
215
216         [ "$fs" != "vfat" ] && dialog --title " Question " --defaultno --yesno \
217                 "\nDo you want to install /system directory as read-write?\n\nLet /system be read-write make it easier to debug, but it needs more disk space and longer installation time." 10 61
218         instal_rw=$?
219
220         files="mnt/$SRC/kernel mnt/$SRC/initrd.img mnt/$SRC/ramdisk.img"
221         [ $instal_rw -eq 0 ] && files="$files android/system" || files="$files mnt/$SRC/system.*"
222         size=0
223         for s in `du -sk $files | awk '{print $1}'`; do
224                 size=$(($size+$s))
225         done
226         mkdir hd/$asrc
227         cd hd/$asrc
228         rm -rf system*
229         ( ( cd /; find $files | $CPIO -H newc -o ) | pv -ns ${size}k | ( $CPIO -iud > /dev/null; echo $? > /tmp/result )) 2>&1 \
230                 | progress_bar "Installing Android-x86" "Expect to write $size KB..."
231         result=$((`cat /tmp/result`*255))
232
233         if [ $result -eq 0 ]; then
234                 chmod 644 *
235                 chown 0.0 *
236                 mv mnt/$SRC/* . && rm -rf mnt
237                 [ -d android ] && mv android/* . && rmdir android
238
239                 case "$fs" in
240                         vfat|fuseblk)
241                                 create_data_img
242                                 ;;
243                         *)
244                                 mkdir data
245                                 ;;
246                 esac
247         fi
248
249         sync
250
251         return $result
252 }
253
254 install_hd()
255 {
256         select_dev || rebooting
257         retval=1
258         case "$choice" in
259                 [sh]d*)
260                         install_to $choice
261                         retval=$?
262                         ;;
263                 Create*)
264                         cfdisk
265                         ;;
266                 Detect*)
267                         dialog --title " Detecting... " --nocancel --pause "" 8 41 1
268                         ;;
269         esac
270         return $retval
271 }
272
273 do_install()
274 {
275         until install_hd; do
276                 if [ $retval -eq 255 ]; then
277                         dialog --title ' Error! ' --yes-label Retry --no-label Reboot \
278                                 --yesno '\nInstallation failed! Please check if you have enough free disk space to install Android-x86.' 8 51
279                         [ $? -eq 1 ] && rebooting
280                 fi
281         done
282
283         [ -n "$VESA" ] || runit="Run Android-x86"
284         dialog --clear --title ' Congratulations! ' \
285                 --menu "\n Android-x86 is installed successfully.\n " 12 51 13 \
286                 "$runit" "" "Create a fake SD card" "" "Reboot" "" 2> $tempfile
287         case "`cat $tempfile`" in
288                 Run*)
289                         cd /android
290                         umount system
291                         if mountpoint -q /sfs; then
292                                 umount /sfs
293                                 mount -o loop /hd/$asrc/system.sfs /sfs
294                                 mount -o loop /sfs/system.img system
295                         else
296                                 mount -o loop /hd/$asrc/system.img system
297                         fi
298                         if [ -d /hd/$asrc/data ]; then
299                                 mount --bind /hd/$asrc/data data
300                         elif [ -e /hd/$asrc/data.img ]; then
301                                 mount -o loop /hd/$asrc/data.img data
302                         fi
303                         ;;
304                 Create*)
305                         create_sdcard_img
306                         rebooting
307                         ;;
308                 *)
309                         rebooting
310                         ;;
311         esac
312 }