Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to install a PXE boot server for diskless clients
# This installs a PXE boot server.
#
# It's based on https://help.ubuntu.com/community/DisklessUbuntuHowto.
# It's been used with ubuntu-10.10-server-amd64.iso and ubuntu-10.10-server-i386.iso.
#
# It requires two network cards. One for access to the outside world and one
# for a private network of PXE clients. I've choosen this setup to not cause problems
# with DHCP on the normal network.
#
# It also requires that you have a second partition mounted on /nfsroot.
CLUSTER_NET_IP=10.1.1.1
CLUSTER_NET_BROADCAST=10.1.1.255
CLUSTER_NET_NETMASK=255.255.255.0
CLUSTER_NET_SUBNET=10.1.1.0
CLUSTER_NET_IP_FROM=10.1.1.10
CLUSTER_NET_IP_TO=10.1.1.254
CLUSTER_NET_DNS=8.8.8.8
CLUSTER_NET_NFS_ACCESS=10.1.1.*
KERNEL=`uname -r`
echo "#### Installing and configuring client system ####"
apt-get install nfs-common -y
cp -ax /. /nfsroot/.
cp -ax /dev/. /nfsroot/dev/.
sed 's/MODULES=most/MODULES=netboot/g' /nfsroot/etc/initramfs-tools/initramfs.conf > /tmp/a && mv /tmp/a /nfsroot/etc/initramfs-tools/initramfs.conf
sed 's/BOOT=local/BOOT=nfs/g' /nfsroot/etc/initramfs-tools/initramfs.conf > /tmp/a && mv /tmp/a /nfsroot/etc/initramfs-tools/initramfs.conf
chroot /nfsroot mkinitramfs -o /tmp/initrd.img-$KERNEL
echo "auto lo" > /nfsroot/etc/network/interfaces
echo "iface lo inet loopback" >> /nfsroot/etc/network/interfaces
echo "iface eth0 inet manual" >> /nfsroot/etc/network/interfaces
echo "proc /proc proc defaults 0 0" > /nfsroot/etc/fstab
echo "/dev/nfs / nfs defaults 1 1" >> /nfsroot/etc/fstab
echo "none /tmp tmpfs defaults 0 0" >> /nfsroot/etc/fstab
echo "none /var/run tmpfs defaults 0 0" >> /nfsroot/etc/fstab
echo "none /var/lock tmpfs defaults 0 0" >> /nfsroot/etc/fstab
echo "none /var/tmp tmpfs defaults 0 0" >> /nfsroot/etc/fstab
echo "#### Installing and configuring server ####"
apt-get install dhcp3-server tftpd-hpa syslinux nfs-kernel-server initramfs-tools -y
mkdir -p /tftpboot/pxelinux.cfg
echo "auto eth1" >> /etc/network/interfaces
echo "iface eth1 inet static" >> /etc/network/interfaces
echo " address $CLUSTER_NET_IP" >> /etc/network/interfaces
echo " netmask $CLUSTER_NET_NETMASK" >> /etc/network/interfaces
echo "iptables --flush" > /etc/rc.local
echo "iptables --table nat --flush" >> /etc/rc.local
echo "iptables --delete-chain" >> /etc/rc.local
echo "iptables --table nat --delete-chain" >> /etc/rc.local
echo "iptables --table nat --append POSTROUTING --out-interface eth0 -j MASQUERADE" >> /etc/rc.local
echo "iptables --append FORWARD --in-interface eth1 -s $CLUSTER_NET_IP -j ACCEPT" >> /etc/rc.local
echo "echo 1 > /proc/sys/net/ipv4/ip_forward" >> /etc/rc.local
echo "allow booting;" >> /etc/dhcp3/dhcpd.conf
echo "allow bootp;" >> /etc/dhcp3/dhcpd.conf
echo "subnet $CLUSTER_NET_SUBNET netmask $CLUSTER_NET_NETMASK {" >> /etc/dhcp3/dhcpd.conf
echo " range $CLUSTER_NET_IP_FROM $CLUSTER_NET_IP_TO;" >> /etc/dhcp3/dhcpd.conf
echo " option broadcast-address $CLUSTER_NET_BROADCAST;" >> /etc/dhcp3/dhcpd.conf
echo " option routers $CLUSTER_NET_IP;" >> /etc/dhcp3/dhcpd.conf
echo " option domain-name-servers $CLUSTER_NET_DNS;" >> /etc/dhcp3/dhcpd.conf
echo " filename \"/pxelinux.0\";" >> /etc/dhcp3/dhcpd.conf
echo "}" >> /etc/dhcp3/dhcpd.conf
echo "TFTP_USERNAME=\"tftp\"" > /etc/default/tftpd-hpa
echo "TFTP_DIRECTORY=\"/tftpboot\"" >> /etc/default/tftpd-hpa
echo "TFTP_ADDRESS=\"$CLUSTER_NET_IP:69\"" >> /etc/default/tftpd-hpa
echo "TFTP_OPTIONS=\"--secure\"" >> /etc/default/tftpd-hpa
echo "DEFAULT linux" > /tftpboot/pxelinux.cfg/default
echo "LABEL linux" >> /tftpboot/pxelinux.cfg/default
echo "KERNEL vmlinuz-$KERNEL" >> /tftpboot/pxelinux.cfg/default
echo "APPEND root=/dev/nfs initrd=initrd.img-$KERNEL nfsroot=$CLUSTER_NET_IP:/nfsroot ip=dhcp rw" >> /tftpboot/pxelinux.cfg/default
cp /usr/lib/syslinux/pxelinux.0 /tftpboot
mv /nfsroot/tmp/initrd.img-$KERNEL /tftpboot
cp /boot/vmlinuz-$KERNEL /tftpboot
chmod -R 777 /tftpboot
echo "/nfsroot $CLUSTER_NET_NFS_ACCESS(rw,no_root_squash,async,no_subtree_check)" >> /etc/exports
exportfs -rv
echo "Done. Reboot the server and connect with a PXE client."
@angelassets

This comment has been minimized.

Copy link

commented Aug 1, 2017

Hi Joakim. This is nice. Do you have a modified script to use with a server with just one network adapter?
Orlando

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.