#!/bin/bash
#
# 工具

CONFIGFILE=/tmp/ky-installer.cfg
LOG_FILE=/var/log/installer/kylin-os-installer-detail.log
LOG_PROGRESS_FILE=/var/log/installer/kylin-os-installer.log
INSTALLER_PROGRESS=/var/log/installer/installer-progress.log
IN_TARGET=/tmp/k-i/

msg() {
    echo "$(date +'%Y-%m-%d %H:%M:%S') $*" >&2
}

get_value() {
    local key=$1
    local value
    value=$(grep "^$key=" "$CONFIGFILE")
    echo "${value#*=}"
}

get_value_bytearray() {
    local key=$1
    local value
    value=$(grep "^$key=" "$CONFIGFILE")

    if ! egrep -q "^$key=.?@ByteArray" $CONFIGFILE; then
        echo "${value#*=}"
        return 0
    fi

    value1=$(echo "${value#*\(}")
    echo "${value1%)*}"
}

readonly LANGUAGES=$(get_value language)

set_backup() {
    backup_dev=$1
    backup_root=$2
    backup_uuid=$(blkid -o value -s UUID $backup_dev)
    mkdir -p $backup_root/etc
    echo "RECOVERY_DEV_UUID=$backup_uuid
SNAPSHOT_ENABLED=1" >$backup_root/etc/.bootinfo
    mkdir -p $backup_root/backup/current
    mkdir -p $backup_root/backup/snapshots
    msg "设置备份还原分区完成"
}

# 卸载文件系统
umount_all() {
    msg "正在卸载文件系统..."
    sync
    umount -l /target || true
    if mount | grep -q /backup; then
        umount -l /backup || true
    fi
    umount -l /kylin-sysroot || true
    msg "卸载完成"
}

do_mipsel_loongson_2f() {
    ROOT=
    ARCH=$(archdetect)
    case $ARCH in
        mipsel/loongson-2f)
        # Configure PMON to load GRUB by default.
        if [ ! -e $ROOT/boot.cfg ] && [ ! -e $ROOT/boot/boot.cfg ]; then
            pmon_partition="$(grub_probe -d -t drive "$bootfs" | \
                      sed 's/.*,//; s/[^0-9]//g')"
            if [ "$pmon_partition" ]; then
                pmon_partition=$(($pmon_partition - 1))
            else
                pmon_partition=0 # fallback guess
            fi
            if [[ "$rootfs" == "$bootfs" ]]; then
                pmon_grub_path=/boot/grub.elf
                pmon_boot_cfg_path=$ROOT/boot.cfg
            else
                pmon_grub_path=/grub.elf
                pmon_boot_cfg_path=$ROOT/boot/boot.cfg
            fi
            cat > $pmon_boot_cfg_path <<EOF
default 0
timeout 0
showmenu 0

title Boot with GRUB
    kernel (wd0,$pmon_partition)$pmon_grub_path
    args nil
EOF
        fi
        ;;
    esac
}

do_mips_bootloader() {
   # if [[ -f /usr/sbin/update-grub ]]; then
   #    update-grub
   # fi

    rsync -r /cdrom/boot/ /boot

    pushd /boot
    ln -fs . boot || true
    popd

    # 其他设置
    do_mipsel_loongson_2f

    # bug 32789, 创建 esp 时无法引导
    if [[ -d /boot/efi ]] && [[ -d /boot/EFI ]]; then
        rsync -a /boot/{EFI/,efi}
    fi
}

do_sw64_bootloader() {
    grub_opts="root=UUID=$(blkid -o value -s UUID $(findmnt / -no source))"
    swap_dev=$(grep /dev /proc/swaps | head -1 | awk '{print $1}')
    swap_opts=""
    if [[ ! -z "${swap_dev}" ]]; then
        swap_opts="resume=UUID=$(blkid -o value -s UUID ${swap_dev})"
    fi
    rsync -r /cdrom/boot/ /boot
    sed -i "s|boot=casper|${grub_opts} ${swap_opts}|g" /boot/grub/grub.cfg

    pushd /boot
    ln -fs . boot || true
    popd
}

