此脚本用于生成ubuntu和centos网络重装引导项,ks和pressed配置文件见前文,上传到http服务器并替换脚本中内容,在centos7和ubuntu18.04上测试安装centos7和ubuntu18.04通过。
#!/bin/bash
#set -x
install=ubuntu
network=static
stage2="nfs:nfsvers=4:www.haiyun.me:/"
cidr2mask () {
set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
[ $1 -gt 1 ] && shift $1 || shift
echo ${1-0}.${2-0}.${3-0}.${4-0}
}
function valid_ip() {
local ip=$1
local stat=1
if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFS=$IFS
IFS='.'; ip=($ip); IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
stat=$?
fi
return $stat
}
which wget &> /dev/null && which ip &> /dev/null || {
echo '请先安装wget和ip'
exit;
}
if [ "$network" == 'static' ]; then
address=`ip -o -f inet addr show | awk '/scope global/ {print $4}'`
ip=`echo $address|awk -F'/' '{print $1}'`
cidr=`echo $address|awk -F'/' '{print $2}'`
rou=`ip rou|awk '/default via/ {print $3}'`
mask=`cidr2mask $cidr`
echo 'ip:' $ip
echo 'route:' $rou
echo 'netmask:' $mask
valid_ip "$ip" && valid_ip "$mask" && valid_ip "$rou" || {
echo '获取网络信息失败'
exit;
}
echo '请核对以上IP信息是否正确'
fi
if [ -f "/etc/redhat-release" ]; then
dist="centos"
grubfile=/boot/grub2/grub.cfg
grubcmd=grub2-mkconfig
else
grubfile=/boot/grub/grub.cfg
grubcmd=grub-mkconfig
dist="ubuntu"
fi
#root=`grep "set root" $grubfile|sed -e 's/^[ \t]*//'|head -n 1`
root=`grep 'set root' $grubfile |sed -e 's/^[ \t]*//'|sort|uniq -c|head -n 1|awk '{print $2,$3}'`
if mount|grep -q /boot; then
dir=/
else
dir=/boot/
fi
vmlinuzfile=${dir}vmlinuz
initrdfile=${dir}initrd.img
rm -rf /boot/vmlinuz
rm -rf /boot/initrd.img
if [ "$install" == 'centos' ]; then
mem=`free -m | grep Mem | awk '{print $2}'`
if [ ! $stage2 ] && (($mem < 1500)); then
echo '内存小于1.5G要通过网络安装centos7可能会失败'
exit;
fi
base=https://mirrors.aliyun.com/centos/7/os/x86_64
wget -q ${base}/isolinux/vmlinuz -O /boot/vmlinuz || exit;
wget -q ${base}/isolinux/initrd.img -O /boot/initrd.img || exit;
if [ "$network" == 'static' ]; then
linux16="$vmlinuzfile inst.ks=https://www.haiyun.me/centos7.cfg net.ifnames=0 biosdevname=0 inst.headless ip=${ip}::${rou}:${mask}::eth0:none nameserver=8.8.8.8"
else
linux16="$vmlinuzfile inst.ks=https://www.haiyun.me/centos7.cfg net.ifnames=0 biosdevname=0 inst.headless ip=dhcp"
fi
if [ $stage2 ]; then
linux16="$linux16 inst.stage2=$stage2"
fi
else
base=https://mirrors.aliyun.com/ubuntu/dists/bionic/main/installer-amd64/current/images/netboot/ubuntu-installer/amd64
wget -q ${base}/linux -O /boot/vmlinuz || exit;
wget -q ${base}/initrd.gz -O /boot/initrd.img || exit;
if [ "$network" == 'static' ]; then
#linux16="$vmlinuzfile ks=https://www.haiyun.me/ubuntu-ks.cfg domain= hostname=ubuntu-server interface=auto netcfg/get_ipaddress=${ip} netcfg/get_netmask=${mask} netcfg/get_gateway=${rou} netcfg/get_nameservers=8.8.8.8 netcfg/disable_autoconfig=true"
linux16="$vmlinuzfile auto=true url=https://www.haiyun.me/ubuntu.cfg keymap=us domain= hostname=ubuntu-server interface=auto netcfg/get_ipaddress=${ip} netcfg/get_netmask=${mask} netcfg/get_gateway=${rou} netcfg/get_nameservers=8.8.8.8 netcfg/disable_autoconfig=true"
else
#linux16="$vmlinuzfile ks=https://www.haiyun.me/ubuntu-ks.cfg domain= hostname=ubuntu-server interface=auto"
linux16="$vmlinuzfile auto=true url=https://www.haiyun.me/ubuntu.cfg keymap=us domain= hostname=ubuntu-server interface=auto"
fi
fi
[[ -f /boot/vmlinuz ]] && [[ -f /boot/initrd.img ]] || {
echo '引导文件不存在'
exit;
}
cat > /etc/grub.d/40_custom <<EOF
#!/bin/sh
exec tail -n +3 \$0
menuentry 'netinstall' {
$root
linux16 $linux16
initrd16 $initrdfile
}
EOF
sed -i 's/GRUB_DEFAULT=.*/GRUB_DEFAULT="netinstall"/' /etc/default/grub
$grubcmd -o $grubfile
cat /etc/grub.d/40_custom
https://www.haiyun.me/archives/1246.html
https://www.haiyun.me/archives/1249.html
标签:none