Debian網絡安裝/重裝一鍵腳本

  • 背景:
    適用于由GRUB引導的CentOS,Ubuntu,Debian系統.
    定制自己的環境去掉預裝軟件.
    只要有root權限,還您一個純凈的系統。

    注意:
    全自動安裝默認root密碼:Vicer,安裝完成后請立即更改密碼.
    請使用 passwd root 命令更改密碼.
    特別注意:OpenVZ構架不適用.

    更新:
    [2017.06.25]
    適配了由較老GRUB版本引導的CentOS6等系統.
    去除-cn參數,此參數作用不大.
    添加-apt/--mirror參數,用于指定源(需完整的鏡像源地址).
    用法示例:
    --mirror 'http://ftp.riken.jp/Linux/debian/debian/'
    --mirror 'http://mirrors.ustc.edu.cn/debian/'

    [2017.06.24]
    由公網探測IP地址,改為本機探測IP地址.適配更加廣泛.

    [2017.06.23]
    修復Debian9不能使用root登陸的問題.
    移除對于route命令的依賴,使用ip命令并計算子網掩碼.
    修復使用ls命令時的一個錯誤警告.
    增加-cn參數,使國內機器下載所需資源更加迅速.

    [2017.06.20]
    增加對Debian9的支持,支持全自動化安裝.
    未做大量測試,有問題請反饋.

    [2017.06.09]
    添加支持從CentOS7運行全自動化安裝Debian.
    理論上支持由grub2引導的系統(CentOS6由grub引導,故不支持.).
    優化判斷邏輯,刪除 -t參數.
    添加-a參數(全自動化安裝)和-m參數(從VNC模式安裝)

    [2017.06.05]
    修復全自動安裝Debian8會出現卡住和不能使用root密碼登陸的問題.

    [2017.06.04]
    增加全自動方式安裝,實現在無救援模式,無VNC的情況下安裝Debian.
    已在AWS Lightsail(Ubuntu),DigitalOcean,UltraVPS.eu通過測試.
    默認root密碼:Vicer,安裝完成后請立即更改密碼.
    使用 passwd root 命令更改密碼.

    [2017.03.28]
    增加了一個之參數選項;
    此參數用于手動指定機器的虛擬化類型。
    一般情況下不需要指定此參數。

    [2017.03.25]
    修復了一些已知問題。

    需要:
    1.Debian/Ubuntu/CentOS 系統(由GRUB引導);
    2.wget 用來下載文件,獲取公網IP;
    3.ip 獲取網關,掩碼等;
    4.sed awk grep 處理文本流;
    5.VNC 安裝系統(此項為可選)。

    確保安裝了所需軟件:
    1
    2
    3
    4
    5
    #Debian/Ubuntu:
    apt-get install -y gawk sed grep
     
    #RedHat/CentOS:
    yum install -y gawk sed grep
    如果出現了錯誤,請運行:
    1
    2
    3
    4
    5
    #Debian/Ubuntu:
    apt-get update
     
    #RedHat/CentOS:
    yum update
    一鍵下載及使用:
    1
    wget --no-check-certificate -qO DebianNET.sh 'https://moeclub.org/attachment/LinuxShell/DebianNET.sh' && chmod -x DebianNET.sh
    1
    2
    3
    Usage:
            bash DebianNET.sh       -d/--debian [7/wheezy|8/jessie|9/stretch]
                                    -v/--ver [32/i386|64/amd64]
    安裝示例:
    全自動安裝:
    1
    bash DebianNET.sh -d wheezy -v i386 -a
    VNC手動安裝:
    1
    bash DebianNET.sh -d wheezy -v i386 -m
    使用:
    【默認】安裝Debian 7 x32:
    1
    bash DebianNET.sh -d wheezy -v i386
    -或者

    1
    bash DebianNET.sh -d 7 -v 32
    安裝Debian 7 x64:
    1
    bash DebianNET.sh -d wheezy -v amd64
    -或者

    1
    bash DebianNET.sh -d 7 -v 64
    安裝Debian 8 x32:
    1
    bash DebianNET.sh -d jessie -v i386
    -或者

    1
    bash DebianNET.sh -d 8 -v 32
    安裝Debian 8 x64:
    1
    bash DebianNET.sh -d jessie -v amd64
    -或者

    1
    bash DebianNET.sh -d 8 -v 64
    【默認】預覽:
    InstallOS

