OSDN Git Service

init: support Xen hypervisor device
[android-x86/bootable-newinstaller.git] / install / scripts / 1-install
index 080a965..1a50ebf 100644 (file)
@@ -1,6 +1,5 @@
 #
 # By Chih-Wei Huang <cwhuang@linux.org.tw>
-# Last updated 2012/07/07
 #
 # License: GNU Public License
 # We explicitely grant the right to use the scripts
@@ -11,6 +10,7 @@ tempfile=/tmp/temp-$$
 menufile=/tmp/menu-$$
 
 CPIO=cpio
+OS_TITLE=${OS_TITLE:-Android-x86}
 
 rebooting()
 {
@@ -20,22 +20,78 @@ rebooting()
        reboot -f
 }
 
+auto_answer()
+{
+       echo "$answer" > $tempfile
+       unset answer
+       test "`cat $tempfile`" != "0"
+}
+
+set_answer_if_auto()
+{
+       [ -n "$AUTO_INSTALL" ] && answer="$1"
+}
+
+adialog()
+{
+       if [ -n "$answer" ]; then
+               auto_answer
+       else
+               dialog "$@" 2> $tempfile
+       fi
+}
+
 choose()
 {
-       dialog --clear --title " $1 " \
-               --menu "$2" 20 71 13 --file $menufile 2> $tempfile
+       adialog --clear --title " $1 " --menu "$2" 21 79 13 --file $menufile
 
        retval=$?
        choice=`cat $tempfile`
 }
 
+size_gb()
+{
+       printf %0.2fGB $(dc `cat $1/size` 2097152 / p)
+}
+
+find_partition()
+{
+       grep -H ^$2$ /sys/block/$1/*/partition 2> /dev/null | cut -d/ -f5
+}
+
+list_disks()
+{
+       for b in /sys/block/[shv]d[a-z] /sys/block/xvd[a-z] /sys/block/mmcblk? /sys/block/nvme*; do
+               [ -d $b ] && echo $b
+       done
+}
+
+auto_partition()
+{
+       [ "$AUTO_INSTALL" = "force" ] || dialog --title " Auto Installer " --defaultno --yesno \
+               "\nYou have chosen the AUTO installation.\n\nThe installer will erase the whole /dev/$1 and install $OS_TITLE to it.\n\nThis is the last confirmation. Are you sure to do so?" 12 61
+       [ $? -ne 0 ] && rebooting
+
+       if [ -z "$efi" ]; then
+               echo -e "o\nn\np\n1\n\n\nw\n" | fdisk /dev/$1
+               p=1
+       else
+               sgdisk --zap-all /dev/$1
+               sgdisk --new=1::+260M --typecode=1:EF00 --largest-new=2 --typecode=2:8300 /dev/$1
+               p=2
+       fi > /dev/tty6
+
+       while sleep 1; do
+               answer=`find_partition $1 $p`
+               [ -n "$answer" ] && break
+       done
+       [ -n "$efi" ] && mkdosfs -n EFI /dev/`find_partition $1 1`
+}
+
 partition_drive()
 {
        echo -n > $menufile
-       for i in /sys/block/[shv]d[a-z] /sys/block/mmcblk?; do
-               if [ ! -d $i ]; then  # pathname expansion failed
-                       continue
-               fi
+       for i in `list_disks`; do
                echo -n `basename $i` >> $menufile
                if [ -f $i/removable -a `cat $i/removable` -eq 0 ]; then
                        echo -n ' "Harddisk ' >> $menufile
@@ -43,29 +99,48 @@ partition_drive()
                        echo -n ' "Removable' >> $menufile
                fi
                if [ -f $i/size ]; then
-                       echo -n " (" `cat $i/size` "blocks)" >> $menufile
+                       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 }'`
+       count=`wc -l < $menufile`
        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`
+
+       if [ $count -eq 1 -o "$AUTO_INSTALL" = "force" ]; then
+               drive=1
+       else
+               drive=`basename $AUTO_INSTALL`
+       fi
+       choice=`awk -v n=$drive '{ if (n == NR || n == $1) print $1 }' $menufile`
+       if [ -b /dev/$choice ]; then
                retval=0
        else
-               choose "Choose Drive" "Please select a drive to edit partitions:"
+               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
+               if [ -n "$AUTO_INSTALL" ]; then
+                       auto_partition $choice
+                       return 1
+               fi
+               if fdisk -l /dev/$choice | grep -q GPT; then
+                       part_tool=cgdisk
+               elif fdisk -l /dev/$choice | grep -q doesn.t; then
+                       dialog --title " Confirm " --defaultno --yesno "\n Do you want to use GPT?" 7 29
+                       [ $? -eq 0 ] && part_tool=cgdisk || part_tool=cfdisk
                else
-                       cfdisk /dev/$choice
+                       part_tool=cfdisk
                fi
+               $part_tool /dev/$choice
                if [ $? -eq 0 ]; then
                        retval=1
                else
@@ -78,34 +153,38 @@ partition_drive()
 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
-                               break
+                               gsub(/TYPE=|"/, "", t)
+                       } else if (match($i, "^LABEL")) {
+                               l=$i
+                               gsub(/LABEL=|"/, "", l)
                        }
-               gsub(/TYPE=|"/, "", t)
-               printf("%s\t%s\n", $1, t)
+               printf("%-11s%-12s%-18s\n", $1, t, l)
        }' > $tempfile
 
-       lsblk=`ls /sys/block | grep -v -E "loop|ram|sr|boot|rpmb"`
-       for d in $lsblk; do
-               p=0
-               for i in /sys/block/$d/$d* /sys/block/$d; do
-                       [ -e $i/partition ] && p=1
-                       [ $p -eq 1 -a "$i" = "/sys/block/$d" ] && break
-                       echo $i | grep -q -E "boot|rpmb" && continue
-                       [ -d $i ] && ( grep "`basename $i:`" $tempfile || echo "`basename $i` unknown" )
+       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%-30s" `basename $i` unknown)
+                       sz=$(size_gb $i)
+                       [ "$sz" = "0.00GB" ] || printf "$f%10s\n" $sz
                done
-       done | awk '{
-               sub(/:/, "", $1)
-               printf("\"%-13s%-17s", $1, $2)
-               system("cd /sys/block; for f in "$1"/device/model "$1"/device/name */"$1"/../device/model */"$1"/../device/name; do [ -e $f ] && echo -n `cat $f` && break; done")
-               printf("\" \"\"\n")
+       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("\"Create/Modify partitions\" \"\"\n\"Detect devices\" \"\"")
+               printf("\"\" \"\"\n\"Create/Modify partitions\" \"\"\n\"Detect devices\" \"\"")
        }' > $menufile
