Re-install OS remotely

  1. login your server as root
  2. run the script below, check everything is ok then reboot
  3. wait till the server boot, then use vnc client connect your server by "$IP:1"
  4. reinstall your server step by step

be careful you'd better test under a virtual machine environment first

sysver=`awk -F"release" '{print $NF}' /etc/redhat-release | cut -d. -f1 | sed 's/ //g' | awk -F'(' '{print $1}'| head -n 1`

if [ ${sysver} == "6" ]; then
    echo "Centos 6 Install"
    #YUM=`yum -v repolist | grep -A 8 'Repo-id      : base' | grep baseurl | awk {'print $3'}`
    YUM="http://mirrors.163.com/centos-vault/6.9/os/x86_64/"
    GATEWAY=`ip route show | grep default | awk {'print $3'}`
    DEVICE=`ip route show | grep default | awk {'print $5'}`
    IP=`ifconfig $DEVICE | grep 'inet ' | awk {'print $2'} | awk -F ':' {'print $2'}`
    MAC=`ifconfig $DEVICE | grep HWaddr | awk {'print $5'}`
    BCAST=`ifconfig $DEVICE | grep 'inet ' | awk {'print $3'} | awk -F ':' {'print $2'}`
    NETMASK=`ifconfig $DEVICE | grep 'inet ' | awk {'print $4'} | awk -F ':' {'print $2'}`
    ROOT=`grep -v '#' /etc/grub.conf | grep 'root (hd' | awk {'print $2'}`
    DNS=`grep -v '#' /etc/resolv.conf | grep nameserver | head -1 | awk {'print $2'}`

    #wget $YUM/images/pxeboot/initrd.img -O  /boot/initrd.img.cent.pxe
    #wget $YUM/images/pxeboot/vmlinuz -O  /boot/vmlinuz.cent.pxe
    curl $YUM/images/pxeboot/initrd.img -o  /boot/initrd.img.cent.pxe
    curl $YUM/images/pxeboot/vmlinuz -o  /boot/vmlinuz.cent.pxe


    echo "Network Info: $DEVICE $IP/$NETMASK GATEWAY: $GATEWAY"
    echo "MAC:$MAC"
    echo "BCAST:$BCAST DNS:$DNS"
    echo "YUM: $YUM"

cat << EOF > /tmp/grub.conf
default=0
timeout=5
splashimage=$ROOT/grub/splash.xpm.gz
hiddenmenu
title Centos 6 Install
        root $ROOT
        kernel /vmlinuz.cent.pxe vnc headless vncpassword=xinyong ip=$IP netmask=$NETMASK gateway=$GATEWAY dns=$DNS ksdevice=$MAC method=$YUM lang=en_US keymap=us
        initrd /initrd.img.cent.pxe 
EOF

    cp -pr /boot/grub/grub.conf /boot/grub/grub.conf.bak
    cp -pr /tmp/grub.conf /boot/grub/grub.conf
fi

if [ ${sysver} == "7" ]; then
    echo "Centos 7 Install"
    #YUM=`yum -v repolist | grep -A 8 'Repo-id      : base' | grep baseurl | awk {'print $3'}`
    YUM="http://mirrors.163.com/centos/7.9.2009/os/x86_64/"
    GATEWAY=`ip route show | grep default | awk {'print $3'}`
    DEVICE=`ip route show | grep default | awk {'print $5'}`
    IP=`ifconfig $DEVICE | grep 'inet '| awk {'print $2'}`
    MAC=`ifconfig $DEVICE | grep 'ether ' | awk {'print $2'}`
    NETMASK=`ifconfig $DEVICE | grep 'inet ' | awk {'print $4'}`
    BCAST=`ifconfig $DEVICE | grep 'inet ' | awk {'print $3'} | awk -F ':' {'print $2'}`
    DNS=`grep -v '#' /etc/resolv.conf | grep nameserver | head -1 | awk {'print $2'}`

    echo "Network Info: $DEVICE $IP/$NETMASK GATEWAY: $GATEWAY"
    echo "MAC:$MAC"
    echo "BCAST:$BCAST DNS:$DNS"
    echo "YUM: $YUM"

    curl $YUM/images/pxeboot/initrd.img -o  /boot/initrd.img.cent.pxe
    curl $YUM/images/pxeboot/vmlinuz -o  /boot/vmlinuz.cent.pxe

    touch /etc/grub.d/40_custom
    GRUBCONFIG=/etc/grub.d/40_custom
    cp -pr $GRUBCONFIG ${GRUBCONFIG}.bak

    cat << EOF > $GRUBCONFIG
#!/bin/sh
exec tail -n +3 $GRUBCONFIG 
menuentry 'Centos 7 Install' {
    linux16 /vmlinuz.cent.pxe ksdevice=$MAC method=$YUM net.ifnames=0 biosdevname=0 ip=$IP netmask=$NETMASK gateway=$GATEWAY nameserver=$DNS sshd vnc vncpassword=xinyong
    initrd16 /initrd.img.cent.pxe
}
EOF

    chmod +x /etc/grub.d/40_custom
    sed -i "s/^GRUB_TIMEOUT=.*/GRUB_TIMEOUT=5/" /etc/default/grub
    grub2-mkconfig -o /boot/grub2/grub.cfg
    grub2-set-default  "Centos 7 Install"
    grub2-mkconfig -o /boot/grub2/grub.cfg
fi
powered by GitbookFile Modify: 2022-03-23 10:45:50

results matching ""

    No results matching ""