关于Ubuntu pxe启动及自动化安装请查看Ubuntu16.04桌面版pxe启动实现自动安装;
本博客只介绍方法及大致步骤,提供思路,实际操作需要一定的基础;不明白的可以google及百度;
远程安装完成后往往需要配置为静态IP,以方便ssh登录等其他操作;Ubuntu 16.04 desktop版本通过修改preseed.cfg尝试无法配置为静态IP,故通过修改安装镜像实现;大致步骤如下:
1、挂载ISO镜像;
2、修改casper/initrd.lz;
- mkdir temp # 创建存储内容的临时文件
- cp initrd.lz temp/initrd.lzma # 改名initrd为lzma后缀
- cd temp
- lzma -d initrd.lzma
- cpio -id < initrd # 解压出文件
- 修改temp/scripts/casper-bottom/23networking文件(内容见后)
- cd temp#
- find . |cpio --quiet --dereference -o -H newc>../initrd
- cd ..
- lzma -7 initrd (等待一会儿……)
- mv initrd.lzma initrd.lz
#! /bin/sh
PREREQ=""
DESCRIPTION="Preconfiguring networking..."
IFFILE="/root/etc/network/interfaces"
prereqs()
{
echo "$PREREQ"
}
case $1 in
# get pre-requisites
prereqs)
prereqs
exit 0
;;
esac
. /scripts/casper-functions
log_begin_msg "$DESCRIPTION"
if [ "${STATICIP}" = "frommedia" -a -e "$IFFILE" ] ; then
# will use existent /etc/network/interfaces
log_end_msg
exit 0
fi
cat > "$IFFILE" <<EOF
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
EOF
udevadm trigger
udevadm settle
if [ -z "${NETBOOT}" -a -n "${STATICIP}" -a "${STATICIP}" != "frommedia" ]; then
parsed=$(echo "${STATICIP}" | sed -e 's/:/ /g')
for ifline in ${parsed}; do
ifname="$(echo ${ifline} | cut -f1 -d ',')"
ifaddress="$(echo ${ifline} | cut -f2 -d ',')"
ifnetmask="$(echo ${ifline} | cut -f3 -d ',')"
ifgateway="$(echo ${ifline} | cut -f4 -d ',')"
cat >> "$IFFILE" <<EOF
auto ${ifname}
iface ${ifname} inet static
address ${ifaddress}
netmask ${ifnetmask}
gateway ${ifgateway}
EOF
done
else
if [ -z "${NETBOOT}" ]; then
# default, dhcp assigned
method="dhcp"
else
# make sure that the preconfigured interface would not get reassigned by dhcp
# on startup by ifup script - otherwise our root fs might be disconnected!
method="manual"
fi
# iterate the physical interfaces and add them to the interfaces list
if [ "$method" != dhcp ] || [ ! -x /root/usr/sbin/NetworkManager ]; then
for device in /sys/class/net/*/device; do
interface="${device%*/device}"
[ -e $interface ] || continue
i="$(basename $interface)"
cat >> "$IFFILE" <<EOF
auto $i
iface $i inet static
#下面为添加部分******
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameserver 114.114.114.114
#******************
EOF
done
fi
if [ ! -f /root/etc/resolv.conf ] || [ -z "$(cat /root/etc/resolv.conf)" ]; then
if [ -n "${DEVICE}" ] && [ -e /run/net-"${DEVICE}".conf ]; then
# create a resolv.conf if it is not present
#ipconfig quotes DNSDOMAIN, quotes need to be removed for a correct resolv.conf
rc_search="$(sed -n "s/^DNSDOMAIN='\(.*\)'/\1/p" /run/net-"${DEVICE}".conf)"
#search might contain multiple entries but domain should only have one.
rc_domain="$(sed -n "s/^DNSDOMAIN='\([^ ]*\).*'/\1/p" /run/net-"${DEVICE}".conf)"
rc_server0="$(sed -n "s/^IPV4DNS0='\(.*\)'/\1/p" /run/net-"${DEVICE}".conf)"
rc_server1="$(sed -n "s/^IPV4DNS1='\(.*\)'/\1/p" /run/net-"${DEVICE}".conf)"
# Deal with resolvconf
# Writing to /run instead or /root/run as /sbin/init will move /run
# to /root/run a bit later on
if [ -x /root/sbin/resolvconf ] && [ -L /root/etc/resolv.conf ]; then
mkdir -p /run/resolvconf/interface/
resolv=/run/resolvconf/interface/casper
else
resolv=/root/etc/resolv.conf
fi
cat > $resolv <<EOF
# /etc/resolv.conf
# Autogenerated by casper
EOF
[ -n "$rc_search" ] && echo "search ${rc_search}" >> $resolv
[ -n "$rc_domain" ] && echo "domain ${rc_domain}" >> $resolv
[ -n "$rc_server0" ] && [ "$rc_server0" != "0.0.0.0" ] && echo "nameserver $rc_server0" >> $resolv
[ -n "$rc_server1" ] && [ "$rc_server1" != "0.0.0.0" ] && echo "nameserver $rc_server1" >> $resolv
cat $resolv >> /root/var/log/netboot.config
fi
fi
fi
if [ ! -x /root/usr/sbin/NetworkManager ]; then
for device in /sys/class/net/*/device; do
name=$(basename ${device%*/device})
grep -q "iface $name" $IFFILE && continue
cat >> "$IFFILE" <<EOF
auto $name
iface $name inet static
EOF
done
fi
log_end_msg
3、重新制作ISO,这个步骤请自行百度解决;也可通过windows下UltraISO软件进行制作;
4、由于Ubuntu 16.04 desktop版本没有集成openssh-server,若需要此功能需要重新制作文件系统及ISO;
参照wiki及官方文档很麻烦,而且很容易出错,此处可以借用工具Cubic进行添加,工具的使用方法请百度。
Cubic的安装方法:
sudo apt-add-repository ppa:cubic-wizard/release
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6494C6D6997C215E
sudo apt update
sudo apt install cubic
使用方法可以参照: