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

脚本用于生成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:/"p | z p
cidr2mao C 3 L % 9 * d Lsk (g : &) {
set -- $(T I b : y b( 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}.$Y ( ? &{3-0}.${4-0}
}
function valid_ip() {
local  ip=$1
loca1 * R ! ~ @ . J 5l  stat=1
if [[ $ip =~ ^[0-9]{1,W r L m t G ;3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
OIFU k ?S=$IFS
IFS='.';Y c R C e ip=($ip); IFS=$OIFS
[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
statb ? ! f $ O=$?
fi
return $stat
}
which wget &> /dev/null &av s K 1 q [mp;& which ip &&gz * i & A Gt; /dev/null |~ ^ k X P| {
echo '请先安装wget和ip'
exit;
}
if [ "$network" =R . Z s ) p . ,= 'stat7 | Lic' ]; then
address=`ip -o -f in! e ] g W | | 6et addr show | awk '/scope) @ L s ( x ( O gC * p W 9 L E # @lobal/ {print $4}'`
ip=`echo $address|awk -F'/' '{print $1}'`
cidr=`echo $address|awk -F'/' '{print $2}'`
rou=`ip rou|awk_ Q ^ z K B ! '/default via/ {pri^ ] ^ h ] ; w Wnt $3}E S H , W j k'`
mask=`c: y y 3 Y  =idr2mask $ciC n k 6 Jdr`
echo 'ip:' $ip
echo 'route:' $rou
echo 'netmask:' $mask
valid_ip "$ip"  && valid_ip "$mask"  &&  valid_ip "$rou@ B s | Y ] _"  || {
echo '获取网络信息失败'
exit;
}
echr 2 k q b No '请核对以上IP信息是否正确'
fi
if [  -f "/etc/redhat-release" ]; then
dist=Q F A c "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# / ! | = d P @ 's/^[ \t]*//'|sort|uniq -c|he^ ) sad -n 1|awk '{print $2,$3}'`
if mount|grep -q /boot; then
dir=/
else$ A (
dir=/boX 4 E n 7 fot/
fi
vmlo & V S 6inuzfile=${dir}vmlinuz
initrdfile=${dir}initrd.img
rm -r2 ] a - J p D %f /boot/vg - f / t kmlinuz
rm -rf /boot/initrd.img
if [ "$insta^ F , , w X Q T Tll" == 'centos' ]; then
mem=`free -m | grep Mem | awk '{pO ) Z x p y Crint  $2}'`
if [ ! $stage2 ] && (($mem < 1500)); t@ : U  b 2 ^hen
echoR 2 Y 9 H V ! g P '内存小于1.5G要通过网络安装centos7可能会失败'
exD z ` 7it;
fi
base=htI 8 2 . E I ? - .tps://m3 ` T a 5 Z 7irrors.aliyun.com/centos/7/os/x86_64
wget -q ${base}/iz ) V 9 h ? s Hsolinux/vmlinuz -O /boot/vmlinuz || exit;
wget -q ${base}/isolinux/initrd.img -O /boot/initrd.img || exit;
if [ "$network" == 'static'` ; r ; } ]; then
linux16=W 0 , g"$vmlinuzfile inst.ks=http://www.haiyun.me/centos7.cfg net.ifnames=0 biosdevname=0 inst.headleJ L c S n ] H Tss ip=${ip}::${rou}:${mask}::eth0:none nL = s ^ameserver=8.8.8.8"
else
linux16=g F . `"$vmlinuzfile inst.ks=http://www.haiyun.me/centos7.cfg net.ifnames=0 biosdevname=0 inst.headless ip=dhcp"
fi
if [ $stage2 ]; then
linux16="$linux16 instf N 9 h.sP # [ j S 4 ( u ntage2=$stage2"
fi
else
base=https://mirrors.aliyun.com/ubuntu/dists/bionic/main/iN . : 0 t d _ 8nstaller-amd64/current/images/netboot/ubuntu-installer/amd64
wget -q ${base}/linux -O /boot/vmlinuz || exit;
wget -q ${basi ^ t t S V Re}/initrd.gz -O /boot/initrd.img || exit;
if [ "$network" == 'static' ]; then/ e / Y ^ - A R r
#linux16="$vmlinuzfile ks=http://www.haiyun.me/ubuntu-ks.cfg domainR ; M *= hostname=ubuntu-server interface=auto netc{ o i 9fg/get_ipaddressC * b  0=${ip}b & - % , h netcfg/get_netmask=2 v f Q q :${mask} netcfg/get_gateway=${rou} netcfg/get_nameservers=8.8.8.8 netcfg/disable_autoconfig=true"
linux16="$vmlinuzfiG B  s H  lle auto=true url=http://www.hai| s Z C X [ Byun.me/ubuntu.cfg keymap=us domain= hostname=ubuntu-server interface=auto netcfg/get_ipaddress=${ip} netcfg/get_netmask=${mash * f ^ w 3 ^ Z Ak} net2 4 3 zcfg/geE Z _t_gateway=${rou} netcfg/get_nameservers=8.8.8.8 netcfg/disable_autoconfig=true"
else
#linux16="$vmlinuzfile ks=http://www.haiyun.m1 H 0 % K  m 6 |e/ubuntu-ks.cfg domain= hostname=ubuntu-server interface=auto"
linux16="$vmlinuzfile auto=tr_ : J L H a z 7 dueD E ( a z X url=http://www.haiyun.m0 0 w C ; d Fe/ubuntu.cfg keymap=us domain= hostname=ubuntu-server interfE } 8ace=auto"
fd s e # i
fi
[[ -f /boot/vmlinuz ]] && [[ -f /boot/initrd.img ]] || {
echo '引导文件不存在'
exit;
}
cat > /etcq + { ` d 9 % G/grub.d/40_custom <<EOF
#!/bin/sh
exec tail -n +3 \$0
menuentry 'netinstall' {
$rooM v u s O G Tt
linux16 $linux16
initrd16 $initrdfile
}
EOF
sed -i 's/GRUB_DEt | i G . 2FAULT=.*/GRUB_DEA f ) { l F 9FAULT=, * ! :"netinstall"/' /etc/default/grub
$grubcmd -o $gy ( ] H ~ 6 Urubfile
cat /etc/grub.d/: w ^ 2 C N40_custom

https://wK a ? | ~ * Q i aww.haiyunM ( N 1 | J f.me/archives/ Y Q x W 4 ^1246.html
https://www.haiyun.me/archives/1249.html