# 根据磁盘大小自动选择磁盘(未调用)
auto_select_disk() {
    DiskList=`LANG=en_US; sudo fdisk -l | awk '{if($1=="Disk" && $2!="identifier:" && $2!="model:") print $2$5}'`
    MAX=$[$(get_value max)]
    MIN=$[$(get_value min)]
    Size_0=1024
    Size_1=1024
    Ret=

    for i in $DiskList
    do
        Val=`LANG=en_US; echo $i | cut -d ':' -f 2`
        Val=$[$Val / 1024 / 1024 / 1024]
        Disk=`LANG=en_US; echo $i | cut -d ':' -f 1`
            if [[ `echo "$Val < 35" | bc` -eq 1 ]]; then
            continue
        fi
        if [[ `echo "$Val < $Size_0" | bc` -eq 1 ]]; then
            Size_0=$Val
            Ret_back=$Disk
        fi
            if [[ `echo "$Val >= $MIN" | bc` -eq 1 ]] &&
               [[ `echo "$Val <= $MAX" | bc` -eq 1 ]] &&
               [[ `echo "$Val < $Size_1" | bc` -eq 1 ]]; then
        Size_1=$Val
        Ret=$Disk
        fi
    done

    if [ -z $Ret ]; then
        Ret=$Ret_back
    fi

    echo $Ret
}

