#!/usr/bin/bash
# boot-gpu_test.sh


if [[ $UCLOUD_INIT != 0 ]];then
    # 验证 UCLOUD_INIT=1
    source /ucloud/exports.ini
    [[ $UCLOUD_INIT == 1 ]] && exit 1
fi

get_env(){
    dev_sda3=$(df -PTh / |grep -v Filesystem |awk '{print $1}')
    UUID=$(blkid -s UUID -o value "$dev_sda3")
    [[ $UUID ]] || UUID=$(blkid -s UUID -o value /dev/sda3)

    # 获取默认路由的网卡名
    interface=$(ip route show default | awk '/default/ {print $5}')
    # 获取网卡的IP地址
    ip_addr=$(ip addr show dev $interface | awk '/inet / {print $2}' | awk -F/ '{print $1}')
    # 获取网关地址
    gateway=$(ip route show default | awk '/default/ {print $3}')
    # 获取子网掩码位数
    netmask=$(ip addr show dev $interface |grep -v '00:00:00:00:00:00' | awk '/inet / {print $2}' | awk -F/ '{print $2}')
    # 获取网卡的MAC地址
    MAC_ADDR=$(ip link show dev $interface | awk '/link\/ether/ {print $2}')
}

get_env

echo "UUID: $UUID"
echo "网卡名: $interface"
echo "IP地址: $ip_addr"
echo "网关地址: $gateway"
echo "子网掩码位数: $netmask"
echo "MAC地址: $MAC_ADDR"

echo "ip=${ip_addr}::${gateway}:${netmask}:::none:10.64.46.101" "MAC_ADDR=${MAC_ADDR}"


if [[ -z $ip_addr && -z $gateway && -z $netmask && -z $MAC_ADDR && -z $UUID ]] ;then
exit 1
fi

cd /dev/shm/ || true

[[ $KERNEL_VERSION ]] || KERNEL_VERSION=vmlinuz-5.15.0-43-generic
[[ $INITRD_VERSION ]] || INITRD_VERSION=initrd-5.15.0-43-535.161.07-704.cpio.xz
[[ $SCRIPTS_VERSION ]] || SCRIPTS_VERSION=initrd-scripts-gpu.img
[[ $HTIME ]] || HTIME=6h
[[ $GPU_TASK ]] || GPU_TASK=all

[[ $HTIME =~ h ]] && test_time=$((${HTIME/h}*60))
[[ $MTIME =~ m ]] && test_time=$((${HTIME/m}))
[[ $TIME =~ h ]] && test_time=$((${TIME/h}*60))
[[ $TIME =~ m ]] && test_time=$((${TIME/m}))


# curl softlib.service.ucloud.cn/usa/images/boot/boot_pangu_gpu_test.sh
# [[ $DOWNLOAD_URL ]] || DOWNLOAD_URL=10.77.8.4/stress_test/boot
DOWNLOAD_URL=${DOWNLOAD_URL:-softlib.service.ucloud.cn/usa/stress_test}

download_fun(){
    wget_args=' -q '
    [[ $debug ]] && wget_args=''
    if ! wget $wget_args -N "${DOWNLOAD_URL}/boot/$1" 
    then
        echo download $1 error
        exit 1
    fi
}

download_fun "$KERNEL_VERSION"
download_fun "$SCRIPTS_VERSION"
download_fun "$INITRD_VERSION"

if ! cat "$INITRD_VERSION" "$SCRIPTS_VERSION" > initrd.img
then
    echo download initrd error
    exit 1
fi

ls -ltrha


vmlinuz_version=$(file -b "$KERNEL_VERSION" | grep -oP '(?<=version )\d+\.\d+\.\d+-\d+')

nameserver=$(cat /etc/resolv.conf |grep nameserver  |tr ' ' '=' |tr '\n' ' ')

[[ $debug ]] && echo kexec -l "$KERNEL_VERSION" --initrd=initrd.img --append="console=tty0 gpu_task=$GPU_TASK uuid=$UUID test_time=$test_time hahaha=off initrd_version=$INITRD_VERSION vmlinuz_version=$vmlinuz_version ip=${ip_addr}::${gateway}:${netmask}:::none:10.64.46.101 mac_addr=${MAC_ADDR} $nameserver net.ifnames=0 biosdevname=0 modprobe.blacklist=nouveau rd.retry=300 rootdelay=300 rootwait=300 "

kexec -l "$KERNEL_VERSION" --initrd=initrd.img --append="console=tty0 gpu_task=$GPU_TASK uuid=$UUID test_time=$test_time hahaha=off initrd_version=$INITRD_VERSION vmlinuz_version=$vmlinuz_version ip=${ip_addr}::${gateway}:${netmask}:::none:10.64.46.101 mac_addr=${MAC_ADDR} $nameserver net.ifnames=0 biosdevname=0 modprobe.blacklist=nouveau rd.retry=300 rootdelay=300 rootwait=300 "

wall "Change to MINIOS Ubuntu $KERNEL_VERSION . Please Wait ... $(date +%F_%T_%Z)"

(nohup sleep 5 &&  kexec -e &) ; exit 0

# curl -s softlib.service.ucloud.cn/usa/stress_test/pangu-boot-stress_test.sh |UCLOUD_INIT=0 bash -x && exit 0

