环境:ubuntu 16.04 LTS server
ip地址:192.168.159.3  
需要文件:ubuntu-server-16.04.2-LTS.ISO
目的:通过pxe安装方式实现自动安装
步骤:
一、修改文件打开数
主要在 /etc/security/limits.conf中添加如下参数

* soft nofile 1048576

* hard nofile 1048576

root soft nofile 1048576

root hard nofile 1048576

* soft nproc 80480

* hard nproc 80960

root soft nproc 80480

root hard nproc 80960

二、修改安装相应的主机tfpd、dhcp server、http server和dns 服务器
sudo apt-get install -y isc-dhcp-server tftpd-hpa tftp-hpa apache2 bind9 
1、配置dhcp server   文件位置 /etc/dhcp/dhcpd.conf ,内容如下

subnet 192.168.159.0 netmask 255.255.255.0 {

  range 192.168.159.4 192.168.159.100;

  option subnet-mask 255.255.255.0;

  option routers 192.168.159.2;

  option broadcast-address 192.168.159.255;

  default-lease-time 600;

  max-lease-time 7200;

  filename "pxelinux.0";

  next-Server 192.168.159.3;

}

2、配置DNS
/etc/bind/named.conf.local 添加如下

zone "ubuntu.com" {

        type master;

        file "/etc/bind/db.ubuntu";

};

添加新的文件 /etc/bind/db.ubuntu

$TTL    604800

@       IN      SOA     localhost. root.localhost. (

                              1         ; Serial

                         604800         ; Refresh

                          86400         ; Retry

                        2419200         ; Expire

                         604800 )       ; Negative Cache TTL

;

@       IN      NS      localhost.

*.archive.ubuntu.com.   IN      A 192.168.159.3


3.配置http server建议使用apache,原因是只要一个iso文件就全ok了,可以不依赖网络
(原先使用的nginx,总脚本中注销了nginx,如使用nginx的请注释掉apache的并参考总脚本,这里以apache为准)
apache配置,文件位置/etc/apache2/sites-available/000-default.conf,执行下面的脚本

sudo sed -i 's?/var/www/html?/var/lib/tftpboot/Ubuntu/16.04/amd64/?g' /etc/apache2/sites-available/000-default.conf

sudo sed -i 13i"\    " /etc/apache2/sites-available/000-default.conf

sudo sed -i 14i"\        Options Indexes" /etc/apache2/sites-available/000-default.conf

sudo sed -i 15i"\        AllowOverride All" /etc/apache2/sites-available/000-default.conf

sudo sed -i 16i"\        Allow from all" /etc/apache2/sites-available/000-default.conf

sudo sed -i 17i"\        Require all granted" /etc/apache2/sites-available/000-default.conf

sudo sed -i 18i"\     " /etc/apache2/sites-available/000-default.conf

4、配置tftpd

修改配置文件/etc/default/tftpd-hpa,执行如下脚本

sudo bash -c "echo RUN_DAEMON=\"yes\" >> /etc/default/tftpd-hpa"

sudo sed -i 's?/srv/tftp?/var/lib/tftpboot?g' /etc/default/tftpd-hpa

sudo sed -i '2iTFTP_OPTIONS="-s -l"' /etc/default/tftpd-hpa

5、配置pxe相关,下载ubuntu最新的iso,这里以16.04.2为示例,复制相应的文件

sudo mkdir -p /var/lib/tftpboot/pxelinux.cfg

sudo mkdir -p /var/lib/tftpboot/Ubuntu/16.04/amd64/

sudo mount -o loop ubuntu-16.04.2-server-amd64.iso /mnt