InstallOS



#!/bin/bash
 
while [[ $# -ge 1 ]]; do
  case $1 in
    -v|--ver)
      shift
      VERtmp="$1"
      shift
      ;;
    -d|--debian)
      shift
      vDEBtmp="$1"
      shift
      ;;
    -p|--password)
      shift
      WDtmp="$1"
      shift
      ;;
    -a|--auto)
      shift
      INStmp='auto'
      ;;
    -m|--manual)
      shift
      INStmp='manual'
      ;;
    -apt|--mirror)
      shift
      isMirror='1'
      tmpMirror="$1"
      shift
      ;;
    *)
      echo -ne " Usage:\n\tbash $0\t-d/--debian [7/\033[33m\033[04mwheezy\033[0m|8/jessie|9/stretch]\n\t\t\t\t-v/--ver [32/\033[33m\033[04mi386\033[0m|64/amd64]\n\t\t\t\t-apt/--mirror\n\t\t\t\t-a/--auto\n\t\t\t\t-m/--manual\n"
      exit 1;
      ;;
    esac
  done
 
[ $EUID -ne 0 ] && echo "Error:This script must be run as root!" && exit 1
[ -f /boot/grub/grub.cfg ] && GRUBOLD='0' && GRUBDIR='/boot/grub' && GRUBFILE='grub.cfg'
[ -z $GRUBDIR ] && [ -f /boot/grub2/grub.cfg ] && GRUBOLD='0' && GRUBDIR='/boot/grub2' && GRUBFILE='grub.cfg'
[ -z $GRUBDIR ] && [ -f /boot/grub/grub.conf ] && GRUBOLD='1' && GRUBDIR='/boot/grub' && GRUBFILE='grub.conf'
[ -z $GRUBDIR -o -z $GRUBFILE ] && echo "Error! Not Found grub path." && exit 1
 
[ -n $vDEBtmp ] && {
[ "$vDEBtmp" == '7' -o "$vDEBtmp" == 'wheezy' ] && vDEB='wheezy';
[ "$vDEBtmp" == '8' -o "$vDEBtmp" == 'jessie' ] && vDEB='jessie';
[ "$vDEBtmp" == '9' -o "$vDEBtmp" == 'stretch' ] && vDEB='stretch';
}
[ -n $vDEBtmp ] && {
[ "$VERtmp" == '32' -o "$VERtmp" == 'i386' ] && VER='i386';
[ "$VERtmp" == '64' -o "$VERtmp" == 'amd64' ] && VER='amd64';
}
 
[ -n $isMirror ] && [ "$isMirror" == '1' ] && [ -n $tmpMirror ] && {
tmpDebianMirror="$(echo -n "$tmpMirror" |grep -Eo '.*\.(\w+)')"
echo -n "$tmpDebianMirror" |grep -q '://'
[ $? -eq '0' ] && {
DebianMirror="$(echo -n "$tmpDebianMirror" |awk -F'://' '{print $2}')"
} || {
DebianMirror="$(echo -n "$tmpDebianMirror")"
}
} || {
DebianMirror='httpredir.debian.org'
}
[ -z $DebianMirrorDirectory ] && [ -n $DebianMirror ] && [ -n $tmpMirror ] && {
DebianMirrorDirectory="$(echo -n "$tmpMirror" |awk -F''${DebianMirror}'' '{print $2}' |sed 's/\/$//g')"
}
[ "$DebianMirrorDirectory" == '/' ] && [ -n $DebianMirror ] && DebianMirrorDirectory='/debian'
[ -z $DebianMirrorDirectory ] && [ -n $DebianMirror ] && DebianMirrorDirectory='/debian'
 
