OSDN Git Service

1-install: disallow to install read-write mode on ntfs
[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=/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 "${GRUB_OPTIONS:-default=0\ntimeout=6\nsplashimage=/grub/android-x86.xpm.gz\n}root (hd0,$1)\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         create_entry "Android-x86 $VER" quiet $cmdline
100         create_entry "Android-x86 $VER (Debug mode)" $cmdline DEBUG=2
101         create_entry "Android-x86 $VER (Debug nomodeset)" nomodeset $cmdline DEBUG=2
102         create_entry "Android-x86 $VER (Debug video=LVDS-1:d)" video=LVDS-1:d $cmdline DEBUG=2
103 }
104
105 create_winitem()
106 {
107         win=`fdisk -l /dev/$(echo $1 | cut -b-3) | grep ^/dev | cut -b6-12,55- | awk '{
108                 if (match($2, "NTFS"))
109                         print $1
110         }' | head -1`
111         if [ -n "$win" ]; then
112                 dialog --title " Confirm " --yesno \
113                         "\nThe installer found a Windows partition in /dev/$win.\n\nDo you want to create a boot item for Windows?" 9 59
114                 [ $? -ne 0 ] && return 1
115                 wp=$((`echo $win | cut -b4-`-1))
116                 echo -e "title Windows\n\trootnoverify (hd$d,$wp)\n\tchainloader +1\n" >> $menulst
117         fi
118 }
119
120 create_img()
121 {
122         bname=`basename $2`
123         if [ -e $2 ]; then
124                 dialog --title " Confirm " --defaultno --yesno \
125                         "\n $bname exists. Overwrite it?" 7 38
126                 [ $? -ne 0 ] && return 255
127                 rm -f $2
128         fi
129         dialog --title " Question " --nook --nocancel --inputbox \
130                 "\nPlease input the size of the $bname in MB (max 2047):" 8 63 $1 2> $tempfile
131         size=`cat $tempfile`
132         [ 0$size -le 0 -o 0$size -gt 2047 ] && size=2047
133         ( dd bs=1M count=$size if=/dev/zero | pv -ns ${size}m | dd of=$2 ) 2>&1 \
134                 | progress_bar "Creating $bname" "Expect to write $size MB..."
135 }
136
137 create_data_img()
138 {
139         dialog --title " Confirm " --yesno \
140                 "\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
141
142         if [ $? -eq 0 ]; then
143                 if create_img 512 data.img; then
144                         losetup /dev/loop6 data.img
145                         mke2fs -jL /data /dev/loop6 > /dev/tty6
146                 fi
147                 [ $? -ne 0 ] && dialog --msgbox "\n Failed to create data.img." 7 33
148         else
149                 dialog --title " Warning " --msgbox \
150                         "\nOK. So data will be save to a RAMDISK(tmpfs), and lose after power off." 8 49
151         fi
152 }
153
154 install_to()
155 {
156         cd /
157         mountpoint -q /hd && umount /hd
158         while [ 1 ]; do
159                 format_fs $1
160                 try_mount rw /dev/$1 /hd && break
161                 dialog --clear --title " Error " --defaultno --yesno \
162                         "\n Cannot mount /dev/$1\n Do you want to format it?" 8 37
163                 [ $? -ne 0 ] && return 255
164         done
165
166         fs=`cat /proc/mounts | grep /dev/$1 | awk '{ print $3 }'`
167
168         asrc=android-$VER
169         dialog --title " Confirm " --no-label Skip --defaultno --yesno \
170                 "\n Do you want to install boot loader GRUB?" 7 47
171         if [ $? -eq 0 ]; then
172                 cp -af /grub /hd
173                 d=0
174                 while [ 1 ]; do
175                         h=`echo $d | awk '{ printf("%c", $1+97) }'`
176                         [ -d /sys/block/[sh]d$h/$1 ] && break
177                         d=$(($d+1))
178                 done
179                 p=$((`echo $1 | cut -b4-`-1))
180                 create_menulst $p
181                 create_winitem $1 $d
182                 rm -f /hd/boot/grub/stage1
183                 echo "setup (hd$d) (hd$d,$p)" | grub > /dev/tty5
184                 [ $? -ne 0 ] && return 255
185         fi
186
187         [ "$fs" != "vfat" -a "$fs" != "fuseblk" ] && dialog --title " Question " --yesno \
188                 "\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
189         instal_rw=$?
190
191         files="mnt/$SRC/kernel mnt/$SRC/initrd.img mnt/$SRC/ramdisk.img"
192         [ $instal_rw -eq 0 ] && files="$files android/system" || files="$files mnt/$SRC/system.*"
193         size=0
194         for s in `du -sk $files | awk '{print $1}'`; do
195                 size=$(($size+$s))
196         done
197         mkdir hd/$asrc
198         cd hd/$asrc
199         rm -rf system*
200         ( ( cd /; find $files | $CPIO -H newc -o ) | pv -ns ${size}k | ( $CPIO -iud > /dev/null; echo $? > /tmp/result )) 2>&1 \
201                 | progress_bar "Installing Android-x86" "Expect to write $size KB..."
202         result=$((`cat /tmp/result`*255))
203
204         if [ $result -eq 0 ]; then
205                 chmod 644 *
206                 chown 0.0 *
207                 mv mnt/$SRC/* . && rm -rf mnt
208                 [ -d android ] && mv android/* . && rmdir android
209
210                 case "$fs" in
211                         vfat|fuseblk)
212                                 create_data_img
213                                 ;;
214                         *)
215                                 mkdir data
216                                 ;;
217                 esac
218         fi
219
220         sync
221
222         return $result
223 }
224
225 install_hd()
226 {
227         select_dev || rebooting
228         retval=1
229         case "$choice" in
230                 [sh]d*)
231                         install_to $choice
232                         retval=$?
233                         ;;
234                 Create*)
235                         cfdisk
236                         ;;
237                 Detect*)
238                         dialog --title " Detecting... " --nocancel --pause "" 8 41 1
239                         ;;
240         esac
241         return $retval
242 }
243
244 do_install()
245 {
246         until install_hd; do
247                 if [ $retval -eq 255 ]; then
248                         dialog --title ' Error! ' --yes-label Retry --no-label Reboot \
249                                 --yesno '\nInstallation failed! Please check if you have enough free disk space to install Android-x86.' 8 51
250                         [ $? -eq 1 ] && rebooting
251                 fi
252         done
253
254         [ -n "$VESA" ] || runit="Run Android-x86"
255         dialog --clear --title ' Congratulations! ' \
256                 --menu "\n Android-x86 is installed successfully.\n " 11 51 13 \
257                 "$runit" "" "Reboot" "" 2> $tempfile
258         case "`cat $tempfile`" in
259                 Run*)
260                         cd /android
261                         umount system
262                         if mountpoint -q /sfs; then
263                                 umount /sfs
264                                 mount -o loop /hd/$asrc/system.sfs /sfs
265                                 mount -o loop /sfs/system.img system
266                         else
267                                 mount -o loop /hd/$asrc/system.img system
268                         fi
269                         if [ -d /hd/$asrc/data ]; then
270                                 mount --bind /hd/$asrc/data data
271                         elif [ -e /hd/$asrc/data.img ]; then
272                                 mount -o loop /hd/$asrc/data.img data
273                         fi
274                         ;;
275                 *)
276                         rebooting
277                         ;;
278         esac
279 }