2 # By Chih-Wei Huang <cwhuang@linux.org.tw>
4 # License: GNU Public License
5 # We explicitely grant the right to use the scripts
6 # with Android-x86 project.
13 OS_TITLE=${OS_TITLE:-Android-x86}
17 dialog --title " Rebooting... " --nocancel --pause "" 8 41 1
25 echo "$answer" > $tempfile
27 test "`cat $tempfile`" != "0"
32 [ -n "$AUTO_INSTALL" ] && answer="$1"
37 if [ -n "$answer" ]; then
40 dialog "$@" 2> $tempfile
46 adialog --clear --title " $1 " --menu "$2" 21 79 13 --file $menufile
49 choice=`cat $tempfile`
54 printf %0.2fGB $(dc `cat $1/size` 2097152 / p)
59 grep -H ^$2$ /sys/block/$1/*/partition 2> /dev/null | cut -d/ -f5
64 for b in /sys/block/[shv]d[a-z] /sys/block/mmcblk? /sys/block/nvme*; do
71 [ "$AUTO_INSTALL" = "force" ] || dialog --title " Auto Installer " --defaultno --yesno \
72 "\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
73 [ $? -ne 0 ] && rebooting
75 if [ -z "$efi" ]; then
76 echo -e "o\nn\np\n1\n\n\nw\n" | fdisk /dev/$1
79 sgdisk --zap-all /dev/$1
80 sgdisk --new=1::+260M --typecode=1:EF00 --largest-new=2 --typecode=2:8300 /dev/$1
85 answer=`find_partition $1 $p`
86 [ -n "$answer" ] && break
88 [ -n "$efi" ] && mkdosfs -n EFI /dev/`find_partition $1 1`
94 for i in `list_disks`; do
95 echo -n `basename $i` >> $menufile
96 if [ -f $i/removable -a `cat $i/removable` -eq 0 ]; then
97 echo -n ' "Harddisk ' >> $menufile
99 echo -n ' "Removable' >> $menufile
101 if [ -f $i/size ]; then
103 [ "$sz" = "0.00GB" ] && sz="<0.01GB"
104 printf " %10s" $sz >> $menufile
106 for f in $i/device/model $i/*/name; do
107 [ -e $f ] && echo -n " `sed $'s/\x04//g' $f`" >> $menufile && break
109 [ "`basename $i`" = "$booted_from" -o -d $i/$booted_from ] && echo -n " *" >> $menufile
110 echo '"' >> $menufile
112 count=`wc -l < $menufile`
113 if [ $count -eq 0 ]; then
114 dialog --title " Error " --msgbox \
115 "\nOK. There is no hard drive to edit partitions." 8 49
119 if [ $count -eq 1 -o "$AUTO_INSTALL" = "force" ]; then
122 drive=`basename $AUTO_INSTALL`
124 choice=`awk -v n=$drive '{ if (n == NR || n == $1) print $1 }' $menufile`
125 if [ -b /dev/$choice ]; then
128 choose "Choose Drive" "Please select a drive to edit partitions:\n\n* - Installer source"
130 if [ $retval -eq 0 ]; then
131 if [ -n "$AUTO_INSTALL" ]; then
132 auto_partition $choice
135 if fdisk -l /dev/$choice | grep -q GPT; then
137 elif fdisk -l /dev/$choice | grep -q doesn.t; then
138 dialog --title " Confirm " --defaultno --yesno "\n Do you want to use GPT?" 7 29
139 [ $? -eq 0 ] && part_tool=cgdisk || part_tool=cfdisk
143 $part_tool /dev/$choice
144 if [ $? -eq 0 ]; then
155 blkid | grep -v -E "^/dev/block/|^/dev/loop" | cut -b6- | sort | awk '{
159 for (i = NF; i > 1; --i)
160 if (match($i, "^TYPE")) {
162 gsub(/TYPE=|"/, "", t)
163 } else if (match($i, "^LABEL")) {
165 gsub(/LABEL=|"/, "", l)
167 printf("%-11s%-12s%-18s\n", $1, t, l)
170 for d in `ls /sys/block`; do
171 for i in /sys/block/$d/$d*; do
172 [ -d $i ] || continue
173 echo $i | grep -qE "loop|ram|sr|boot|rpmb" && continue
174 f=$(grep "`basename $i`" $tempfile || printf "%-11s%-30s" `basename $i` unknown)
176 [ "$sz" = "0.00GB" ] || printf "$f%10s\n" $sz
178 done | awk -v b=$booted_from '{
180 printf("\"%s\" \"", $0)
181 system("cd /sys/block/*/"$1"; for f in ../device/model ../device/name; do [ -e $f ] && printf %-17s \"`cat $f`\" && break; done")
185 printf("\"\" \"\"\n\"Create/Modify partitions\" \"\"\n\"Detect devices\" \"\"")
187 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"
193 dialog --clear --title " $1 " --gauge "\n $2" 8 70
198 if blkid /dev/$1 | grep -q ext2; then
199 /system/bin/tune2fs -j /dev/$1
202 if blkid /dev/$1 | grep -q ext3; then
203 /system/bin/tune2fs -O extents,uninit_bg /dev/$1
211 echo -e '"Do not re-format" ""\next4 ""\nntfs ""\nfat32 ""' > $menufile
212 set_answer_if_auto $FORCE_FORMAT
213 choose "Choose filesystem" "Please select a filesystem to format $1:"
227 if [ -n "$cmd" ]; then
228 [ -n "$AUTO_INSTALL" ] || dialog --title " Confirm " --defaultno --yesno \
229 "\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
230 [ $? -ne 0 ] && return 1
231 $cmd Android-x86 /dev/$1 | awk '{
232 # FIXME: very imprecise progress
233 if (match($0, "done"))
234 printf("%d\n", i+=33)
235 }' | progress_bar "Formatting" "Formatting partition $1..."
236 elif blkid /dev/$1 | grep -q ext[23]; then
237 dialog --clear --title " Warning " --yesno \
238 "\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
239 [ $? -eq 0 ] && convert_fs $1
247 echo -e "title $title\n\tkernel /$asrc/kernel$vga $@ SRC=/$asrc\n\tinitrd /$asrc/initrd.img\n" >> $menulst
252 menulst=/hd/grub/menu.lst
253 [ -n "$VESA" ] && vga=" vga=788 modeset=0"
254 echo -e "${GRUB_OPTIONS:-default=0\ntimeout=6\nsplashimage=/grub/android-x86.xpm.gz\n}root (hd0,$1)\n" > $menulst
256 create_entry "$OS_TITLE $VER" quiet $cmdline
257 create_entry "$OS_TITLE $VER (Debug mode)" $cmdline DEBUG=2
258 create_entry "$OS_TITLE $VER (Debug nomodeset)" nomodeset $cmdline DEBUG=2
259 create_entry "$OS_TITLE $VER (Debug video=LVDS-1:d)" video=LVDS-1:d $cmdline DEBUG=2
264 win=`fdisk -l /dev/$(echo $1 | cut -b-3) | grep ^/dev | cut -b6-12,55- | awk '{
265 if (match($2, "NTFS"))
268 if [ -n "$win" ]; then
269 dialog --title " Confirm " --yesno \
270 "\nThe installer found a Windows partition in /dev/$win.\n\nDo you want to create a boot item for Windows?" 9 59
271 [ $? -ne 0 ] && return 1
272 wp=$((`echo $win | cut -b4-`-1))
273 echo -e "title Windows\n\trootnoverify (hd$d,$wp)\n\tchainloader +1\n" >> $menulst
279 losetup /dev/loop7 data.img
280 if blkid /dev/loop7 | grep -q ext[23]; then
281 dialog --clear --title " Warning " --yesno \
282 "\nYour data.img is an ext2/3 filesystem. We suggest you convert it to ext4 for better reliability." 8 58
283 [ $? -eq 0 ] && convert_fs loop7
285 losetup -d /dev/loop7
290 if [ "$fs" = "vfat" ]; then
291 ( dd bs=1M count=$1 if=/dev/zero | pv -ns $1m | dd of=$2 ) 2>&1 \
292 | progress_bar "Creating `basename $2`" "Expect to write $1 MB..."
294 dd if=/dev/zero bs=1 count=0 seek=$1M of=$2
302 dialog --title " Confirm " --defaultno --yesno \
303 "\n $bname exists. Overwrite it?" 7 38
304 [ $? -ne 0 ] && return 255
307 dialog --title " Question " --nook --nocancel --inputbox \
308 "\nPlease input the size of the $bname in MB:" 8 63 $1 2> $tempfile
310 [ 0$size -le 0 ] && size=2048
316 dialog --title " Confirm " --yesno \
317 "\nThe installer is going to create a disk image to save the user data. At least 2048MB free disk space is recommended.\n\nAre you sure to create the image?" 11 62
319 if [ $? -eq 0 ]; then
320 if create_img 2048 data.img; then
321 losetup /dev/loop6 data.img
322 make_ext4fs -L /data /dev/loop6 > /dev/tty6
324 [ $? -ne 0 ] && dialog --msgbox "\n Failed to create data.img." 7 33
326 dialog --title " Warning " --msgbox \
327 "\nOK. So data will be save to a RAMDISK(tmpfs), and lose after power off." 8 49
336 [ -e "$d"/ramdisk.img -a -n "`ls "$d"/system* 2> /dev/null`" ] && echo \"`basename $d`\" \"\"
337 done | sort -r > $menufile
339 count=`wc -l < $menufile`
340 if [ $count -gt 1 ]; then
341 echo -e '"" ""\n"Install to new folder '`basename $1`'" ""' >> $menufile
342 choose "Multiple older versions are found" "Please select one to upgrade:"
343 elif [ $count -eq 1 ]; then
344 eval choice=`awk '{ print $1 }' $menufile`
346 adialog --title " Question " --yesno \
347 "\nAn older version $choice is detected.\nWould you like to upgrade it?" 8 61
348 [ $? -eq 0 ] || choice=
351 if [ -n "$choice" ]; then
353 if [ -d "$prev" ]; then
355 rm -rf $1/data/dalvik-cache/* $1/data/system/wpa_supplicant
356 [ -s $1/data/misc/wifi/wpa_supplicant.conf ] && sed -i 's/\(ctrl_interface=\)\(.*\)/\1wlan0/' $1/data/misc/wifi/wpa_supplicant.conf
365 h=`echo $d | awk '{ printf("%c", $1+97) }'`
366 for part in /sys/block/[shv]d$h/$1 /sys/block/mmcblk$d/$1 /sys/block/nvme0n$(($d+1))/$1; do
367 [ -d $part ] && break 2
371 p=`cat $part/partition`
372 disk=$(basename `dirname $part`)
377 local t=`echo /sys/block/*/$1/partition`
378 [ -f "$t" ] || return 1
379 until [ -b /dev/$1 ]; do
380 echo add > `dirname $t`/uevent
387 wait_for_device $1 || return 1
389 mountpoint -q /hd && umount /hd
390 [ -n "$AUTO_UPDATE" ] && FORCE_FORMAT=no || FORCE_FORMAT=ext4
393 try_mount rw /dev/$1 /hd && break
394 dialog --clear --title " Error " --defaultno --yesno \
395 "\n Cannot mount /dev/$1\n Do you want to format it?" 8 37
396 [ $? -ne 0 ] && return 255
400 fs=`cat /proc/mounts | grep /dev/$1 | awk '{ print $3 }'`
401 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`
403 [ -n "$INSTALL_PREFIX" ] && asrc=$INSTALL_PREFIX || asrc=android-$VER
405 [ -z "$efi" ] && adialog --title " Confirm " --no-label Skip --defaultno --yesno \
406 "\n Do you want to install boot loader GRUB?" 7 47
407 if [ $? -eq 0 ]; then
409 if fdisk -l /dev/$disk | grep -q GPT; then
411 dialog --title " Warning " --defaultno --yesno \
412 "\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
413 [ $? -eq 1 ] && rebooting
414 plist=$(sgdisk --print /dev/$disk | awk '/^ / { printf "%s:", $1 }' | sed 's/:$//')
415 sgdisk --gpttombr=$plist /dev/$disk > /dev/tty6
416 until try_mount rw /dev/$1 /hd; do
424 rm -f /hd/boot/grub/stage1
425 echo "(hd$d) /dev/$disk" > /hd/grub/device.map
426 echo "setup (hd$d) (hd$d,$p)" | grub --device-map /hd/grub/device.map > /dev/tty5
427 [ $? -ne 0 ] && return 255
430 [ -n "$efi" ] && adialog --title " Confirm " --no-label Skip --yesno \
431 "\n Do you want to install EFI GRUB2?" 7 39
432 if [ $? -eq 0 ]; then
433 [ -z "$AUTO_INSTALL" -o -n "$AUTO_UPDATE" ] && for i in `list_disks`; do
435 esp=`sgdisk --print /dev/$disk 2> /dev/null | grep EF00 | awk '{print $1}'`
436 [ -n "$esp" ] && boot=`find_partition $disk $esp` && break
438 if [ -z "$esp" ]; then
440 boot=$(blkid /dev/$disk* | grep -v $disk: | grep vfat | cut -d: -f1 | head -1)
441 [ -z "$boot" ] && boot=`find_partition $disk 1` || boot=`basename $boot`
442 esp=`cat /sys/block/$disk/$boot/partition`
445 mountpoint -q efi && umount efi
446 wait_for_device $boot
447 until try_mount rw /dev/$boot efi; do
448 dialog --title " Confirm " --defaultno --yesno "\n Cannot mount /dev/$boot.\n Do you want to format it?" 8 37
449 [ $? -eq 0 ] && mkdosfs -n EFI /dev/$boot
451 if [ "$efi" = "32" ]; then
452 grubcfg=efi/boot/grub/i386-efi/grub.cfg
455 grubcfg=efi/boot/grub/x86_64-efi/grub.cfg
458 if [ -d efi/efi/boot -a ! -s efi/efi/boot/android.cfg ]; then
462 rm -rf efi/efi/Android
464 mkdir -p `dirname $grubcfg` efi$efidir
465 cp -af grub2/efi/boot/* efi$efidir
466 sed -i "s|VER|$VER|; s|CMDLINE|$cmdline|; s|OS_TITLE|$OS_TITLE|" efi$efidir/android.cfg
467 [ -s efi/boot/grub/grubenv ] || ( printf %-1024s "# GRUB Environment Block%" | sed 's/k%/k\n/; s/ /###/g' > efi/boot/grub/grubenv )
469 echo -e 'set timeout=5\nset debug_mode="(DEBUG mode)"' > $grubcfg
470 # Our grub-efi doesn't support ntfs directly.
471 # Copy boot files to ESP so grub-efi could read them
472 if [ "$fs" = "fuseblk" ]; then
473 cp -f src/kernel src/initrd.img efi$efidir
474 echo -e "set kdir=$efidir\nset src=SRC=/$asrc" >> $grubcfg
476 echo -e "set kdir=/$asrc" >> $grubcfg
478 echo -e '\nsource $cmdpath/android.cfg' >> $grubcfg
479 if [ -d src/boot/grub/theme ]; then
480 cp -R src/boot/grub/[ft]* efi/boot/grub
481 find efi/boot/grub -name TRANS.TBL -delete
484 # Checking for old EFI entries, removing them and adding new depending on bitness
485 efibootmgr | grep -Eo ".{0,6}Android-x86" | cut -c1-4 > /tmp/efientries
486 if [ -s /tmp/efientries ]; then
488 adialog --title " Question " --yesno "\nEFI boot entries for previous Android-x86 installations were found.\n\nDo you wish to delete them?" 10 61
489 [ $? -eq 0 ] && while read entry; do efibootmgr -Bb "$entry" > /dev/tty4 2>&1; done < /tmp/efientries
491 efibootmgr -v -c -d /dev/$disk -p $esp -L "Android-x86 $VER" -l $efidir/$bootefi > /dev/tty4 2>&1
493 if [ -s efi/startup.nsh ]; then
494 sed -i "s|\\\\efi\\\\Android|$efidir|; s|/|\\\\|g" efi/startup.nsh
496 echo $efidir/$bootefi | sed 's|/|\\|g' > efi/startup.nsh
500 ! test -f hd/$asrc/system.img -o -d hd/$asrc/system
501 set_answer_if_auto $?
502 adialog --title " Question " --defaultno --yesno \
503 "\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
504 if [ $? -eq 0 -a -e /sfs/system.img ]; then
505 sysimg="/sfs/system.img"
507 sysimg="mnt/$SRC/system.*"
509 files="mnt/$SRC/kernel mnt/$SRC/initrd.img mnt/$SRC/$RAMDISK $sysimg"
511 for s in `du -sk $files | awk '{print $1}'`; do
518 ( ( cd /; find $files | $CPIO -H newc -o ) | pv -ns ${size}k | ( $CPIO -iud > /dev/null; echo $? > /tmp/result )) 2>&1 \
519 | progress_bar "Installing $OS_TITLE to $1" "Expect to write $size KB..."
520 result=$((`cat /tmp/result`*255))
522 if [ $result -eq 0 ]; then
523 for d in android mnt sfs ./$SRC; do
524 [ -d $d ] && mv $d/* . && rmdir $d
528 [ -d $f ] || chmod 644 $f
533 [ -e data.img ] && check_data_img || create_data_img
541 dialog --infobox "\n Syncing to disk..." 5 27
550 case "$AUTO_INSTALL" in
552 answer=${AUTO_UPDATE:-$(blkid | grep -v loop | grep -v iso9660 | sort | grep Android-x86 | cut -d: -f1 | head -1)}
553 answer=${answer:-$(blkid | grep -v loop | sort | grep ext4 | cut -d: -f1 | head -1)}
554 [ -b "$answer" -o -b /dev/$answer ] && answer=`basename $answer` || answer=
555 AUTO_UPDATE=${answer:-$AUTO_UPDATE}
558 [ -z "$answer" ] && set_answer_if_auto Create
562 select_dev || rebooting
570 dialog --title " Detecting... " --nocancel --pause "" 8 41 1
582 booted_from=`basename $dev`
583 efi=$(cat /sys/firmware/efi/fw_platform_size 2> /dev/null)
584 [ -n "$efi" ] && mount -t efivarfs none /sys/firmware/efi/efivars
587 if [ $retval -eq 255 ]; then
588 dialog --title ' Error! ' --yes-label Retry --no-label Reboot \
589 --yesno "\nInstallation failed! Please check if you have enough free disk space to install $OS_TITLE." 8 51
590 [ $? -eq 1 ] && rebooting
594 [ -n "$VESA" ] || runit="Run $OS_TITLE"
595 dialog --clear --title ' Congratulations! ' \
596 --menu "\n $OS_TITLE is installed successfully.\n " 11 51 13 \
597 "$runit" "" "Reboot" "" 2> $tempfile
598 case "`cat $tempfile`" in
602 mountpoint -q /sfs && umount /sfs
603 if [ -e /hd/$asrc/system.sfs ]; then
604 mount -o loop /hd/$asrc/system.sfs /sfs
605 mount -o loop /sfs/system.img system
606 elif [ -e /hd/$asrc/system.img ]; then
607 mount -o loop /hd/$asrc/system.img system
609 mount --bind /hd/$asrc/system system
611 if [ -d /hd/$asrc/data ]; then
612 mount --bind /hd/$asrc/data data
613 elif [ -e /hd/$asrc/data.img ]; then
614 mount -o loop /hd/$asrc/data.img data