OSDN Git Service

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