OSDN Git Service

installer: support read-write installation
[android-x86/bootable-newinstaller.git] / install / scripts / 1-install
1 #
2 # By Chih-Wei Huang <cwhuang@linux.org.tw>
3 # Last updated 2010/10/14
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 rebooting()
14 {
15         dialog --title " Rebooting... " --nocancel --pause "" 8 41 1
16         sync
17         umount -a
18         reboot -f
19 }
20
21 choose()
22 {
23         dialog --clear --title " $1 " \
24                 --menu "$2" 20 71 13 --file $menufile 2> $tempfile
25
26         retval=$?
27         choice=`cat $tempfile`
28 }
29
30 select_dev()
31 {
32         fdisk -l | grep ^/dev | cut -b6-12,55- | awk '{
33                 if (!match($2, "Extended")) {
34                         printf("\"%-28s", $0)
35                         system("echo -n `cat /sys/block/*/"$1"/../device/model`")
36                         printf("\" \"\"\n")
37                 }
38         } END {
39                 printf("\"Create/Modify partitions\" \"\"\n\"Detect devices\" \"\"")
40         }' > $menufile
41         choose "Choose Partition" "Please select a partition to install Android-x86:"
42         return $retval
43 }
44
45 progress_bar()
46 {
47         dialog --clear --title " $1 " --gauge "\n $2" 8 70
48 }
49
50 format_fs()
51 {
52         local cmd
53         echo -e '"Do not format" ""\next3 ""\next2 ""\nntfs ""\nfat32 ""' > $menufile
54         choose "Choose filesystem" "Please select a filesystem to format $1:"
55         case "$choice" in
56                 ext3)
57                         cmd="mke2fs -jL"
58                         ;;
59                 ext2)
60                         cmd="mke2fs -L"
61                         ;;
62                 ntfs)
63                         cmd="mkntfs -fL"
64                         ;;
65                 fat32)
66                         cmd="mkdosfs -vn"
67                         ;;
68                 *)
69                         ;;
70         esac
71         if [ -n "$cmd" ]; then
72                 dialog --title " Confirm " --no-label Skip --yesno \
73                         "\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
74                 [ $? -ne 0 ] && return 1
75                 $cmd Android-x86 /dev/$1 | awk '{
76                         # FIXME: very imprecise progress
77                         if (match($0, "done"))
78                                 printf("%d\n", i+=33)
79                 }' | progress_bar "Formatting" "Formatting partition $1..."
80         fi
81 }
82
83 create_entry()
84 {
85         title=$1
86         shift
87         echo -e "title $title\n\tkernel /$asrc/kernel$vga $@ SRC=/$asrc\n\tinitrd /$asrc/initrd.img\n" >> $menulst
88 }
89
90 create_menulst()
91 {
92         menulst=/hd/grub/menu.lst
93         [ -n "$VESA" ] && vga=" vga=788 modeset=0"
94         echo -e "default=0\ntimeout=6\nroot (hd0,$1)\nsplashimage=/grub/android-x86.xpm.gz\n" > $menulst
95         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.*\)||"`
96
97         if grep -q ^ro.sf.lcd_density /android/default.prop /android/system/build.prop; then
98                 create_entry "Android-x86 $VER" quiet $cmdline
99         else
100                 create_entry "Android-x86 $VER (HDPI)" quiet $cmdline DPI=240
101                 create_entry "Android-x86 $VER (MDPI)" quiet $cmdline DPI=160
102         fi
103         create_entry "Android-x86 $VER (Debug mode)" $cmdline DEBUG=1
104 }
105
106 create_winitem()
107 {
108         win=`fdisk -l /dev/$(echo $1 | cut -b-3) | grep ^/dev | cut -b6-12,55- | awk '{
109                 if (match($2, "NTFS"))
110                         print $1
111         }' | head -1`
112         if [ -n "$win" ]; then
113                 dialog --title " Confirm " --yesno \
114                         "\nThe installer found a Windows partition in /dev/$win.\n\nDo you want to create a boot item for Windows?" 9 59
115                 [ $? -ne 0 ] && return 1
116                 p=$((`echo $win | cut -b4-`-1))
117                 echo -e "title Windows\n\trootnoverify (hd$d,$p)\n\tchainloader +1\n" >> $menulst
118         fi
119 }
120
121 install_to()
122 {
123         mountpoint -q /hd && umount /hd
124         while [ 1 ]; do
125                 format_fs $1
126                 try_mount rw /dev/$1 /hd && break
127                 dialog --clear --title " Error " --defaultno --yesno \
128                         "\n Cannot mount /dev/$1\n Do you want to format it?" 8 37
129                 [ $? -ne 0 ] && return 255
130         done
131
132         fs=`cat /proc/mounts | grep /dev/$1 | awk '{ print $3 }'`
133         if [ "$fs" = "vfat" ]; then
134                 dialog --title " Warning " --yesno \
135                         "\nYou are going to install Android-x86 to a fat32 partition. Though this is possible, android cannot save user data to that partition. So data will be save to a RAMDISK(tmpfs), and lose after power off.\n\nAre you sure to continue?" 12 63
136                 [ $? -ne 0 ] && return 1
137         elif [ "$fs" = "fuseblk" ]; then
138                 dialog --title " Warning " --yesno \
139                         "\nYou are going to install Android-x86 to an NTFS partition. Though this is possible, some user data cannot be saved properly to that partition and will lose after power off.\n\nAre you sure to continue?" 11 63
140                 [ $? -ne 0 ] && return 1
141         fi
142
143         asrc=android-$VER
144         dialog --title " Confirm " --no-label Skip --yesno \
145                 "\n Do you want to install boot loader GRUB?" 7 47
146         if [ $? -eq 0 ]; then
147                 cp -af /grub /hd
148                 d=0
149                 while [ 1 ]; do
150                         h=`echo $d | awk '{ printf("%c", $1+97) }'`
151                         [ -d /sys/block/[sh]d$h/$1 ] && break
152                         d=$(($d+1))
153                 done
154                 p=$((`echo $1 | cut -b4-`-1))
155                 create_menulst $p
156                 create_winitem $1 $d
157                 rm -f /hd/boot/grub/stage1
158                 echo "setup (hd$d) (hd$d,$p)" | grub > /dev/tty5
159                 [ $? -ne 0 ] && return 255
160         fi
161
162         dialog --title " Question " --defaultno --yesno \
163                 "\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
164         instal_rw=$?
165
166         cd /
167         files="mnt/$SRC/kernel mnt/$SRC/initrd.img mnt/$SRC/ramdisk.img"
168         [ $instal_rw -eq 0 ] && files="$files android/system" || files="$files mnt/$SRC/system.*"
169         size=0
170         for s in `du -sk $files | awk '{print $1}'`; do
171                 size=$(($size+$s))
172         done
173         mkdir hd/$asrc
174         cd hd/$asrc
175         rm -rf system*
176         ( ( cd /; find $files | cpio -H newc -o ) | pv -ns ${size}k | ( cpio -iud > /dev/null; echo $? > /tmp/result )) 2>&1 \
177                 | progress_bar "Installing Android-x86" "Expect to write $size KB..."
178
179         chmod 644 *
180         chown 0.0 *
181         mv mnt/$SRC/* . && rm -rf mnt
182         [ -d android ] && mv android/* . && rmdir android
183
184         [ "$fs" != "vfat" ] && mkdir data
185         sync
186
187         return $((`cat /tmp/result`*255))
188 }
189
190 install_hd()
191 {
192         mdev -s
193         select_dev || rebooting
194         retval=1
195         case "$choice" in
196                 [sh]d*)
197                         install_to $choice
198                         retval=$?
199                         ;;
200                 Create*)
201                         cfdisk
202                         ;;
203                 Detect*)
204                         dialog --title " Detecting... " --nocancel --pause "" 8 41 1
205                         ;;
206         esac
207         return $retval
208 }
209
210 do_install()
211 {
212         until install_hd; do
213                 if [ $retval -eq 255 ]; then
214                         dialog --title ' Error! ' --yes-label Retry --no-label Reboot \
215                                 --yesno '\n      Installation failed!' 7 35
216                         [ $? -eq 1 ] && rebooting
217                 fi
218         done
219
220         [ -n "$VESA" ] || runit="Run Android-x86"
221         dialog --clear --title ' Congratulations! ' \
222                 --menu "\n Android-x86 is installed successfully.\n " 11 51 13 \
223                 "$runit" "" "Reboot" "" 2> $tempfile
224         case "`cat $tempfile`" in
225                 Run*)
226                         cd /android
227                         umount system
228                         if mountpoint -q /sfs; then
229                                 umount /sfs
230                                 mount -o loop /hd/$asrc/system.sfs /sfs
231                                 mount -o loop /sfs/system.img system
232                         else
233                                 mount -o loop /hd/$asrc/system.img system
234                         fi
235                         [ -d /hd/$asrc/data ] && mount --bind /hd/$asrc/data data
236                         ;;
237                 *)
238                         rebooting
239                         ;;
240         esac
241 }