-       choose "Choose Partition" "Please select a partition to install Android-x86:"
+       choose "Choose Partition" "Please select a partition to install $OS_TITLE:\n\nRecommended minimum free space - 4GB  |  Optimum free space >= 8GB\n\nPartition | Filesystem | Label            | Size     | Drive name/model"
        return $retval
 }
 
@@ -114,21 +193,28 @@ 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 ""\next3 ""\next2 ""\nntfs ""\nfat32 ""' > $menufile
+       echo -e '"Do not re-format" ""\next4 ""\nntfs ""\nfat32 ""' > $menufile
+       set_answer_if_auto $FORCE_FORMAT
        choose "Choose filesystem" "Please select a filesystem to format $1:"
        case "$choice" in
                ext4)
                        cmd="make_ext4fs -L"
                        ;;
-               ext3)
-                       cmd="mke2fs -jL"
-                       ;;
-               ext2)
-                       cmd="mke2fs -L"
-                       ;;
                ntfs)
                        cmd="mkntfs -fL"
                        ;;
@@ -139,14 +225,18 @@ format_fs()
                        ;;
        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 LOSE.\n\n Are you sure to format the partition $1?" 10 51
+               [ -n "$AUTO_INSTALL" ] || 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 59
                [ $? -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
 }
 
@@ -163,10 +253,10 @@ create_menulst()
        [ -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_entry "$OS_TITLE $VER" quiet $cmdline
+       create_entry "$OS_TITLE $VER (Debug mode)" $cmdline DEBUG=2
+       create_entry "$OS_TITLE $VER (Debug nomodeset)" nomodeset $cmdline DEBUG=2
+       create_entry "$OS_TITLE $VER (Debug video=LVDS-1:d)" video=LVDS-1:d $cmdline DEBUG=2
 }
 
 create_winitem()