[ -n $INStmp ] && {
[ "$INStmp" == 'auto' ] && inVNC='n'
[ "$INStmp" == 'manual' ] && inVNC='y'
}
[ -n $WDtmp ] && myPASSWORD="$WDtmp"
 
[ -z $vDEB ] && vDEB='wheezy';
[ -z $VER ] && VER='i386';
[ -z $myPASSWORD ] && myPASSWORD='Vicer'
 
clear && echo -e "\n\033[36m# Install\033[0m\n"
 
[ -z $inVNC ] && ASKVNC(){
inVNC='y';
echo -ne "\033[34mCan you login VNC?\033[0m\e[33m[\e[32my\e[33m/n]\e[0m "
read inVNCtmp
[[ -n "$inVNCtmp" ]] && inVNC=$inVNCtmp
[ "$inVNC" == 'y' -o "$inVNC" == 'Y' ] && inVNC='y'
[ "$inVNC" == 'n' -o "$inVNC" == 'N' ] && inVNC='n'
}
 
[ "$inVNC" == 'y' -o "$inVNC" == 'n' ] || ASKVNC;
 
[ "$inVNC" == 'y' ] && echo -e "\033[34mManual Mode\033[0m insatll \033[33mDebian\033[0m [\033[33m$vDEB\033[0m] [\033[33m$VER\033[0m] in VNC. "
[ "$inVNC" == 'n' ] && echo -e "\033[34mAuto Mode\033[0m insatll \033[33mDebian\033[0m [\033[33m$vDEB\033[0m] [\033[33m$VER\033[0m]. "
 
echo -e "\n[\033[33m$vDEB\033[0m] [\033[33m$VER\033[0m] Downloading..."
[ -z $DebianMirror ] && echo -ne "\033[31mError! \033[0mGet debian mirror fail! \n" && exit 1
[ -z $DebianMirrorDirectory ] && echo -ne "\033[31mError! \033[0mGet debian mirror directory fail! \n" && exit 1
wget --no-check-certificate -qO '/boot/initrd.gz' "http://$DebianMirror$DebianMirrorDirectory/dists/$vDEB/main/installer-$VER/current/images/netboot/debian-installer/$VER/initrd.gz"
[ $? -ne '0' ] && echo -ne "\033[31mError! \033[0mDownload 'initrd.gz' failed! \n" && exit 1
wget --no-check-certificate -qO '/boot/linux' "http://$DebianMirror$DebianMirrorDirectory/dists/$vDEB/main/installer-$VER/current/images/netboot/debian-installer/$VER/linux"
[ $? -ne '0' ] && echo -ne "\033[31mError! \033[0mDownload 'linux' failed! \n" && exit 1
 
IPSUB="$(ip addr |grep 'global' |grep 'brd' |head -n1 |grep -o '[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}/[0-9]\{1,2\}')"
IPv4="$(echo -n "$IPSUB" |cut -d'/' -f1)"
NETSUB="$(echo -n "$IPSUB" |grep -o '/[0-9]\{1,2\}')"
GATE="$(ip route show |grep -o 'default via [0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}' |head -n1 |grep -o '[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}')"
[ -n "$NETSUB" ] && MASK="$(echo -n '128.0.0.0/1,192.0.0.0/2,224.0.0.0/3,240.0.0.0/4,248.0.0.0/5,252.0.0.0/6,254.0.0.0/7,255.0.0.0/8,255.128.0.0/9,255.192.0.0/10,255.224.0.0/11,255.240.0.0/12,255.248.0.0/13,255.252.0.0/14,255.254.0.0/15,255.255.0.0/16,255.255.128.0/17,255.255.192.0/18,255.255.224.0/19,255.255.240.0/20,255.255.248.0/21,255.255.252.0/22,255.255.254.0/23,255.255.255.0/24,255.255.255.128/25,255.255.255.192/26,255.255.255.224/27,255.255.255.240/28,255.255.255.248/29,255.255.255.252/30,255.255.255.254/31,255.255.255.255/32' |grep -o '[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}.[0-9]\{1,3\}'${NETSUB}'' |cut -d'/' -f1)"
 
