海运的博客

centos/debian自动通过网络远程安装系统配置脚本

发布时间:September 30, 2018 // 分类: // No Comments

此脚本用于生成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

评论已关闭

分类
最新文章
最近回复
  • 海运: 恩山有很多。
  • swsend: 大佬可以分享一下固件吗,谢谢。
  • Jimmy: 方法一 nghtp3步骤需要改成如下才能编译成功: git clone https://git...
  • 海运: 地址格式和udpxy一样,udpxy和msd_lite能用这个就能用。
  • 1: 怎么用 编译后的程序在家里路由器内任意一台设备上运行就可以吗?比如笔记本电脑 m参数是笔记本的...
  • 孤狼: ups_status_set: seems that UPS [BK650M2-CH] is ...
  • 孤狼: 擦。。。。apcupsd会失联 nut在冲到到100的时候会ONBATT进入关机状态,我想想办...
  • 海运: 网络,找到相应的url编辑重发请求,firefox有此功能,其它未知。
  • knetxp: 用浏览器F12网络拦截或监听后编辑重发请求,修改url中的set为set_super,将POS...
  • Albert: 啊啊啊啊啊啊啊啊啊 我太激动了,终于好了英文区搜索了半天,翻遍了 pve 论坛没找到好方法,博...