sudo cp -av /mnt/* /var/lib/tftpboot/Ubuntu/16.04/amd64/

sudo cp -av /var/lib/tftpboot/Ubuntu/16.04/amd64/install/netboot/ubuntu-installer/amd64/* /var/lib/tftpboot/

sudo cp -f /var/lib/tftpboot/boot-screens/ldlinux.c32 /var/lib/tftpboot/

 添加pxe启动菜单

sudo bash -c 'cat > /var/lib/tftpboot/pxelinux.cfg/default << EOF

# D-I config version 2.0

# search path for the c32 support libraries (libcom32, libutil etc.)

default install

label install

        menu label ^Install Ubuntu Server

        menu default

        kernel linux

        append vga=788 ks=http://192.168.159.3/ks.cfg initrd=initrd.gz  live-installer/net-image=http://192.168.159.3/install/filesystem.squashfs --- quiet              

path boot-screens

include boot-screens/menu.cfg

default boot-screens/vesamenu.c32

prompt 0

timeout 1

EOF'


添加自应答的ks.cfg,并复制到http的根目录下(自应答请参考总脚本,这里就不写了)

6、重启相关服务,开启对应的主机并以pxe启动,没有意外就会自动安装,无需人工干预


总脚本如下:
#!/bin/bash

if [ `grep -vE "#|^$" /etc/security/limits.conf|wc -l` -eq 0 ];then

sudo bash -c 'cat >> /etc/security/limits.conf <

* soft nofile 1048576

* hard nofile 1048576

root soft nofile 1048576

root hard nofile 1048576

* soft nproc 80480

* hard nproc 80960

root soft nproc 80480

root hard nproc 80960

EOF'

fi

sudo mkdir -p /var/lib/tftpboot/pxelinux.cfg

sudo mkdir -p /var/lib/tftpboot/Ubuntu/16.04/amd64/

sudo mount -o loop ubuntu-16.04.2-server-amd64.iso /mnt

sudo cp -av /mnt/* /var/lib/tftpboot/Ubuntu/16.04/amd64/

sudo bash -c " echo 'deb file:///var/lib/tftpboot/Ubuntu/16.04/amd64/ xenial main ' >/etc/apt/sources.list"

sudo apt-get update

#sudo apt-get install -y isc-dhcp-server tftpd-hpa tftp-hpa nginx bind9 --allow-unauthenticated

sudo apt-get install -y isc-dhcp-server tftpd-hpa tftp-hpa apache2 bind9 --allow-unauthenticated

sudo mkdir -p /var/lib/tftpboot/pxelinux.cfg

sudo mkdir -p /var/lib/tftpboot/Ubuntu/16.04/amd64/

sudo cp -av /var/lib/tftpboot/Ubuntu/16.04/amd64/install/netboot/ubuntu-installer/amd64/* /var/lib/tftpboot/

sudo cp -f /var/lib/tftpboot/boot-screens/ldlinux.c32 /var/lib/tftpboot/

##nginx config

#if [ `grep "autoindex on;" /etc/nginx/nginx.conf|wc -l` -eq 0 ];then

#sudo sed -i 16i'autoindex on;' /etc/nginx/nginx.conf

#sudo sed -i "16s/^/\t/g" /etc/nginx/nginx.conf

#fi

#sudo sed -i 36c'root /var/lib/tftpboot/Ubuntu/16.04/amd64;' /etc/nginx/sites-available/default

#sudo sed -i "36s/^/\t/g" /etc/nginx/sites-available/default

#sudo service nginx restart

#apache2 config

if [ `grep "" /etc/apache2/sites-available/000-default.conf|wc -l` -eq 0 ];then

sudo sed -i 's?/var/www/html?/var/lib/tftpboot/Ubuntu/16.04/amd64/?g' /etc/apache2/sites-available/000-default.conf

sudo sed -i 13i"\    " /etc/apache2/sites-available/000-default.conf

sudo sed -i 14i"\        Options Indexes" /etc/apache2/sites-available/000-default.conf

sudo sed -i 15i"\        AllowOverride All" /etc/apache2/sites-available/000-default.conf

sudo sed -i 16i"\        Allow from all" /etc/apache2/sites-available/000-default.conf

sudo sed -i 17i"\        Require all granted" /etc/apache2/sites-available/000-default.conf

sudo sed -i 18i"\     " /etc/apache2/sites-available/000-default.conf

fi

sudo systemctl restart apache2.service

#dhcp config

if [ `grep "pxelinux.0" /etc/dhcp/dhcpd.conf|wc -l` -eq 0 ];then

sudo bash -c ' cat >> /etc/dhcp/dhcpd.conf <

subnet 192.168.159.0 netmask 255.255.255.0 {

  range 192.168.159.4 192.168.159.100;

  option subnet-mask 255.255.255.0;

  option routers 192.168.159.2;

  option broadcast-address 192.168.159.255;

  default-lease-time 600;

  max-lease-time 7200;

  filename "pxelinux.0";

  next-Server 192.168.159.3;

}

EOF'

sudo sed -i 's?ns1.example.org, ns2.example.org?192.168.159.3?g' /etc/dhcp/dhcpd.conf

sudo sed -i '16s?example.org?ubuntu.com?g' /etc/dhcp/dhcpd.conf 

fi

sudo systemctl restart isc-dhcp-server.service

#dns config

if [ `grep "ubuntu.com" /etc/bind/named.conf.local|wc -l` -eq 0 ];then

sudo bash -c ' cat >> /etc/bind/named.conf.local <

zone "ubuntu.com" {

        type master;

        file "/etc/bind/db.ubuntu";

};

EOF'

sudo bash -c ' cat >> /etc/bind/db.ubuntu <

$TTL    604800

@       IN      SOA     localhost. root.localhost. (

                              1         ; Serial

                         604800         ; Refresh

                          86400         ; Retry

                        2419200         ; Expire

                         604800 )       ; Negative Cache TTL

;

@       IN      NS      localhost.

*.archive.ubuntu.com.   IN      A 192.168.159.3

EOF'

fi

sudo systemctl restart bind9.service 

#pxe menu

sudo bash -c 'cat > /var/lib/tftpboot/pxelinux.cfg/default << EOF

# D-I config version 2.0

# search path for the c32 support libraries (libcom32, libutil etc.)

default install

label install

        menu label ^Install Ubuntu Server

        menu default

        kernel linux

        append vga=788 ks=http://192.168.159.3/ks.cfg initrd=initrd.gz  live-installer/net-image=http://192.168.159.3/install/filesystem.squashfs --- quiet              

path boot-screens

include boot-screens/menu.cfg

default boot-screens/vesamenu.c32

prompt 0

timeout 1

EOF'

#tftpd config

if [ `grep RUN_DAEMON /etc/default/tftpd-hpa|wc -l` -eq 0 ];then

sudo bash -c "echo RUN_DAEMON=\"yes\" >> /etc/default/tftpd-hpa"

sudo sed -i 's?/srv/tftp?/var/lib/tftpboot?g' /etc/default/tftpd-hpa

sudo sed -i '2iTFTP_OPTIONS="-s -l"' /etc/default/tftpd-hpa

fi

sudo systemctl restart tftpd-hpa.service

#ks.cfg

sudo bash -c 'cat > /var/lib/tftpboot/Ubuntu/16.04/amd64/ks.cfg <

#Generated by Kickstart Configurator

#platform=AMD64 or Intel EM64T

#System language

lang en_US

#Language modules to install

langsupport en_US

#System keyboard

keyboard us

#System mouse

mouse

#System timezone

timezone Asia/Shanghai

#Root password

rootpw --disabled

#Initial user ;password 1234sdn 

user sdn --fullname "sdn" --iscrypted --password --iscrypted --password $1$XqeCxFK2$pL1zPjo2TaArGGJGAIUDK.

#Reboot after installation

reboot

#Use text mode install

text

#Install OS instead of upgrade

install

#Use Web installation

url --url http://192.168.159.3/

#System bootloader configuration

bootloader --location=mbr

#Clear the Master Boot Record

zerombr yes

#Partition clearing information

clearpart --all --initlabel

#Disk partitioning information

part /boot --fstype ext2 --size 500

part pv.01 --size=1 --grow 

volgroup sdn_vg pv.01

logvol  swap  --vgname=sdn_vg --name=lv_swap --size=2048

logvol  /  --vgname=sdn_vg --fstype ext4 --size=1 --grow --name=lv_root

#System authorization infomation

auth  --useshadow  --enablemd5 

#Network information

network --bootproto=dhcp --nameserver=192.168.159.3 --noipv6 --activate --device=eth0 --onboot=on

#Firewall configuration

firewall --disabled 

# packages/groups

%packages

openssh-server

EOF'

sudo chmod -R 777 /var/lib/tftpboot