[ -n "$GATE" ] && [ -n "$MASK" ] && [ -n "$IPv4" ] || {
echo "Error! Not configure network. "
exit 1
}
[ -f /etc/network/interfaces ] && {
[[ -z "$(sed -n '/iface.*inet static/p' /etc/network/interfaces)" ]] && AutoNet='1' || AutoNet='0'
[ -d /etc/network/interfaces.d ] && {
ICFGN="$(find /etc/network/interfaces.d -name '*.cfg' |wc -l)" || ICFGN='0'
[ "$ICFGN" -ne '0' ] && {
for NetCFG in `ls -1 /etc/network/interfaces.d/*.cfg`
 do 
  [[ -z "$(cat $NetCFG | sed -n '/iface.*inet static/p')" ]] && AutoNet='1' || AutoNet='0'
  [ "$AutoNet" -eq '0' ] && break
done
}
}
}
[ -d /etc/sysconfig/network-scripts ] && {
ICFGN="$(find /etc/sysconfig/network-scripts -name 'ifcfg-*' |grep -v 'lo'|wc -l)" || ICFGN='0'
[ "$ICFGN" -ne '0' ] && {
for NetCFG in `ls -1 /etc/sysconfig/network-scripts/ifcfg-* |grep -v 'lo$' |grep -v ':[0-9]\{1,\}'`
 do 
  [[ -n "$(cat $NetCFG | sed -n '/BOOTPROTO.*[dD][hH][cC][pP]/p')" ]] && AutoNet='1' || {
  AutoNet='0' && . $NetCFG
  [ -n $NETMASK ] && MASK="$NETMASK"
  [ -n $GATEWAY ] && GATE="$GATEWAY"
}
  [ "$AutoNet" -eq '0' ] && break
done
}
}
 
[ ! -f $GRUBDIR/$GRUBFILE ] && echo "Error! Not Found $GRUBFILE. " && exit 1
 
[ ! -f $GRUBDIR/$GRUBFILE.old ] && [ -f $GRUBDIR/$GRUBFILE.bak ] && mv -f $GRUBDIR/$GRUBFILE.bak $GRUBDIR/$GRUBFILE.old
mv -f $GRUBDIR/$GRUBFILE $GRUBDIR/$GRUBFILE.bak
[ -f $GRUBDIR/$GRUBFILE.old ] && cat $GRUBDIR/$GRUBFILE.old >$GRUBDIR/$GRUBFILE || cat $GRUBDIR/$GRUBFILE.bak >$GRUBDIR/$GRUBFILE
 
[ "$GRUBOLD" == '0' ] && {
CFG0="$(awk '/menuentry /{print NR}' $GRUBDIR/$GRUBFILE|head -n 1)"
CFG2="$(awk '/menuentry /{print NR}' $GRUBDIR/$GRUBFILE|head -n 2 |tail -n 1)"
CFG1=""
for CFGtmp in `awk '/}/{print NR}' $GRUBDIR/$GRUBFILE`
 do
  [ $CFGtmp -gt "$CFG0" -a $CFGtmp -lt "$CFG2" ] && CFG1="$CFGtmp";
 done
[ -z "$CFG1" ] && {
echo "Error! read $GRUBFILE. "
exit 1
}
sed -n "$CFG0,$CFG1"p $GRUBDIR/$GRUBFILE >/tmp/grub.new
[ -f /tmp/grub.new ] && [ "$(grep -c '{' /tmp/grub.new)" -eq "$(grep -c '}' /tmp/grub.new)" ] || {
echo -ne "\033[31mError! \033[0mNot configure $GRUBFILE. \n"
exit 1
}
 
sed -i "/menuentry.*/c\menuentry\ \'Install OS \[$vDEB\ $VER\]\'\ --class debian\ --class\ gnu-linux\ --class\ gnu\ --class\ os\ \{" /tmp/grub.new
[ "$(grep -c '{' /tmp/grub.new)" -eq "$(grep -c '}' /tmp/grub.new)" ] || {
echo "Error! configure append $GRUBFILE. "
exit 1
}
sed -i "/echo.*Loading/d" /tmp/grub.new
}
 