#自动选择系统盘,先最大nvme盘,再最大固态ssd,最后最大机械hdd
auto_disk() {
    DiskList=`LANG=en_US; sudo fdisk -l | awk '{if($1=="Disk" && $2!="identifier:" && $2!="model:") print $2$5}'`
    MAX=2050
    MIN=50
    Size_0=50
    Size_1=50
    Size_2=50

    Ret=

    if echo $DiskList | grep -q 'nvme'; then
        DiskList=`LANG=en_US; sudo fdisk -l | awk '{if($1=="Disk" && $2!="identifier:" && $2!="model:") print $2$5}' | grep "nvme"`
    elif echo $DiskList | grep -q 'sd'; then
        DiskList=`LANG=en_US; sudo fdisk -l | awk '{if($1=="Disk" && $2!="identifier:" && $2!="model:") print $2$5}' | grep "sd"`
    else
            DiskList=$DiskList
    fi

    num=1
    fdd=1
    for i in $DiskList
    do
        Val=`LANG=en_US; echo $i | cut -d ':' -f 2`
        Val=$[$Val / 1024 / 1024 / 1024]

        Disk=`LANG=en_US; echo $i | cut -d ':' -f 1`

        if [[ `echo "$Val < 35" | bc` -eq 1 ]]; then
            continue
        fi

        Sd=${Disk##*/}
        fdd=$( cat /sys/block/${Sd}/queue/rotational )
        if [[ $fdd == 0 || $num == 0 ]];then
                num=0
                if [[ $fdd == 1 ]];then
                        continue
                fi
                if [[ `echo "$Val > $Size_0" | bc` -eq 1 ]]; then
                         Size_0=$Val
                        Ret_back=$Disk
                fi

                 if [[ `echo "$Val >= $MIN" | bc` -eq 1 ]] &&
                   [[ `echo "$Val <= $MAX" | bc` -eq 1 ]] &&
                   [[ `echo "$Val > $Size_2" | bc` -eq 1 ]]; then
                        Size_2=$Val
                        Ret=$Disk
                 fi
                 continue
        fi

        if [[ `echo "$Val > $Size_0" | bc` -eq 1 ]]; then
                 Size_0=$Val
                 Ret_back=$Disk
        fi

        if [[ `echo "$Val >= $MIN" | bc` -eq 1 ]] &&
           [[ `echo "$Val <= $MAX" | bc` -eq 1 ]] &&
           [[ `echo "$Val > $Size_1" | bc` -eq 1 ]]; then
                Size_1=$Val
                Ret=$Disk
        fi
    done



    if [ -z $Ret ]; then
        Ret=$Ret_back
    fi

    echo $Ret
    sed -i "/devpath/d" /tmp/ky-installer.cfg
    echo "devpath=$Ret" >>/tmp/ky-installer.cfg
}

#自动选择系统盘,先最小nvme盘,再最小固态ssd,最后最小机械hdd
auto_disk_min() {
    DiskList=`LANG=en_US; sudo fdisk -l | awk '{if($1=="Disk" && $2!="identifier:" && $2!="model:") print $2$5}'`
    MAX=2050
    MIN=50
    Size_0=2050
    Size_1=2050
    Size_2=2050

    Ret=

    if echo $DiskList | grep -q 'nvme'; then
        DiskList=`LANG=en_US; sudo fdisk -l | awk '{if($1=="Disk" && $2!="identifier:" && $2!="model:") print $2$5}' | grep "nvme"`
    elif echo $DiskList | grep -q 'sd'; then
        DiskList=`LANG=en_US; sudo fdisk -l | awk '{if($1=="Disk" && $2!="identifier:" && $2!="model:") print $2$5}' | grep "sd"`
    else
            DiskList=$DiskList
    fi

    num=1
    fdd=1
    for i in $DiskList
    do
        Val=`LANG=en_US; echo $i | cut -d ':' -f 2`
        Val=$[$Val / 1024 / 1024 / 1024]

        Disk=`LANG=en_US; echo $i | cut -d ':' -f 1`

        if [[ `echo "$Val < 35" | bc` -eq 1 ]]; then
            continue
        fi

        Sd=${Disk##*/}
        fdd=$( cat /sys/block/${Sd}/queue/rotational )
        if [[ $fdd == 0 || $num == 0 ]];then
                num=0
                if [[ $fdd == 1 ]];then
                        continue
                fi
                if [[ `echo "$Val < $Size_0" | bc` -eq 1 ]]; then
                         Size_0=$Val
                        Ret_back=$Disk
                fi

                 if [[ `echo "$Val >= $MIN" | bc` -eq 1 ]] &&
                   [[ `echo "$Val <= $MAX" | bc` -eq 1 ]] &&
                   [[ `echo "$Val < $Size_2" | bc` -eq 1 ]]; then
                        Size_2=$Val
                        Ret=$Disk
                 fi
                 continue
        fi

        if [[ `echo "$Val < $Size_0" | bc` -eq 1 ]]; then
                 Size_0=$Val
                 Ret_back=$Disk
        fi

        if [[ `echo "$Val >= $MIN" | bc` -eq 1 ]] &&
           [[ `echo "$Val <= $MAX" | bc` -eq 1 ]] &&
           [[ `echo "$Val < $Size_1" | bc` -eq 1 ]]; then
                Size_1=$Val
                Ret=$Disk
        fi
    done



    if [ -z $Ret ]; then
        Ret=$Ret_back
    fi

    echo $Ret
    sed -i "/devpath/d" /tmp/ky-installer.cfg
    echo "devpath=$Ret" >>/tmp/ky-installer.cfg
}

auto_disk_slot () {
        slot=
        slot=$(dmidecode -t 41 |grep "$1" -A 4|grep "Bus Address"| awk '{print $3}') || true
        if [ -n "${slot}" ]; then
                disk_menu=$(lsblk -d | grep -v NAME | grep -v loop | grep -v sr | awk '{print $1}')
                for i in ${disk_menu}
                do
                        #排除usb设备的影响
                        disk_remove=$(readlink /sys/block/$i | grep ${slot})|| true
                        if [ -n "${disk_remove}" ]; then
                                Ret=/dev/$i
                        else
                                continue
                        fi

                done
        fi
        if [ -z $Ret ]; then
                auto_disk
        else
                echo "devpath=$Ret=$1=${slot}"
                sed -i "/devpath/d" /tmp/ky-installer.cfg
                echo "devpath=$Ret" >>/tmp/ky-installer.cfg
        fi
}


#根据总线速度选择读写最快的系统盘(若速度相同,再比较磁盘容量)
auto_disk_speed () {
    disk_speed=0
    disk_width=0
    disk_size=0
    ret_disk=
    disk_menu=$(lsblk -d | grep -v NAME | grep -v loop | grep -v sr | awk '{print $1}')
    for i in ${disk_menu}
    do
        #排除usb设备的影响
        disk_remove=$(readlink /sys/block/$i | grep usb)|| true
        if [ -n "${disk_remove}" ]; then
                echo "$i is usb disk"
        else
                disk_search=
                disk_search=$(readlink /sys/block/$i)
                disk_search=${disk_search#*/}
                disk_search=/sys/${disk_search}

                echo ${disk_search}
                if [[ -e ${disk_search} ]]; then
                        until [[ -e  "${disk_search}/max_link_speed" ]]
                        do
                                disk_search=${disk_search%/*}
                                if [[ -e ${disk_search} ]]; then
                                        echo ${disk_search}
                                else
                                        break
                                fi
                        done
                        disk_current_width=${disk_search}/current_link_width
                        disk_search=${disk_search}/current_link_speed
                fi
                if [[ -e ${disk_search} ]]; then
                        test_speed=0
                        test_size=0
                        test_speed=$(cat ${disk_search} | awk '{print $1}')

                        #当未检测到总线最大速度时,跳出循环
                        if [[ -n ${test_speed} ]]; then
                                echo "/dev/$i=${test_speed}GT/s"
                                #过滤掉小于50G的磁盘
                                test_size=$(fdisk -s "/dev/$i")
                                test_size=$(( test_size / 1024 / 1024))
                                if [[ `echo "${test_size} < 50" | bc` -eq 1 ]]; then
                                        continue
                                fi
                                test_size=0
                        else
                                continue
                        fi

                        #先比较总线的峰值读写速度,峰值速度一致,再比较磁盘容量
                        if [[ `echo "${test_speed} > ${disk_speed}" | bc` -eq 1 ]]; then
                                disk_speed=${test_speed}
                                ret_disk=/dev/$i
                                if [[ -e ${disk_current_width} ]]; then
                                        disk_width=$(cat ${disk_current_width})
                                fi
                        elif [[ `echo "${test_speed} == ${disk_speed}" | bc` -eq 1 ]]; then
                                if [[ -e ${disk_current_width} ]]; then
                                        test_width=$(cat ${disk_current_width})
                                        if [[ `echo "${test_width} > ${disk_width}" | bc` -eq 1 ]]; then
                                                disk_width=${test_width}
                                                ret_disk=/dev/$i
                                        elif [[ `echo "${test_width} == ${disk_width}" | bc` -eq 1 ]]; then
                                                #固态盘优先级高于机械盘
                                                disk_i=${ret_disk##*/}
                                                fisk_fdd=$( cat /sys/block/${disk_i}/queue/rotational )
                                                test_fdd=$( cat /sys/block/$i/queue/rotational )

                                                if [[ `echo "${fisk_fdd} > ${test_fdd}" | bc` -eq 1 ]]; then
                                                        ret_disk=/dev/$i
                                                elif [[ `echo "${fisk_fdd} == ${test_fdd}" | bc` -eq 1 ]]; then
                                                        test_size=$(fdisk -s "/dev/$i")
                                                        disk_size=$(fdisk -s "${ret_disk}")
                                                        if [[ `echo "${test_size} > ${disk_size}" | bc` -eq 1 ]]; then
                                                                ret_disk=/dev/$i
                                                        fi
                                                fi

                                        else
                                                continue
                                        fi
                                else
                                        continue
                                fi
                        fi
                        echo "ret_disk=${ret_disk}"
                fi
        fi
    done
    if [[ -e ${ret_disk} ]]; then
        echo "search disk ending"
        echo "ret_disk=${ret_disk}"
        sed -i "/devpath/d" /tmp/ky-installer.cfg
        echo "devpath=${ret_disk}" >>/tmp/ky-installer.cfg
    else
        auto_disk
    fi
}

#单块硬盘指定错误磁盘也能找到正常磁盘安装
get_disk() {
    disk_num=0
    disk_menu=$(lsblk -d | grep -v NAME | grep -v loop | grep -v sr | awk '{print $1}')
    for i in ${disk_menu}
    do
        disk_remove=$(readlink /sys/block/$i | grep usb)|| true
        if [ -n "${disk_remove}" ]; then
                echo "$i is usb disk"
        else
                disk_num=`expr ${disk_num} + 1`
        fi

    done
    echo ${disk_num}
    if [[ ${disk_num} -ge 2 ]]; then
        echo " DISK  more than 1"
    else
        auto_disk
    fi
}

get_data_disk() {
    disk_num=0
    disk_menu=$(lsblk -d | grep -v NAME | grep -v loop | grep -v sr | awk '{print $1}')
    for i in ${disk_menu}
    do
        disk_remove=$(readlink /sys/block/$i | grep usb)|| true
        if [ -n "${disk_remove}" ]; then
                echo "$i is usb disk"
        else
                disk_num=`expr ${disk_num} + 1`
        fi
    done

    echo ${disk_num}
    if [[ ${disk_num} -ge 2 ]]; then
        echo " DISK number is more than 2"
        disk_num=0
        disk_menu=$(lsblk -d | grep -v NAME | grep -v loop | grep -v sr | awk '{print $1}')
        disk=$(get_value devpath)

        DiskList=`LANG=en_US; sudo fdisk -l | grep -v "${disk}" | awk '{if($1=="Disk" && $2!="identifier:" && $2!="model:") print $2$5}'`
        MAX=2050
        MIN=50
        Size_0=50
        Size_1=50
        Size_2=50

        Ret=

        if echo $DiskList | grep -q 'nvme'; then
                DiskList=`LANG=en_US; sudo fdisk -l | grep -v "${disk}" | awk '{if($1=="Disk" && $2!="identifier:" && $2!="model:") print $2$5}' | grep "nvme"`
        elif echo $DiskList | grep -q 'sd'; then
                DiskList=`LANG=en_US; sudo fdisk -l | grep -v "${disk}" | awk '{if($1=="Disk" && $2!="identifier:" && $2!="model:") print $2$5}' | grep "sd"`
        else
            DiskList=$DiskList
        fi

        num=1
        fdd=1

         for i in $DiskList
         do
                Val=`LANG=en_US; echo $i | cut -d ':' -f 2`
                Val=$[$Val / 1024 / 1024 / 1024]

                Disk=`LANG=en_US; echo $i | cut -d ':' -f 1`

                if [[ `echo "$Val < 35" | bc` -eq 1 ]]; then
                        continue
                fi

                Sd=${Disk##*/}
                fdd=$( cat /sys/block/${Sd}/queue/rotational )
                if [[ $fdd == 0 || $num == 0 ]];then
                        num=0
                        if [[ $fdd == 1 ]];then
                                continue
                        fi
                        if [[ `echo "$Val > $Size_0" | bc` -eq 1 ]]; then
                                 Size_0=$Val
                                 Ret_back=$Disk
                        fi

                        if [[ `echo "$Val >= $MIN" | bc` -eq 1 ]] &&
                           [[ `echo "$Val <= $MAX" | bc` -eq 1 ]] &&
                           [[ `echo "$Val > $Size_2" | bc` -eq 1 ]]; then
                                Size_2=$Val
                                Ret=$Disk
                        fi
                        continue
                fi

                if [[ `echo "$Val > $Size_0" | bc` -eq 1 ]]; then
                         Size_0=$Val
                         Ret_back=$Disk
                fi

                if [[ `echo "$Val >= $MIN" | bc` -eq 1 ]] &&
                [[ `echo "$Val <= $MAX" | bc` -eq 1 ]] &&
                [[ `echo "$Val > $Size_1" | bc` -eq 1 ]]; then
                        Size_1=$Val
                        Ret=$Disk
                fi
         done

         if [ -z $Ret ]; then
                sed -i "/data-device/d" /tmp/ky-installer.cfg || true
         else
                echo $Ret
                sed -i "/data-device/d" /tmp/ky-installer.cfg
                echo "data-device=$Ret" >>/tmp/ky-installer.cfg
         fi
    else
         sed -i "/data-device/d" /tmp/ky-installer.cfg
    fi

}



get_is_efi() {
    if type archdetect >/dev/null 2>&1; then
        archdetect=$(archdetect)
    else
        archdetect=unknown/generic
    fi
    arch=${archdetect%/*}
    sub=${archdetect#*/}
    ret=true
    case "$arch" in
        alpha|sw64)
            ret=true;;
        amd64|i386)
            case "$sub" in
                mac|efi)
                ret=true;;
                *)
                ret=false;;
            esac;;
        arm|armhf|armel)
            ret=false;;
        arm64)
            ret=true;;
        mips64el)
            case "$sub" in
                efi)
                ret=true;;
                *)
                ret=false;;
            esac;;
        mips|mipsel)
            ret=false;;
        *)
            ret=true;;
    esac

    # 华为 990 设置为 efi
    if egrep -qi 'kirin.?9[09]0' /proc/cpuinfo; then
        ret=true
    fi
    if egrep -qi 'PANGU.M900' /proc/cpuinfo; then
        ret=true
    fi

    echo $ret
}