@@ -184,6 +274,17 @@ create_winitem()
        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`
@@ -221,20 +322,34 @@ create_data_img()
 try_upgrade()
 {
        [ -d $1 ] && return
-       PREV_VERS="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
-               prev=hd/android-$v
-               if [ -d $prev ]; then
-                       dialog --title " Question " --yesno \
-                               "\nAn older Android-x86 version $v is detected.\nWould you like to upgrade it?" 8 55
-                       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
+
+       for d in hd/*; do
+               [ -e "$d"/ramdisk.img -a -n "`ls "$d"/system* 2> /dev/null`" ] && echo \"`basename $d`\" \"\"
+       done | sort -r > $menufile
+
+       count=`wc -l < $menufile`
+       if [ $count -gt 1 ]; then
+               echo -e '"" ""\n"Install to new folder '`basename $1`'" ""' >> $menufile
+               choose "Multiple older versions are found" "Please select one to upgrade:"
+       elif [ $count -eq 1 ]; then
+               eval choice=`awk '{ print $1 }' $menufile`
+               set_answer_if_auto 1
+               adialog --title " Question " --yesno \
+                       "\nAn older version $choice is detected.\nWould you like to upgrade it?" 8 61
+               [ $? -eq 0 ] || choice=
+       fi
+
+       if [ -n "$choice" ]; then
+               prev=hd/$choice
+               if [ -d "$prev" ]; then
+                       mv $prev $1
+                       for d in `find hd -type l -maxdepth 1`; do
+                               [ "`readlink $d`" = "$choice" ] && ln -sf `basename $1` $d
+                       done
+                       rm -rf $1/data/dalvik-cache/* $1/data/system/wpa_supplicant
+                       [ -s $1/data/misc/wifi/wpa_supplicant.conf ] && sed -i 's/\(ctrl_interface=\)\(.*\)/\1wlan0/' $1/data/misc/wifi/wpa_supplicant.conf
                fi
-       done
+       fi
 }
 
 get_part_info()
@@ -242,7 +357,7 @@ 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; do
+               for part in /sys/block/[shv]d$h/$1 /sys/block/xvd$h/$1 /sys/block/mmcblk$d/$1 /sys/block/nvme0n$(($d+1))/$1; do
                        [ -d $part ] && break 2
                done
                d=$(($d+1))
@@ -251,27 +366,52 @@ get_part_info()
        disk=$(basename `dirname $part`)
 }
 
+wait_for_device()
+{
+       local t=`echo /sys/block/*/$1/partition`
+       [ -f "$t" ] || return 1
+       until [ -b /dev/$1 ]; do
+               echo add > `dirname $t`/uevent
+               sleep 1
+       done
+}
+
 install_to()
 {
+       wait_for_device $1 || return 1
        cd /
        mountpoint -q /hd && umount /hd
+       [ -n "$AUTO_UPDATE" ] && FORCE_FORMAT=no || FORCE_FORMAT=ext4
        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
+               FORCE_FORMAT=ext4
        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*\)||" /proc/cmdline`
+       cmdline=`sed "s|\(initrd.*img\s*\)||; s|quiet\s*||; s|\(vga=\w\+\?\s*\)||; s|\(DPI=\w\+\?\s*\)||; s|\(AUTO_INSTALL=\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*\)||; s|[[:space:]]*$||" /proc/cmdline`
 
-       asrc=android-$VER
-       dialog --title " Confirm " --no-label Skip --defaultno --yesno \
+       [ -n "$INSTALL_PREFIX" ] && asrc=$INSTALL_PREFIX || asrc=android-$VER
+       set_answer_if_auto 1
+       [ -z "$efi" ] && adialog --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
+               if fdisk -l /dev/$disk | grep -q GPT; then
+                       umount /hd
+                       dialog --title " Warning " --defaultno --yesno \
+                               "\nFound GPT on /dev/$disk. The legacy GRUB can't be installed to GPT. Do you want to convert it to MBR?\n\nWARNING: This is a dangerous operation. You should backup your data first." 11 63
+                       [ $? -eq 1 ] && rebooting
+                       plist=$(sgdisk --print /dev/$disk | awk '/^  / { printf "%s:", $1 }' | sed 's/:$//')
+                       sgdisk --gpttombr=$plist /dev/$disk > /dev/tty6
+                       until try_mount rw /dev/$1 /hd; do
+                               sleep 1
+                       done
+               fi
+               cp -af /grub /hd
                p=$(($p-1))
                create_menulst $p
                create_winitem $1 $d
@@ -281,37 +421,81 @@ install_to()
                [ $? -ne 0 ] && return 255
        fi
 
-       dialog --title " Confirm " --no-label Skip --defaultno --yesno \
+       [ -n "$efi" ] && adialog --title " Confirm " --no-label Skip --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 ] && break
+               [ -z "$AUTO_INSTALL" -o -n "$AUTO_UPDATE" ] && for i in `list_disks`; do
+                       disk=`basename $i`
+                       esp=`sgdisk --print /dev/$disk 2> /dev/null | grep EF00 | awk '{print $1}'`
+                       [ -n "$esp" ] && boot=`find_partition $disk $esp` && break
                done
-               boot=`basename $i`
-               mountpoint -q /hd && umount /hd
-               dialog --title " Confirm " --defaultno --yesno \
-                       "\n Do you want to format the boot partition\n /dev/$boot?" 8 45
-               [ $? -eq 0 ] && mkdosfs -n EFI /dev/$boot
-               try_mount rw /dev/$boot /hd
-               cp -af /grub2/efi /hd
-               mkdir -p /hd/boot/grub
-               grubcfg=/hd/boot/grub/grub.cfg
-               echo -e "set timeout=5\n\n" > $grubcfg
-               echo -e "menuentry \"Android-x86 $VER\" {\n\tsearch --set=root --file /$asrc/kernel\n\tlinux /$asrc/kernel quiet $cmdline \n\tinitrd /$asrc/initrd.img\n}" >> $grubcfg
-               echo -e "menuentry \"Android-x86 $VER (DEBUG mode)\" {\n\tsearch --set=root --file /$asrc/kernel\n\tlinux /$asrc/kernel $cmdline DEBUG=2\n\tinitrd /$asrc/initrd.img\n}" >> $grubcfg
-               if [ -e /hd/EFI/Microsoft/Boot/bootmgfw.efi ]; then
-                       echo -e "menuentry \"Windows (UEFI)\" {\n\tsearch --set=root --file /EFI/Microsoft/Boot/bootmgfw.efi\n\tchainloader /EFI/Microsoft/Boot/bootmgfw.efi\n}" >> $grubcfg
+               if [ -z "$esp" ]; then
+                       get_part_info $1
+                       boot=$(blkid /dev/$disk* | grep -v $disk: | grep vfat | cut -d: -f1 | head -1)
+                       [ -z "$boot" ] && boot=`find_partition $disk 1` || boot=`basename $boot`
+                       esp=`cat /sys/block/$disk/$boot/partition`
+               fi
+               mkdir -p efi
+               mountpoint -q efi && umount efi
+               wait_for_device $boot
+               until try_mount rw /dev/$boot efi; do
+                       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
+               if [ "$efi" = "32" ]; then
+                       grubcfg=efi/boot/grub/i386-efi/grub.cfg
+                       bootefi=bootia32.efi
+               else
+                       grubcfg=efi/boot/grub/x86_64-efi/grub.cfg
+                       bootefi=BOOTx64.EFI
+               fi
+               if [ -d efi/efi/boot -a ! -s efi/efi/boot/android.cfg ]; then
+                       efidir=/efi/Android
+               else
+                       efidir=/efi/boot
+                       rm -rf efi/efi/Android
+               fi
+               mkdir -p `dirname $grubcfg` efi$efidir
+               cp -af grub2/efi/boot/* efi$efidir
+               sed -i "s|VER|$VER|; s|CMDLINE|$cmdline|; s|OS_TITLE|$OS_TITLE|" efi$efidir/android.cfg
+               [ -s efi/boot/grub/grubenv ] || ( printf %-1024s "# GRUB Environment Block%" | sed 's/k%/k\n/; s/   /###/g' > efi/boot/grub/grubenv )
+
+               echo -e 'set timeout=5\nset debug_mode="(DEBUG mode)"' > $grubcfg
+               # 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 efi$efidir
+                       echo -e "set kdir=$efidir\nset src=SRC=/$asrc" >> $grubcfg
+               else
+                       echo -e "set kdir=/$asrc" >> $grubcfg
+               fi
+               echo -e '\nsource $cmdpath/android.cfg' >> $grubcfg
+
+               # Checking for old EFI entries, removing them and adding new depending on bitness
+               efibootmgr | grep -Eo ".{0,6}Android-x86" | cut -c1-4 > /tmp/efientries
+               if [ -s /tmp/efientries ]; then
+                       set_answer_if_auto 1
+                       adialog --title " Question " --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
+               efibootmgr -v -c -d /dev/$disk -p $esp -L "Android-x86 $VER" -l $efidir/$bootefi > /dev/tty4 2>&1
+
+               if [ -s efi/startup.nsh ]; then
+                       sed -i "s|\\\\efi\\\\Android|$efidir|; s|/|\\\\|g" efi/startup.nsh
+               else
+                       echo $efidir/$bootefi | sed 's|/|\\|g' > efi/startup.nsh
                fi
-               mountpoint -q /hd && umount /hd
-               try_mount rw /dev/$1 /hd
        fi
 
-       dialog --title " Question " --yesno \
+       try_upgrade hd/$asrc
+
+       ! test -f hd/$asrc/system.img -o -d hd/$asrc/system
+       set_answer_if_auto $?
+       adialog --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.img"
+       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.*"
@@ -326,12 +510,12 @@ install_to()
        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..."
+               | progress_bar "Installing $OS_TITLE to $1" "Expect to write $size KB..."
        result=$((`cat /tmp/result`*255))
 
        if [ $result -eq 0 ]; then
@@ -345,7 +529,7 @@ install_to()
 
                case "$fs" in
                        vfat|fuseblk)
-                               create_data_img
+                               [ -e data.img ] && check_data_img || create_data_img
                                ;;
                        *)
                                mkdir -p data
@@ -355,19 +539,29 @@ install_to()
 
        dialog --infobox "\n Syncing to disk..." 5 27
        sync
+       cd /
 
        return $result
 }
 
 install_hd()
 {
+       case "$AUTO_INSTALL" in
+               [Uu]*)
+                       answer=${AUTO_UPDATE:-$(blkid | grep -v loop | grep -v iso9660 | sort | grep Android-x86 | cut -d: -f1 | head -1)}
+                       answer=${answer:-$(blkid | grep -v loop | sort | grep ext4 | cut -d: -f1 | head -1)}
+                       [ -b "$answer" -o -b /dev/$answer ] && answer=`basename $answer` || answer=
+                       AUTO_UPDATE=${answer:-$AUTO_UPDATE}
+                       [ -z "$AUTO_UPDATE" ] && AUTO_INSTALL=
+                       ;;
+               *)
+                       [ -z "$answer" ] && set_answer_if_auto Create
+                       ;;
+       esac
+
        select_dev || rebooting
        retval=1
        case "$choice" in
-               [shvm][dm]*)
-                       install_to $choice
-                       retval=$?
-                       ;;
                Create*)
                        partition_drive
                        retval=$?
@@ -375,23 +569,31 @@ install_hd()
                Detect*)
                        dialog --title " Detecting... " --nocancel --pause "" 8 41 1
                        ;;
+               *)
+                       install_to $choice
+                       retval=$?
+                       ;;
        esac
        return $retval
 }
 
 do_install()
 {
+       booted_from=`basename $dev`
+       efi=$(cat /sys/firmware/efi/fw_platform_size 2> /dev/null)
+       [ -n "$efi" ] && mount -t efivarfs none /sys/firmware/efi/efivars
+
        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
+                               --yesno "\nInstallation failed! Please check if you have enough free disk space to install $OS_TITLE." 8 51
                        [ $? -eq 1 ] && rebooting
                fi
        done
 
-       [ -n "$VESA" ] || runit="Run Android-x86"
+       [ -n "$VESA" ] || runit="Run $OS_TITLE"
        dialog --clear --title ' Congratulations! ' \
-               --menu "\n Android-x86 is installed successfully.\n " 11 51 13 \
+               --menu "\n $OS_TITLE is installed successfully.\n " 11 51 13 \
                "$runit" "" "Reboot" "" 2> $tempfile
        case "`cat $tempfile`" in
                Run*)