[ "$GRUBOLD" == '1' ] && {
CFG0="$(awk '/title /{print NR}' $GRUBDIR/$GRUBFILE|head -n 1)"
CFG1="$(awk '/title /{print NR}' $GRUBDIR/$GRUBFILE|head -n 2 |tail -n 1)"
[ -n $CFG0 ] && [ -z $CFG1 -o $CFG1 == $CFG0 ] && sed -n "$CFG0,$"p $GRUBDIR/$GRUBFILE >/tmp/grub.new
[ -n $CFG0 ] && [ -z $CFG1 -o $CFG1 != $CFG0 ] && sed -n "$CFG0,$CFG1"p $GRUBDIR/$GRUBFILE >/tmp/grub.new
[ ! -f /tmp/grub.new ] && echo "Error! configure append $GRUBFILE. " && exit 1
sed -i "/title.*/c\title\ \'Install OS \[$vDEB\ $VER\]\'" /tmp/grub.new
sed -i '/^#/d' /tmp/grub.new
}
 
[ -n "$(grep 'initrd.*/' /tmp/grub.new |awk '{print $2}' |tail -n 1 |grep '^/boot/')" ] && Type='InBoot' || Type='NoBoot'
 
LinuxKernel="$(grep 'linux.*/' /tmp/grub.new |awk '{print $1}' |head -n 1)"
[ -z $LinuxKernel ] && LinuxKernel="$(grep 'kernel.*/' /tmp/grub.new |awk '{print $1}' |head -n 1)"
LinuxIMG="$(grep 'initrd.*/' /tmp/grub.new |awk '{print $1}' |tail -n 1)"
 
[ "$Type" == 'InBoot' ] && {
sed -i "/$LinuxKernel.*\//c\\\t$LinuxKernel\\t\/boot\/linux auto=true hostname=debian domain= -- quiet" /tmp/grub.new
sed -i "/$LinuxIMG.*\//c\\\t$LinuxIMG\\t\/boot\/initrd.gz" /tmp/grub.new
}
 
[ "$Type" == 'NoBoot' ] && {
sed -i "/$LinuxKernel.*\//c\\\t$LinuxKernel\\t\/linux auto=true hostname=debian domain= -- quiet" /tmp/grub.new
sed -i "/$LinuxIMG.*\//c\\\t$LinuxIMG\\t\/initrd.gz" /tmp/grub.new
}
 
sed -i '$a\\n' /tmp/grub.new
 