readonly is_efi=$(get_is_efi)
#虚拟机去掉备份还原分区
readonly virtual_machine=$(get_value virtual-machine)

# 优先使用引导参数中设置
set_swap_mode() {
    if grep -q kyswapfile /proc/cmdline; then
        readonly is_swapfile=true
        return 0
    fi
    readonly is_swapfile=$(get_value enable-swapfile)
}
set_swap_mode

# 优先使用界面配置中的设置
get_oem_mode() {
    ret=false
    oem_config=$(get_value oem-config)
    if [[ ${oem_config} == "true" ]]; then
	    ret=true
    fi
    if [[ ${oem_config} == "false" ]]; then
	    ret=false
    fi
    if grep -qi 'oem-config/enable=true' /proc/cmdline; then
        ret=true
    fi
    echo $ret
}

readonly is_oem_mode=$(get_oem_mode)

# 审核模式，工厂安装
get_audit_mode() {
    ret=false
    if grep -qiE 'auditmode|ple-mode' /proc/cmdline; then
        ret=true
    fi
    echo $ret
}

readonly is_audit_mode=$(get_audit_mode)

get_is_990_9a0() {
    ret=false
    # 匹配 kirin 990 5g, kirin990, kirin 9006c
    if egrep -qi 'kirin.?9[09]0' /proc/cpuinfo; then
        ret=true
    fi
    if egrep -qi 'PANGU.M900' /proc/cpuinfo; then
        ret=true
    fi
    if dmidecode -t 11|grep "String 4"|grep PGUX > /dev/null ;then
        ret=true
    fi

    echo $ret
}

