2 # By Chih-Wei Huang <cwhuang@linux.org.tw>
3 # Last updated 2018/01/16
5 # License: GNU Public License
6 # We explicitely grant the right to use the scripts
7 # with Android-x86 project.
17 dialog --title " Rebooting... " --nocancel --pause "" 8 41 1
25 dialog --clear --title " $1 " \
26 --menu "$2" 21 79 13 --file $menufile 2> $tempfile
29 choice=`cat $tempfile`
34 printf %0.2fGB $(dc `cat $1/size` 2097152 / p)
40 for i in /sys/block/[shv]d[a-z] /sys/block/mmcblk? /sys/block/nvme*; do
41 [ -d $i ] || continue # pathname expansion failed
42 echo -n `basename $i` >> $menufile
43 if [ -f $i/removable -a `cat $i/removable` -eq 0 ]; then
44 echo -n ' "Harddisk ' >> $menufile
46 echo -n ' "Removable' >> $menufile
48 if [ -f $i/size ]; then
50 [ "$sz" = "0.00GB" ] && sz="<0.01GB"
51 printf " %10s" $sz >> $menufile
53 for f in $i/device/model $i/*/name; do
54 [ -e $f ] && echo -n " `sed $'s/\x04//g' $f`" >> $menufile && break
56 [ "`basename $i`" = "$booted_from" -o -d $i/$booted_from ] && echo -n " *" >> $menufile
59 count=`wc -l $menufile | awk '{ print $1 }'`
60 if [ $count -eq 0 ]; then
61 dialog --title " Error " --msgbox \
62 "\nOK. There is no hard drive to edit partitions." 8 49
65 if [ $count -eq 1 ]; then
66 choice=`awk '{ print $1 }' $menufile`
69 choose "Choose Drive" "Please select a drive to edit partitions:\n\n* - Installer source"
71 if [ $retval -eq 0 ]; then
72 dialog --title " Confirm " --defaultno --yesno "\n Do you want to use GPT?" 7 29
89 blkid | grep -v -E "^/dev/block/|^/dev/loop" | cut -b6- | sort | awk '{
93 for (i = NF; i > 1; --i)
94 if (match($i, "^TYPE")) {
96 gsub(/TYPE=|"/, "", t)
97 } else if (match($i, "^LABEL")) {
99 gsub(/LABEL=|"/, "", l)
101 printf("%-11s%-12s%-18s\n", $1, t, l)
104 for d in `ls /sys/block`; do
105 for i in /sys/block/$d/$d*; do
106 [ -d $i ] || continue
107 echo $i | grep -qE "loop|ram|sr|boot|rpmb" && continue
108 f=$(grep "`basename $i`" $tempfile || printf "%-11s%-29s" `basename $i` unknown)
110 [ "$sz" = "0.00GB" ] || printf "$f%10s\n" $sz
112 done | awk -v b=$booted_from '{
114 printf("\"%s\" \"", $0)
115 system("cd /sys/block/*/"$1"; for f in ../device/model ../device/name; do [ -e $f ] && printf %-17s \"`cat $f`\" && break; done")
119 printf("\"\" \"\"\n\"Create/Modify partitions\" \"\"\n\"Detect devices\" \"\"")
121 choose "Choose Partition" "Please select a partition to install Android-x86:\n\nRecommended minimum free space - 4GB | Optimum free space >= 8GB\n\nPartition | Filesystem | Label | Size | Drive name/model"
127 dialog --clear --title " $1 " --gauge "\n $2" 8 70
132 if blkid /dev/$1 | grep -q ext2; then
133 /system/bin/tune2fs -j /dev/$1
136 if blkid /dev/$1 | grep -q ext3; then
137 /system/bin/tune2fs -O extents,uninit_bg /dev/$1
145 echo -e '"Do not format" ""\next4 ""\nntfs ""\nfat32 ""' > $menufile
146 choose "Choose filesystem" "Please select a filesystem to format $1:"
160 if [ -n "$cmd" ]; then
161 dialog --title " Confirm " --defaultno --yesno \
162 "\n You chose to format $1 to $choice.\n All data in that partition will be LOST.\n\n Are you sure to format the partition $1?" 10 51
163 [ $? -ne 0 ] && return 1
164 $cmd Android-x86 /dev/$1 | awk '{
165 # FIXME: very imprecise progress
166 if (match($0, "done"))
167 printf("%d\n", i+=33)
168 }' | progress_bar "Formatting" "Formatting partition $1..."
169 elif blkid /dev/$1 | grep -q ext[23]; then
170 dialog --clear --title " Warning " --yesno \
171 "\nYou chose to install android-x86 to an ext2/3 filesystem. We suggest you convert it to ext4 for better reliability and performance." 9 62
172 [ $? -eq 0 ] && convert_fs $1
180 echo -e "title $title\n\tkernel /$asrc/kernel$vga $@ SRC=/$asrc\n\tinitrd /$asrc/initrd.img\n" >> $menulst
185 menulst=/hd/grub/menu.lst
186 [ -n "$VESA" ] && vga=" vga=788 modeset=0"
187 echo -e "${GRUB_OPTIONS:-default=0\ntimeout=6\nsplashimage=/grub/android-x86.xpm.gz\n}root (hd0,$1)\n" > $menulst
189 create_entry "Android-x86 $VER" quiet $cmdline
190 create_entry "Android-x86 $VER (Debug mode)" $cmdline DEBUG=2
191 create_entry "Android-x86 $VER (Debug nomodeset)" nomodeset $cmdline DEBUG=2
192 create_entry "Android-x86 $VER (Debug video=LVDS-1:d)" video=LVDS-1:d $cmdline DEBUG=2
197 win=`fdisk -l /dev/$(echo $1 | cut -b-3) | grep ^/dev | cut -b6-12,55- | awk '{
198 if (match($2, "NTFS"))
201 if [ -n "$win" ]; then
202 dialog --title " Confirm " --yesno \
203 "\nThe installer found a Windows partition in /dev/$win.\n\nDo you want to create a boot item for Windows?" 9 59
204 [ $? -ne 0 ] && return 1
205 wp=$((`echo $win | cut -b4-`-1))
206 echo -e "title Windows\n\trootnoverify (hd$d,$wp)\n\tchainloader +1\n" >> $menulst
212 losetup /dev/loop7 data.img
213 if blkid /dev/loop7 | grep -q ext[23]; then
214 dialog --clear --title " Warning " --yesno \
215 "\nYour data.img is an ext2/3 filesystem. We suggest you convert it to ext4 for better reliability." 8 58
216 [ $? -eq 0 ] && convert_fs loop7
218 losetup -d /dev/loop7
225 dialog --title " Confirm " --defaultno --yesno \
226 "\n $bname exists. Overwrite it?" 7 38
227 [ $? -ne 0 ] && return 255
230 dialog --title " Question " --nook --nocancel --inputbox \
231 "\nPlease input the size of the $bname in MB:" 8 63 $1 2> $tempfile
233 [ 0$size -le 0 ] && size=1024
234 ( dd bs=1M count=$size if=/dev/zero | pv -ns ${size}m | dd of=$2 ) 2>&1 \
235 | progress_bar "Creating $bname" "Expect to write $size MB..."
240 dialog --title " Confirm " --yesno \
241 "\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
243 if [ $? -eq 0 ]; then
244 if create_img 512 data.img; then
245 losetup /dev/loop6 data.img
246 make_ext4fs -L /data /dev/loop6 > /dev/tty6
248 [ $? -ne 0 ] && dialog --msgbox "\n Failed to create data.img." 7 33
250 dialog --title " Warning " --msgbox \
251 "\nOK. So data will be save to a RAMDISK(tmpfs), and lose after power off." 8 49
258 PREV_VERS="$PREV_VERS 7.1-rc2 7.1-rc1 6.0-r3 6.0-r2 6.0-r1 6.0-rc2 6.0-rc1 5.1-rc1 4.4-r5 4.4-r4 4.4-r3 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"
259 for v in $PREV_VERS; do
261 if [ -d hd/$v ]; then
263 elif [ -d hd/android-$v ]; then
268 dialog --title " Question " --yesno \
269 "\nAn older version $v is detected.\nWould you like to upgrade it?" 8 51
270 if [ $? -eq 0 ]; then
272 rm -rf $1/data/dalvik-cache/* $1/data/system/wpa_supplicant
273 sed -i 's/\(ctrl_interface=\)\(.*\)/\1wlan0/' $1/data/misc/wifi/wpa_supplicant.conf
283 h=`echo $d | awk '{ printf("%c", $1+97) }'`
284 for part in /sys/block/[shv]d$h/$1 /sys/block/mmcblk$d/$1 /sys/block/nvme0n$(($d+1))/$1; do
285 [ -d $part ] && break 2
289 p=`cat $part/partition`
290 disk=$(basename `dirname $part`)
295 [ -b /dev/$1 ] || return 1
297 mountpoint -q /hd && umount /hd
300 try_mount rw /dev/$1 /hd && break
301 dialog --clear --title " Error " --defaultno --yesno \
302 "\n Cannot mount /dev/$1\n Do you want to format it?" 8 37
303 [ $? -ne 0 ] && return 255
306 fs=`cat /proc/mounts | grep /dev/$1 | awk '{ print $3 }'`
307 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=\S\+\?\s*\)||; s|\(iso-scan/filename=\S\+\?\s*\)||;" /proc/cmdline`
309 [ -n "$INSTALL_PREFIX" ] && asrc=$INSTALL_PREFIX || asrc=android-$VER
310 [ -z "$efi" ] && dialog --title " Confirm " --no-label Skip --defaultno --yesno \
311 "\n Do you want to install boot loader GRUB?" 7 47
312 if [ $? -eq 0 ]; then
318 rm -f /hd/boot/grub/stage1
319 echo "(hd$d) /dev/$disk" > /hd/grub/device.map
320 echo "setup (hd$d) (hd$d,$p)" | grub --device-map /hd/grub/device.map > /dev/tty5
321 [ $? -ne 0 ] && return 255
324 [ -n "$efi" ] && dialog --title " Confirm " --no-label Skip --defaultno --yesno \
325 "\n Do you want to install EFI GRUB2?" 7 39
326 if [ $? -eq 0 ]; then
328 for i in /sys/block/$disk/$disk*; do
329 [ 0`cat $i/partition` -eq 1 ] && b=$i
330 [ $(blkid /dev/`basename $i` | grep -c vfat) -ne 0 ] && b=$i && break
333 bootp=`cat $b/partition`
334 mountpoint -q /hd && umount /hd
336 try_mount rw /dev/$boot /hd && break
337 dialog --title " Confirm " --defaultno --yesno "\n Cannot mount /dev/$boot.\n Do you want to format it?" 8 37
338 [ $? -eq 0 ] && mkdosfs -n EFI /dev/$boot
340 if [ "$efi" = "32" ]; then
341 grubcfg=hd/boot/grub/i386-efi/grub.cfg
344 grubcfg=hd/boot/grub/x86_64-efi/grub.cfg
347 mkdir -p `dirname $grubcfg` hd/efi/Android
348 cp -af grub2/efi/boot/* hd/efi/Android
350 # Our grub-efi doesn't support ntfs directly.
351 # Copy boot files to ESP so grub-efi could read them
352 if [ "$fs" = "fuseblk" ]; then
353 cp -f src/kernel src/initrd.img hd/efi/Android
358 echo -e "set timeout=5\n\n" > $grubcfg
359 echo -e "menuentry \"Android-x86 $VER\" {\n\tsearch --set=root --file /$kdir/kernel\n\tlinux /$kdir/kernel SRC=/$asrc quiet $cmdline\n\tinitrd /$kdir/initrd.img\n}" >> $grubcfg
360 echo -e "menuentry \"Android-x86 $VER (DEBUG mode)\" {\n\tsearch --set=root --file /$kdir/kernel\n\tlinux /$kdir/kernel SRC=/$asrc $cmdline DEBUG=2\n\tinitrd /$kdir/initrd.img\n}" >> $grubcfg
361 echo -e '\nset winefi=/EFI/Microsoft/Boot/bootmgfw.efi\nsearch --file --no-floppy --set=win ${winefi}\nif [ -e (${win})/${winefi} ]; then\n\tmenuentry 'Windows' --class windows {\n\t\tset root=${win}\n\t\tchainloader (${root})/${winefi}\n\t}\nfi\n' >> $grubcfg
363 # Checking for old EFI entries, removing them and adding new depending on bitness
364 efibootmgr | grep -Eo ".{0,6}Android-x86" | cut -c1-4 > /tmp/efientries
365 if [ -s /tmp/efientries ]; then
366 dialog --title " Question " --defaultno --yesno "\nEFI boot entries for previous Android-x86 installations were found.\n\nDo you wish to delete them?" 10 61
367 [ $? -eq 0 ] && while read entry; do efibootmgr -Bb "$entry" > /dev/tty4 2>&1; done < /tmp/efientries
369 efibootmgr -v -c -d /dev/$disk -p $bootp -L "Android-x86 $VER" -l /efi/Android/$bootefi > /dev/tty4 2>&1
371 mountpoint -q /hd && umount /hd
372 try_mount rw /dev/$1 /hd
375 dialog --title " Question " --defaultno --yesno \
376 "\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
379 files="mnt/$SRC/kernel mnt/$SRC/initrd.img mnt/$SRC/$RAMDISK"
380 if [ $instal_rw -eq 0 ]; then
381 if [ "$fs" = "vfat" -o "$fs" = "fuseblk" ]; then
382 [ -e /sfs/system.img ] && sysimg="/sfs/system.img" || sysimg="mnt/$SRC/system.*"
384 sysimg="android/system"
387 sysimg="mnt/$SRC/system.*"
389 files="$files $sysimg"
391 for s in `du -sk $files | awk '{print $1}'`; do
398 ( ( cd /; find $files | $CPIO -H newc -o ) | pv -ns ${size}k | ( $CPIO -iud > /dev/null; echo $? > /tmp/result )) 2>&1 \
399 | progress_bar "Installing Android-x86" "Expect to write $size KB..."
400 result=$((`cat /tmp/result`*255))
402 if [ $result -eq 0 ]; then
403 for d in android mnt sfs ./$SRC; do
404 [ -d $d ] && mv $d/* . && rmdir $d
408 [ -d $f ] || chmod 644 $f
413 [ -e data.img ] && check_data_img || create_data_img
421 dialog --infobox "\n Syncing to disk..." 5 27
429 select_dev || rebooting
437 dialog --title " Detecting... " --nocancel --pause "" 8 41 1
449 booted_from=`basename $dev`
450 efi=$(cat /sys/firmware/efi/fw_platform_size 2> /dev/null)
451 [ -n "$efi" ] && mount -t efivarfs none /sys/firmware/efi/efivars
454 if [ $retval -eq 255 ]; then
455 dialog --title ' Error! ' --yes-label Retry --no-label Reboot \
456 --yesno '\nInstallation failed! Please check if you have enough free disk space to install Android-x86.' 8 51
457 [ $? -eq 1 ] && rebooting
461 [ -n "$VESA" ] || runit="Run Android-x86"
462 dialog --clear --title ' Congratulations! ' \
463 --menu "\n Android-x86 is installed successfully.\n " 11 51 13 \
464 "$runit" "" "Reboot" "" 2> $tempfile
465 case "`cat $tempfile`" in
469 mountpoint -q /sfs && umount /sfs
470 if [ -e /hd/$asrc/system.sfs ]; then
471 mount -o loop /hd/$asrc/system.sfs /sfs
472 mount -o loop /sfs/system.img system
473 elif [ -e /hd/$asrc/system.img ]; then
474 mount -o loop /hd/$asrc/system.img system
476 mount --bind /hd/$asrc/system system
478 if [ -d /hd/$asrc/data ]; then
479 mount --bind /hd/$asrc/data data
480 elif [ -e /hd/$asrc/data.img ]; then
481 mount -o loop /hd/$asrc/data.img data