[ "$inVNC" == 'n' ] && {
[ -f /etc/network/interfaces -o -d /etc/sysconfig/network-scripts ] && {
sed -i ''${CFG0}'i\\n' $GRUBDIR/$GRUBFILE
sed -i ''${CFG0}'r /tmp/grub.new' $GRUBDIR/$GRUBFILE
[ -z $AutoNet ] && echo "Error, Not found interfaces config." && exit 1
[ -f  $GRUBDIR/grubenv ] && sed -i 's/saved_entry/#saved_entry/g' $GRUBDIR/grubenv
[ -d /boot/tmp ] && rm -rf /boot/tmp
mkdir -p /boot/tmp/
cd /boot/tmp/
gzip -d < ../initrd.gz | cpio --extract --verbose --make-directories --no-absolute-filenames >>/dev/null 2>&1
cat >/boot/tmp/preseed.cfg<<EOF
d-i debian-installer/locale string en_US
d-i console-setup/layoutcode string us
 
d-i keyboard-configuration/xkb-keymap string us
 
d-i netcfg/choose_interface select auto
 
d-i netcfg/disable_autoconfig boolean true
d-i netcfg/dhcp_failed note
d-i netcfg/dhcp_options select Configure network manually
d-i netcfg/get_ipaddress string $IPv4
d-i netcfg/get_netmask string $MASK
d-i netcfg/get_gateway string $GATE
d-i netcfg/get_nameservers string 8.8.8.8
d-i netcfg/confirm_static boolean true
 
d-i mirror/country string manual
d-i mirror/http/hostname string $DebianMirror
d-i mirror/http/directory string $DebianMirrorDirectory
d-i mirror/http/proxy string
 
d-i passwd/root-login boolean ture
d-i passwd/make-user boolean false
d-i passwd/root-password password $myPASSWORD
d-i passwd/root-password-again password $myPASSWORD
 
d-i clock-setup/utc boolean true
d-i time/zone string US/Eastern
d-i clock-setup/ntp boolean true
 
d-i partman/early_command string \
debconf-set partman-auto/disk "\$(list-devices disk |head -n1)"
d-i partman/mount_style select uuid
d-i partman-auto/init_automatically_partition select Guided - use entire disk
d-i partman-auto/method string regular
d-i partman-auto/choose_recipe select atomic
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
 
d-i debian-installer/allow_unauthenticated boolean true
 
tasksel tasksel/first multiselect minimal
d-i pkgsel/include string openssh-server
d-i pkgsel/upgrade select none
 
popularity-contest popularity-contest/participate boolean false
 
d-i grub-installer/only_debian boolean true
d-i grub-installer/bootdev string default
d-i finish-install/reboot_in_progress note
d-i debian-installer/exit/reboot boolean true
d-i preseed/late_command string \
sed -i 's/^.*PermitRootLogin.*/PermitRootLogin yes/g' /target/etc/ssh/sshd_config; \
sed -i 's/^.*PasswordAuthentication.*/PasswordAuthentication yes/g' /target/etc/ssh/sshd_config;
EOF
[ "$AutoNet" -eq '1' ] && {
sed -i '/netcfg\/disable_autoconfig/d' /boot/tmp/preseed.cfg
sed -i '/netcfg\/dhcp_options/d' /boot/tmp/preseed.cfg
sed -i '/netcfg\/get_.*/d' /boot/tmp/preseed.cfg
sed -i '/netcfg\/confirm_static/d' /boot/tmp/preseed.cfg
}
rm -rf ../initrd.gz
find . | cpio -H newc --create --verbose | gzip -9 > ../initrd.gz
rm -rf /boot/tmp
}
}
 
[ "$inVNC" == 'y' ] && {
sed -i '$i\\n' $GRUBDIR/$GRUBFILE
sed -i '$r /tmp/grub.new' $GRUBDIR/$GRUBFILE
echo -e "\n\033[33m\033[04mIt will reboot! \nPlease look at VNC! \nSelect\033[0m\033[32m Install OS [$vDEB $VER] \033[33m\033[4mto install system.\033[04m\n\n\033[31m\033[04mThere is some information for you.\nDO NOT CLOSE THE WINDOW! \033[0m\n"
echo -e "\033[35mIPv4\t\tNETMASK\t\tGATEWAY\033[0m"
echo -e "\033[36m\033[04m$IPv4\033[0m\t\033[36m\033[04m$MASK\033[0m\t\033[36m\033[04m$GATE\033[0m\n\n"
 
read -n 1 -p "Press Enter to reboot..." INP
if [ "$INP" != '' ] ; then
echo -ne '\b \n'
echo "";
fi
}
 
chown root:root $GRUBDIR/$GRUBFILE
chmod 444 $GRUBDIR/$GRUBFILE
 
sleep 3 && reboot >/dev/null 2>&1



本博客所有文章如無特別注明均為原創。作者:櫻木復制或轉載請以超鏈接形式注明轉自 EFE
原文地址《Debian網絡安裝/重裝一鍵腳本
分享到:更多

相關推薦

發表評論

路人甲 表情
Ctrl+Enter快速提交

網友評論(0)