readonly is_990_9a0=$(get_is_990_9a0)

readonly swap_file=swap_file

get_is_minimal() {
    ret=false
    if egrep -qi kylin-minimal /proc/cmdline; then
        ret=true
    fi
    echo $ret
}
readonly is_minimal=$(get_is_minimal)

readonly is_ghost=$(get_value ghost)

readonly isluks_lvm=$(get_value encrypty)

readonly tpm=$(get_value TPMEncrypt)

readonly lvm=$(get_value lvm)

run_scripts_dir() {
    name=$1
    for i in ${name}/*; do
        if [ -x "$i" ]; then
            filename=$(basename $i)
            echo "$(date +'%b %d %H:%M:%S') ${filename}"
            . "$i"
        else
            msg "跳过 $i"
        fi
    done
}

run_scripts_hooks() {
    name=$1
    for i in ${name}/*; do
        if [ -x "$i" ]; then
            filename=$(basename $i)
            echo "$(date +'%b %d %H:%M:%S') ${filename}" >>${LOG_FILE}
            if [[ $i == "${name}/TRANS.TBL" ]];then
                    echo "跳过 $i" >>${LOG_FILE}
                    continue
            else
                    . "$i" >>${LOG_FILE} 2>&1
            fi
        else
            msg "跳过 $i"
        fi
    done
}

get_hw_typedata() {
    ret=
    if [ -f /proc/hardware ];then
        if grep -rin "huawei.*9006" /proc/hardware; then
                ret=klvv
        elif grep -rinE "[huawei|kirin].*990" /proc/hardware ;then
                typedata=$(dmidecode |grep "String 4")
                if [[ $typedata =~ .*KVK90.* ]];then
                        ret=klvu
                else
                        ret=pguv
                fi
        elif grep -irn "pangu.*900" /proc/hardware;then
                typedata=$(dmidecode |grep "String 4")
                if [[ $typedata =~ "PWC30" ]];then
                        ret=pguw
                else
                        ret=pangum900
                fi
        elif grep -irnE "charlotte|9000C" /proc/hardware; then
                ret=pgux
        fi
    else
        typedata=$(dmidecode -s system-product-name)
        ret="${typedata#* }"
        if [[ "${typedata}" =~ "PGUV" ]]; then
                ret=pguv
        elif [[ "${typedata}" =~ "KLVU" ]]; then
                ret=klvu
        elif [[ "${typedata}" =~ "KLVV" ]]; then
                ret=klvv
        fi
    fi
    if dmidecode -t 11 |grep "String 4"|grep "PGUX";then
            ret=pgux
    fi
    echo $ret

}

readonly hw_typedata=$(get_hw_typedata)

# 华为更改启动项
hw_next_boot() {
    next_boot=$1
    boot_order_file="/boot/efi/EFI/BOOT/HwBootOrder.ini"
    case $next_boot in
    hd|HD)
          echo HwBootOrder=0123 > ${boot_order_file}
          ;;
    pxe|PXE)
          echo HwBootOrder=2013 > ${boot_order_file}
          ;;
    *)
          echo "Error argument $next_boot, only hd or pxe is supported"
          exit 1
          ;;
    esac

    msg "set next order to $next_boot is done"
}

do_bind_data() {
    new_sysroot_dir=$(get_value new-root)
    mkdir -p $new_sysroot/{home,root}
    mkdir -p $new_sysroot_dir/data/{home,root,usershare}

    mount --bind $new_sysroot/data/home $new_sysroot_dir/home
    mount --bind $new_sysroot_dir/data/root $new_sysroot_dir/root

    chmod -R 1777 $new_sysroot_dir/data/usershare >/dev/null
}

do_error() {
    sync
    sleep 1
    error_info=$1
    case $error_info in
    'copy')
          echo "拷贝基础文件系统失败
1.批量拷贝过程,由于系统文件较大,会存在低概率拷贝失败,可进行重新安装
2.PXE安装,网络不稳定也会出现拷贝卡死或失败的情况,可进行重新安装
3.重新安装依然报拷贝失败,请检查启动盘制作环节,是否出现镜像拷贝文件丢失或损坏
        进入启动盘根目录执行下面命令进行检测
                sudo md5sum -c md5sum.txt | grep -v success | grep -v 成功
安装日志可以查看上面日志,也可查看日志文件/var/log/installer/kylin-os-installer-detail.log"  >> ${LOG_FILE}
          ;;
    'third-party')
          echo "位于third-party目录下的第三方包安装失败
请检查位于镜像third-party目录下的第三方包
安装日志可以查看上面日志,也可查看日志文件/var/log/installer/kylin-os-installer-detail.log"  >> ${LOG_FILE}
          ;;
    'post-actions')
          echo "执行kylin-post-actions 脚本失败
请检查位于镜像根目录下的.kylin-post-actions脚本
安装日志可以查看上面日志,也可查看日志文件/var/log/installer/kylin-os-installer-detail.log"  >> ${LOG_FILE}
          ;;
    'post-actions-nochroot')
          echo "执行kylin-post-actions-nochroot脚本失败
请检查位于镜像根目录下的.kylin-post-actions-nochroot脚本
安装日志可以查看上面日志,也可查看日志文件/var/log/installer/kylin-os-installer-detail.log"  >> ${LOG_FILE}
          ;;
    'hooks-prepare')
          echo "执行hooks/prepare目录下定制脚本失败
请检查位于hooks/prepare目录下的脚本
安装日志可以查看上面日志,也可查看日志文件/var/log/installer/kylin-os-installer-detail.log"  >> ${LOG_FILE}
          ;;
    'hooks-inchroot')
          echo "执行hooks/in_chroot目录下定制脚本失败
请检查位于hooks/in_chroot目录下的脚本
安装日志可以查看上面日志,也可查看日志文件/var/log/installer/kylin-os-installer-detail.log"
          ;;
    'hooks-after')
          echo "执行hooks/after目录下定制脚本失败
请检查位于hooks/after目录下的脚本
安装日志可以查看上面日志,也可查看日志文件/var/log/installer/kylin-os-installer-detail.log"  >> ${LOG_FILE}
          ;;
    *)
          echo "安装日志可以查看上面日志,也可查看日志文件/var/log/installer/kylin-os-installer-detail.log"  >> ${LOG_FILE}
          ;;
    esac
    exit 1
}


