#
# By Chih-Wei Huang <cwhuang@>
#
# 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
OS_TITLE=${OS_TITLE:-Android-x86}
rebooting()
{
dialog --title " Rebooting... " --nocancel --pause "" 8 41 1
sync
umount -a
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
#将用户的选择结果保存在tempfile中
dialog "$@" 2> $tempfile
fi
}
choose()
{
#第1条语句是title,第2条语句是显示内容,menufile中的文本内容为选项
adialog --clear --title " $1 " --menu "$2" 21 79 13 --file $menufile
#上一命令正常执行,所以retval=0
retval=$?
#choice即为用户的选择结果
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/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`
}
#使用cfdisk工具进行分区
partition_drive()
{
#清空menufile文件
echo -n > $menufile
#list_disks函数用来列举出符合条件的块设备
for i in `list_disks`; do
#将basename添加到menufile中,比如sda
echo -n `basename $i` >> $menufile
#-f表示是文件,判断块设备sda(硬盘)是否可以被删除,如果$i/removable中的值为0,表示不可被删除
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"
#将硬盘大小追加到menufil
printf " %10s" $sz >> $menufile
fi
#将硬盘品牌追加到menufile,比如:KXG6AZNV512G TOSHIBA
for f in $i/device/model $i/*/name; do
[ -e $f ] && echo -n " `sed $'s/\x04//g' $f`" >> $menufile && break
done
#将" *"追加到menufile
[ "`basename $i`" = "$booted_from" -o -d $i/$booted_from ] && echo -n " *" >> $menufile
#将'"'追加到menufile,经过测试,最终menufile的内容为:sda "Harddisk 10.00GB QEMU HARDDISK "
echo '"' >> $menufile
done
#wc -l用来显示文本行数,menufile只有一行,所以count=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
#count=1,所以drive=1
if [ $count -eq 1 -o "$AUTO_INSTALL" = "force" ]; then
drive=1
else
drive=`basename $AUTO_INSTALL`
fi
#读出menufile中的第1个元素,也就是硬盘名,下面命令处理后的choice=sda
choice=`awk -v n=$drive '{ if (n == NR || n == $1) print $1 }' $menufile`
#/dev/sda是否块设备,结果:是
if [ -b /dev/$choice ]; then
retval=0
else
choose "Choose Drive" "Please select a drive to edit partitions:\n\n* - Installer source"
fi
#retval=0,下面判断成立
if [ $retval -eq 0 ]; then
#-n表示非空串,不成立
if [ -n "$AUTO_INSTALL" ]; then
auto_partition $choice
return 1
fi
#fdisk -l列出所有分区表,最终结果是:part_tool=cfdisk
if fdisk -l /dev/$choice | grep -q GPT; then
part_tool=cgdisk
elif fdisk -l /dev/$choice | grep -q ; then
dialog --title " Confirm " --defaultno --yesno "\n Do you want to use GPT?" 7 29
#选择了否,part_tool=cfdisk
[ $? -eq 0 ] && part_tool=cgdisk || part_tool=cfdisk
else
part_tool=cfdisk
fi
#下面命令的完整形式是:cfdisk /dev/sda,使用cfdisk命令进行分区,进入创建分区界面
$part_tool /dev/$choice
#经过一系列的分区操作后,分区完成,retval=1
if [ $? -eq 0 ]; then
retval=1
else
retval=255
fi
fi
return $retval
}
#选择分区
select_dev()
{
#用blkid可列出当前系统中所有已挂载文件系统的类型,blkid主要用来对系统的块设备(包括交换分区)所使用的文件系统类型、LABEL、UUID等信息进行查询
#举个例子,比如:/dev/sr0: LABEL="Android-x86 2021-11-12(x86-64)" TYPE="iso9660"
#grep -v 去除掉/dev/block/ 和 /dev/loop
#cut -b6- 每行从第6位开始,其实也就是将/dev/裁减掉
#sort 以默认的方式将文本文件的第一列以 ASCII 码的次序排列
#awk 处理文本,每行只保留TYPE和LABEL
#将处理后的文本保存至/tmp/temp-当前进程的进程号
#文本内容为:sr0 iso9660 Android-x86
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
#当分区已经存在,下面命令会将分区信息打印显示出来
#遍历/sys/block目录,查找出符合/sys/block/$d/$d*格式的文件夹,比如/sys/block/sda/sda1/:
#再过滤掉文件名包含字段"loop|ram|sr|boot|rpmb"的文件:
#读取/sys/block/$d/$d*/size大小,并打印
for d in `ls /sys/block`; do
for i in /sys/block/$d/$d*; do
#-d表示是目录也就是文件夹,下面语句的意思是:如果是文件夹就继续向下执行
[ -d $i ] || continue
#如果文件名包含字段"loop|ram|sr|boot|rpmb"则将该文件过滤掉,也就是只保留文件名中不包含"loop|ram|sr|boot|rpmb"的文件
echo $i | grep -qE "loop|ram|sr|boot|rpmb" && continue
#如果tempfile不包含basename,则f为unknown
f=$(grep "`basename $i`" $tempfile || printf "%-11s%-30s" `basename $i` unknown)
#通过size_gb函数将/sys/block/$d/$d*/size中的值进行转化
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 {
#将下面文字保存到menufile文件中,下面选项就是选择分区界面的item
printf("\"\" \"\"\n\"Create/Modify partitions\" \"\"\n\"Detect devices\" \"\"")
}' > $menufile
#select_dev弹出框
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
}
progress_bar()
{
dialog --clear --title " $1 " --gauge "\n $2" 8 70
}
convert_fs()
{
if blkid /dev/$1 | grep -q ext2; then
#tune2fs是调整和查看ext2/ext3文件系统的文件系统参数,Linux系统下面也有文件系统自检,而且是可以通过tune2fs命令,自行定义自检周期及方式。
#-j:将ext2文件系统转换为ext3类型的文件系统,ext2可以转ext3 但不可以转回,转回数据丢失
#添加日志功能,将ext2转换成ext3文件系统
/system/bin/tune2fs -j /dev/$1
#e2fsck命令用于检查使用 Linux ext2 档案系统的 partition 是否正常工作。
e2fsck -fy /dev/$1
fi
#下面的判断成立
if blkid /dev/$1 | grep -q ext3; then
#转换成ext4
/system/bin/tune2fs -O extents,uninit_bg /dev/$1
e2fsck -fy /dev/$1
fi
}
format_fs()
{
local cmd
#将要在"Choose filesystem 弹出框"中出现的4行item,将这4行item保存至menufile文件中
echo -e '"Do not re-format" ""\next4 ""\nntfs ""\nfat32 ""' > $menufile
set_answer_if_auto $FORCE_FORMAT
#Choose filesystem 弹出框
choose "Choose filesystem" "Please select a filesystem to format $1:"
#选择了ext4
case "$choice" in
ext4)
#走这一步
cmd="mkfs.ext3 -L"
;;
ntfs)
cmd="mkntfs -fL"
;;
fat32)
cmd="mkdosfs -n"
;;
*)
;;
esac
if [ -n "$cmd" ]; then
#Confirm界面
[ -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
#选择yes,继续向下执行,将/dev/sda1格式化成ext3文件系统,如果选择no直接返回
[ $? -ne 0 ] && return 1
#将硬盘/dev/sda1格式化成ext3文件系统
$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..."
#转换分区文件类型,将ext3转化成ext4
convert_fs $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/\n" >> $menulst
}
create_menulst()
{
menulst=/hd/grub/
#经过测试,VESA为空串,此处vga未被赋值
[ -n "$VESA" ] && vga=" vga=788 modeset=0"
echo -e "${GRUB_OPTIONS:-default=0\ntimeout=6\nsplashimage=/grub/\n}root (hd0,$1)\n" > $menulst
create_entry "$OS_TITLE $VER" quiet $cmdline
create_entry "$OS_TITLE $VER (Debug mode)" $cmdline DEBUG=2
create_entry "$OS_TITLE $VER (Debug mode) " $cmdline DEBUG=2 GRALLOC=gbm
create_entry "$OS_TITLE $VER (Debug mode) drmfb-composer " $cmdline DEBUG=2 HWC=drmfb GRALLOC=gbm
create_entry "$OS_TITLE $VER (Debug mode) " $cmdline DEBUG=2 HWC=drm GRALLOC=gbm
create_entry "$OS_TITLE $VER (Debug mode) " DEBUG=2 GRALLOC=minigbm
create_entry "$OS_TITLE $VER (Debug mode) hwcomposer.drm_minigbm " DEBUG=2 HWC=drm_minigbm GRALLOC=minigbm
create_entry "$OS_TITLE $VER (Debug mode) " DEBUG=2 HWC=intel GRALLOC=intel
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()
{
win=`fdisk -l /dev/$(echo $1 | cut -b-3) | grep ^/dev | cut -b6-12,55- | awk '{
if (match($2, "NTFS"))
print $1
}' | head -1`
#经过测试,win为空串
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
if blkid /dev/loop7 | grep -q ext[23]; then
dialog --clear --title " Warning " --yesno \
"\nYour 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
}
gen_img()
{
if [ "$fs" = "vfat" ]; then
( dd bs=1M count=$1 if=/dev/zero | pv -ns $1m | dd of=$2 ) 2>&1 \
| progress_bar "Creating `basename $2`" "Expect to write $1 MB..."
else
dd if=/dev/zero bs=1 count=0 seek=$1M of=$2
fi
}
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=2048
gen_img $size $2
}
create_data_img()
{
dialog --title " Confirm " --yesno \
"\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
if [ $? -eq 0 ]; then
if create_img 2048 ; then
losetup /dev/loop6
mkfs.ext4 -L /data /dev/loop6 > /dev/tty6
fi
[ $? -ne 0 ] && dialog --msgbox "\n Failed to create ." 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
for d in hd/*; do
[ -e "$d"/ -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
fi
}
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
#part/partition文件中保存的值表示第几个分区,sda1是硬盘sda的第1个分区,所以p=1
p=`cat $part/partition`
#disk为sda
disk=$(basename `dirname $part`)
}
wait_for_device()
{
local t=`echo /sys/block/*/$1/partition`
[ -f "$t" ] || return 1
#由于/dev/sda1是块设备,不进入until循环体
until [ -b /dev/$1 ]; do
echo add > `dirname $t`/uevent
sleep 1
done
}
install_to()
{
#$1=sda1
#等待块设备,当/dev/$1是块设备时才继续往下执行
wait_for_device $1 || return 1
cd /
#这一段代码的主要功能是完成对磁盘的格式化以及挂载,格式化是在format_fs中实现
#注意:要使用一块新的硬盘,必须将它格式化建立合适的文件系统(linux:ext2,ext3等,windows:ntsf,fat32),并挂载到相应的目录下我们才可以使用。
#使用mountpoint这条命令来确认某个目录是否”临时性“的被文件系统占用,如果被占用了,使用umount卸除目前挂在Linux目录中的文件系统
mountpoint -q /hd && umount /hd
#经过测试,$AUTO_UPDATE为空串,所以FORCE_FORMAT=ext4
[ -n "$AUTO_UPDATE" ] && FORCE_FORMAT=no || FORCE_FORMAT=ext4
while [ 1 ]; do
#硬盘/dev/sda1的格式化
format_fs $1
#将dev/sda1挂载到/hd目录下,向/hd这个目录里写数据将会保存到硬盘dev/sda1里,rw表示可读可写
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
#安装grub启动引导选项
#经过测试,fs=ext4
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|\(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`
#由于INSTALL_PREFIX为空,所以asrc=android-$VER,经过测试,asrc=android-2021-11-12
[ -n "$INSTALL_PREFIX" ] && asrc=$INSTALL_PREFIX || asrc=android-$VER
set_answer_if_auto 1
#efi为空串,弹出Confirm对话框
[ -z "$efi" ] && adialog --title " Confirm " --no-label Skip --defaultno --yesno \
"\n Do you want to install boot loader GRUB?" 7 47
#选择yes
if [ $? -eq 0 ]; then
#获得分区编号,$p=1
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
#将/grub文件夹复制到/hd目录下
cp -af /grub /hd
p=$(($p-1))
#create_menulst 0;向menulst文件中填充属性值,grub目录下定义启动选项
create_menulst $p
#create_winitem sda1 sda,向menulst文件中填充值
create_winitem $1 $d
rm -f /hd/boot/grub/stage1
echo "(hd$d) /dev/$disk" > /hd/grub/
echo "setup (hd$d) (hd$d,$p)" | grub --device-map /hd/grub/ > /dev/tty5
[ $? -ne 0 ] && return 255
fi
#由于efi为空串,所以下面的代码暂时可忽略
[ -n "$efi" ] && adialog --title " Confirm " --no-label Skip --yesno \
"\n Do you want to install EFI GRUB2?" 7 39
if [ $? -eq 0 ]; then
[ -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
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/
bootefi=
else
grubcfg=efi/boot/grub/x86_64-efi/
bootefi=
fi
if [ -d efi/efi/boot -a ! -s efi/efi/boot/ ]; 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/
[ -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/ efi$efidir
echo -e "set kdir=$efidir\nset src=SRC=/$asrc" >> $grubcfg
else
echo -e "set kdir=/$asrc" >> $grubcfg
fi
echo -e '\nsource $cmdpath/' >> $grubcfg
if [ -d src/boot/grub/theme ]; then
cp -R src/boot/grub/[ft]* efi/boot/grub
find efi/boot/grub -name -delete
fi
# 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/ ]; then
sed -i "s|\\\\efi\\\\Android|$efidir|; s|/|\\\\|g" efi/
else
echo $efidir/$bootefi | sed 's|/|\\|g' > efi/
fi
fi
#升级更新,暂时 忽略
try_upgrade hd/$asrc
! test -f hd/$asrc/ -o -d hd/$asrc/system
set_answer_if_auto $?
#弹出Question对话框
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
#下面的判断成立
if [ $? -eq 0 -a -e /sfs/ ]; then
sysimg="/sfs/"
else
sysimg="mnt/$SRC/system.*"
fi
#把镜像文件写入到磁盘,将files中的所有文件写到android9.qcow2
#经过测试,files="mnt///kernel mnt/// mnt/// /sfs/"
files="mnt/$SRC/kernel mnt/$SRC/ mnt/$SRC/$RAMDISK $sysimg"
size=0
for s in `du -sk $files | awk '{print $1}'`; do
size=$(($size+$s))
done
#创建文件hd/android-2021-11-12并进入
mkdir -p hd/$asrc
cd hd/$asrc
#删除system*文件
rm -rf system*
#开始写入,使用cpio解压img文件
( ( cd /; find $files | $CPIO -H newc -o ) | pv -ns ${size}k | ( $CPIO -iud > /dev/null; echo $? > /tmp/result )) 2>&1 \
| progress_bar "Installing $OS_TITLE to $1" "Expect to write $size KB..."
#经过测试result=0
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
#fs=ext4
case "$fs" in
vfat|fuseblk)
[ -e ] && check_data_img || create_data_img
;;
*)
mkdir -p data
;;
esac
fi
dialog --infobox "\n Syncing to disk..." 5 27
sync
cd /
return $result
}
#选择分区,创建分区,安装
install_hd()
{
#经过测试,此处AUTO_INSTALL为空
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表示空串,[ -z "$answer" ]成立,执行函数set_answer_if_auto时由于AUTO_INSTALL为空,所以answer在这里没有被赋值
[ -z "$answer" ] && set_answer_if_auto Create
;;
esac
#select_dev选择分区,选择分区失败会执行rebooting重新启动
select_dev || rebooting
retval=1
#根据select_dev界面不同的选择,执行相应的代码
case "$choice" in
#创建分区
Create*)
#partition_drive创建分区
partition_drive
retval=$?
;;
Detect*)
dialog --title " Detecting... " --nocancel --pause "" 8 41 1
;;
*)
#安装到分区,假设$choice=sda1
install_to $choice
retval=$?
;;
esac
#如果$choice=Create*,也就是创建分区,那retval=1,这个值传递给do_install()函数中的until语句,表示继续循环,直到choice=0循环才终止,也就是要调用install_to()函数进行安装;
#当partition_drive完成创建分区之后,此时在select_dev界面会出现3个选项,第1个选项就是分区项,当点击该项时,会执行install_to()函数进行安装
return $retval
}
#安装流程的入口函数
do_install()
{
#‘basename $dev’为系统命令,意思是:取文件路径的最后/后面的内容,比如:这里dev=/dev/block/sr0,booted_from=sr0 ,booted_from变量被引用时才执行该命令
booted_from=`basename $dev`
#2> /dev/null的意思是将标准错误stderr删掉,经过测试,此处efi为空
efi=$(cat /sys/firmware/efi/fw_platform_size 2> /dev/null)
#-n 表示非空串,此处efi为空,不满足
[ -n "$efi" ] && mount -t efivarfs none /sys/firmware/efi/efivars
#调用install_hd,主要是选择分区,创建分区,以及执行安装
#当install_hd成立时,也就是install_hd返回0时,才退出循环
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 $OS_TITLE." 8 51
[ $? -eq 1 ] && rebooting
fi
done
#如果上面的东西全部安装完成,那最后则会调用以下代码,提示用户以安装完成
[ -n "$VESA" ] || runit="Run $OS_TITLE"
dialog --clear --title ' Congratulations! ' \
--menu "\n $OS_TITLE is installed successfully.\n " 11 51 13 \
"$runit" "" "Reboot" "" 2> $tempfile
case "`cat $tempfile`" in
Run*)
#Run Android-x86
cd /android
umount system
mountpoint -q /sfs && umount /sfs
if [ -e /hd/$asrc/ ]; then
mount -o loop /hd/$asrc/ /sfs
mount -o loop /sfs/ system
elif [ -e /hd/$asrc/ ]; then
mount -o loop /hd/$asrc/ 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/ ]; then
mount -o loop /hd/$asrc/ data
fi
;;
*)
rebooting
;;
esac
}