# # By Chih-Wei Huang # Last updated 2018/01/16 # # License: GNU Public License # We explicitely grant the right to use the scripts # with Android-x86 project. # tempfile=/tmp/temp-$$ menufile=/tmp/menu-$$ CPIO=cpio rebooting() { dialog --title " Rebooting... " --nocancel --pause "" 8 41 1 sync umount -a reboot -f } choose() { dialog --clear --title " $1 " \ --menu "$2" 21 79 13 --file $menufile 2> $tempfile retval=$? choice=`cat $tempfile` } size_gb() { printf %0.2fGB $(dc `cat $1/size` 2097152 / p) } partition_drive() { echo -n > $menufile for i in /sys/block/[shv]d[a-z] /sys/block/mmcblk? /sys/block/nvme*; do [ -d $i ] || continue # pathname expansion failed echo -n `basename $i` >> $menufile if [ -f $i/removable -a `cat $i/removable` -eq 0 ]; then echo -n ' "Harddisk ' >> $menufile else echo -n ' "Removable' >> $menufile fi if [ -f $i/size ]; then sz=$(size_gb $i) [ "$sz" = "0.00GB" ] && sz="<0.01GB" printf " %10s" $sz >> $menufile fi for f in $i/device/model $i/*/name; do [ -e $f ] && echo -n " `sed $'s/\x04//g' $f`" >> $menufile && break done [ "`basename $i`" = "$booted_from" -o -d $i/$booted_from ] && echo -n " *" >> $menufile echo '"' >> $menufile done count=`wc -l $menufile | awk '{ print $1 }'` if [ $count -eq 0 ]; then dialog --title " Error " --msgbox \ "\nOK. There is no hard drive to edit partitions." 8 49 return 255 fi if [ $count -eq 1 ]; then choice=`awk '{ print $1 }' $menufile` retval=0 else choose "Choose Drive" "Please select a drive to edit partitions:\n\n* - Installer source" fi if [ $retval -eq 0 ]; then dialog --title " Confirm " --defaultno --yesno "\n Do you want to use GPT?" 7 29 if [ $? -eq 0 ]; then cgdisk /dev/$choice else cfdisk /dev/$choice fi if [ $? -eq 0 ]; then retval=1 else retval=255 fi fi return $retval } select_dev() { blkid | grep -v -E "^/dev/block/|^/dev/loop" | cut -b6- | sort | awk '{ l="" t="unknown" sub(/:/, "", $1) for (i = NF; i > 1; --i) if (match($i, "^TYPE")) { t=$i gsub(/TYPE=|"/, "", t) } else if (match($i, "^LABEL")) { l=$i gsub(/LABEL=|"/, "", l) } printf("%-11s%-12s%-18s\n", $1, t, l) }' > $tempfile for d in `ls /sys/block`; do for i in /sys/block/$d/$d*; do [ -d $i ] || continue echo $i | grep -qE "loop|ram|sr|boot|rpmb" && continue f=$(grep "`basename $i`" $tempfile || printf "%-11s%-29s" `basename $i` unknown) sz=$(size_gb $i) [ "$sz" = "0.00GB" ] || printf "$f%10s\n" $sz done done | awk -v b=$booted_from '{ if (!match($1, b)) { printf("\"%s\" \"", $0) system("cd /sys/block/*/"$1"; for f in ../device/model ../device/name; do [ -e $f ] && printf %-17s \"`cat $f`\" && break; done") printf("\"\n") } } END { printf("\"\" \"\"\n\"Create/Modify partitions\" \"\"\n\"Detect devices\" \"\"") }' > $menufile 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" return $retval } progress_bar() { dialog --clear --title " $1 " --gauge "\n $2" 8 70 } convert_fs() { if blkid /dev/$1 | grep -q ext2; then /system/bin/tune2fs -j /dev/$1 e2fsck -fy /dev/$1 fi if blkid /dev/$1 | grep -q ext3; then /system/bin/tune2fs -O extents,uninit_bg /dev/$1 e2fsck -fy /dev/$1 fi } format_fs() { local cmd echo -e '"Do not format" ""\next4 ""\nntfs ""\nfat32 ""' > $menufile choose "Choose filesystem" "Please select a filesystem to format $1:" case "$choice" in ext4) cmd="make_ext4fs -L" ;; ntfs) cmd="mkntfs -fL" ;; fat32) cmd="mkdosfs -n" ;; *) ;; esac if [ -n "$cmd" ]; then dialog --title " Confirm " --defaultno --yesno \ "\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 [ $? -ne 0 ] && return 1 $cmd Android-x86 /dev/$1 | awk '{ # FIXME: very imprecise progress if (match($0, "done")) printf("%d\n", i+=33) }' | progress_bar "Formatting" "Formatting partition $1..." elif blkid /dev/$1 | grep -q ext[23]; then dialog --clear --title " Warning " --yesno \ "\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 [ $? -eq 0 ] && convert_fs $1 fi } create_entry() { title=$1 shift echo -e "title $title\n\tkernel /$asrc/kernel$vga $@ SRC=/$asrc\n\tinitrd /$asrc/initrd.img\n" >> $menulst } create_menulst() { menulst=/hd/grub/menu.lst [ -n "$VESA" ] && vga=" vga=788 modeset=0" echo -e "${GRUB_OPTIONS:-default=0\ntimeout=6\nsplashimage=/grub/android-x86.xpm.gz\n}root (hd0,$1)\n" > $menulst create_entry "Android-x86 $VER" quiet $cmdline create_entry "Android-x86 $VER (Debug mode)" $cmdline DEBUG=2 create_entry "Android-x86 $VER (Debug nomodeset)" nomodeset $cmdline DEBUG=2 create_entry "Android-x86 $VER (Debug video=LVDS-1:d)" video=LVDS-1:d $cmdline DEBUG=2 } create_winitem() { win=`fdisk -l /dev/$(echo $1 | cut -b-3) | grep ^/dev | cut -b6-12,55- | awk '{ if (match($2, "NTFS")) print $1 }' | head -1` if [ -n "$win" ]; then dialog --title " Confirm " --yesno \ "\nThe installer found a Windows partition in /dev/$win.\n\nDo you want to create a boot item for Windows?" 9 59 [ $? -ne 0 ] && return 1 wp=$((`echo $win | cut -b4-`-1)) echo -e "title Windows\n\trootnoverify (hd$d,$wp)\n\tchainloader +1\n" >> $menulst fi } check_data_img() { losetup /dev/loop7 data.img if blkid /dev/loop7 | grep -q ext[23]; then dialog --clear --title " Warning " --yesno \ "\nYour data.img is an ext2/3 filesystem. We suggest you convert it to ext4 for better reliability." 8 58 [ $? -eq 0 ] && convert_fs loop7 fi losetup -d /dev/loop7 } create_img() { bname=`basename $2` if [ -e $2 ]; then dialog --title " Confirm " --defaultno --yesno \ "\n $bname exists. Overwrite it?" 7 38 [ $? -ne 0 ] && return 255 rm -f $2 fi dialog --title " Question " --nook --nocancel --inputbox \ "\nPlease input the size of the $bname in MB:" 8 63 $1 2> $tempfile size=`cat $tempfile` [ 0$size -le 0 ] && size=1024 ( dd bs=1M count=$size if=/dev/zero | pv -ns ${size}m | dd of=$2 ) 2>&1 \ | progress_bar "Creating $bname" "Expect to write $size MB..." } create_data_img() { dialog --title " Confirm " --yesno \ "\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 if [ $? -eq 0 ]; then if create_img 512 data.img; then losetup /dev/loop6 data.img make_ext4fs -L /data /dev/loop6 > /dev/tty6 fi [ $? -ne 0 ] && dialog --msgbox "\n Failed to create data.img." 7 33 else dialog --title " Warning " --msgbox \ "\nOK. So data will be save to a RAMDISK(tmpfs), and lose after power off." 8 49 fi } try_upgrade() { [ -d $1 ] && return 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" for v in $PREV_VERS; do local prev if [ -d hd/$v ]; then prev=hd/$v elif [ -d hd/android-$v ]; then prev=hd/android-$v else continue fi dialog --title " Question " --yesno \ "\nAn older version $v is detected.\nWould you like to upgrade it?" 8 51 if [ $? -eq 0 ]; then mv $prev $1 rm -rf $1/data/dalvik-cache/* $1/data/system/wpa_supplicant sed -i 's/\(ctrl_interface=\)\(.*\)/\1wlan0/' $1/data/misc/wifi/wpa_supplicant.conf break fi done } get_part_info() { d=0 while [ 1 ]; do h=`echo $d | awk '{ printf("%c", $1+97) }'` for part in /sys/block/[shv]d$h/$1 /sys/block/mmcblk$d/$1 /sys/block/nvme0n$(($d+1))/$1; do [ -d $part ] && break 2 done d=$(($d+1)) done p=`cat $part/partition` disk=$(basename `dirname $part`) } install_to() { [ -b /dev/$1 ] || return 1 cd / mountpoint -q /hd && umount /hd while [ 1 ]; do format_fs $1 try_mount rw /dev/$1 /hd && break dialog --clear --title " Error " --defaultno --yesno \ "\n Cannot mount /dev/$1\n Do you want to format it?" 8 37 [ $? -ne 0 ] && return 255 done fs=`cat /proc/mounts | grep /dev/$1 | awk '{ print $3 }'` 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` [ -n "$INSTALL_PREFIX" ] && asrc=$INSTALL_PREFIX || asrc=android-$VER efi="`dmesg | grep EFI.VGA`" [ -z "$efi" ] && dialog --title " Confirm " --no-label Skip --defaultno --yesno \ "\n Do you want to install boot loader GRUB?" 7 47 if [ $? -eq 0 ]; then cp -af /grub /hd get_part_info $1 p=$(($p-1)) create_menulst $p create_winitem $1 $d rm -f /hd/boot/grub/stage1 echo "(hd$d) /dev/$disk" > /hd/grub/device.map echo "setup (hd$d) (hd$d,$p)" | grub --device-map /hd/grub/device.map > /dev/tty5 [ $? -ne 0 ] && return 255 fi [ -n "$efi" ] && dialog --title " Confirm " --no-label Skip --defaultno --yesno \ "\n Do you want to install EFI GRUB2?" 7 39 if [ $? -eq 0 ]; then get_part_info $1 for i in /sys/block/$disk/$disk*; do [ 0`cat $i/partition` -eq 1 ] && b=$i [ $(blkid /dev/`basename $i` | grep -c vfat) -ne 0 ] && b=$i && break done boot=`basename $b` bootp=`cat $b/partition` mountpoint -q /hd && umount /hd while [ 1 ]; do try_mount rw /dev/$boot /hd && break dialog --title " Confirm " --defaultno --yesno "\n Cannot mount /dev/$boot.\n Do you want to format it?" 8 37 [ $? -eq 0 ] && mkdosfs -n EFI /dev/$boot done mkdir -p hd/boot/grub hd/efi/Android cp -af grub2/efi/boot/* hd/efi/Android grubcfg=/hd/boot/grub/grub.cfg # Our grub-efi doesn't support ntfs directly. # Copy boot files to ESP so grub-efi could read them if [ "$fs" = "fuseblk" ]; then cp -f src/kernel src/initrd.img hd/efi/Android kdir=efi/Android else kdir=$asrc fi echo -e "set timeout=5\n\n" > $grubcfg 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 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 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 # Checking for old EFI entries, removing them and adding new depending on bitness mount -t efivarfs none /sys/firmware/efi/efivars efibootmgr | grep -Eo ".{0,6}Android-x86" | cut -c1-4 > /tmp/efientries if [ -s /tmp/efientries ]; then dialog --title " Question " --defaultno --yesno "\nEFI boot entries for previous Android-x86 installations were found.\n\nDo you wish to delete them?" 10 61 [ $? -eq 0 ] && while read entry; do efibootmgr -Bb "$entry" > /dev/tty4 2>&1; done < /tmp/efientries fi if [ "$(cat /sys/firmware/efi/fw_platform_size)" == "32" ]; then bootefi=bootia32.efi else bootefi=BOOTx64.EFI fi efibootmgr -v -c -d /dev/$disk -p $bootp -L "Android-x86 $VER" -l /efi/Android/$bootefi > /dev/tty4 2>&1 mountpoint -q /hd && umount /hd try_mount rw /dev/$1 /hd fi dialog --title " Question " --defaultno --yesno \ "\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 instal_rw=$? files="mnt/$SRC/kernel mnt/$SRC/initrd.img mnt/$SRC/$RAMDISK" if [ $instal_rw -eq 0 ]; then if [ "$fs" = "vfat" -o "$fs" = "fuseblk" ]; then [ -e /sfs/system.img ] && sysimg="/sfs/system.img" || sysimg="mnt/$SRC/system.*" else sysimg="android/system" fi else sysimg="mnt/$SRC/system.*" fi files="$files $sysimg" size=0 for s in `du -sk $files | awk '{print $1}'`; do size=$(($size+$s)) done try_upgrade hd/$asrc mkdir -p hd/$asrc cd hd/$asrc rm -rf system* ( ( cd /; find $files | $CPIO -H newc -o ) | pv -ns ${size}k | ( $CPIO -iud > /dev/null; echo $? > /tmp/result )) 2>&1 \ | progress_bar "Installing Android-x86" "Expect to write $size KB..." result=$((`cat /tmp/result`*255)) if [ $result -eq 0 ]; then for d in android mnt sfs ./$SRC; do [ -d $d ] && mv $d/* . && rmdir $d done chown 0.0 * for f in *; do [ -d $f ] || chmod 644 $f done case "$fs" in vfat|fuseblk) [ -e data.img ] && check_data_img || create_data_img ;; *) mkdir -p data ;; esac fi dialog --infobox "\n Syncing to disk..." 5 27 sync return $result } install_hd() { select_dev || rebooting retval=1 case "$choice" in Create*) partition_drive retval=$? ;; Detect*) dialog --title " Detecting... " --nocancel --pause "" 8 41 1 ;; *) install_to $choice retval=$? ;; esac return $retval } do_install() { booted_from=`basename $dev` until install_hd; do if [ $retval -eq 255 ]; then dialog --title ' Error! ' --yes-label Retry --no-label Reboot \ --yesno '\nInstallation failed! Please check if you have enough free disk space to install Android-x86.' 8 51 [ $? -eq 1 ] && rebooting fi done [ -n "$VESA" ] || runit="Run Android-x86" dialog --clear --title ' Congratulations! ' \ --menu "\n Android-x86 is installed successfully.\n " 11 51 13 \ "$runit" "" "Reboot" "" 2> $tempfile case "`cat $tempfile`" in Run*) cd /android umount system mountpoint -q /sfs && umount /sfs if [ -e /hd/$asrc/system.sfs ]; then mount -o loop /hd/$asrc/system.sfs /sfs mount -o loop /sfs/system.img system elif [ -e /hd/$asrc/system.img ]; then mount -o loop /hd/$asrc/system.img system else mount --bind /hd/$asrc/system system fi if [ -d /hd/$asrc/data ]; then mount --bind /hd/$asrc/data data elif [ -e /hd/$asrc/data.img ]; then mount -o loop /hd/$asrc/data.img data fi ;; *) rebooting ;; esac }