Friday, 25 July 2014

Install and configure PXE server on Raspberry Pi "RASPBIAN" (Wheezy)

What:

Raspberry Pi with RASPBIAN (Debian Wheezy)

Challenge:

Use Raspberry Pi as PXE and repository server for Ubuntu 14.04 LTS clients.

Solution:

Log on to Raspberry Pi as root and follow below steps.

1. Configure network with a static ip address


edit /etc/network/interfaces

auto eth0
iface eth0 inet static
 address 192.168.1.2
 netmask 255.255.255.0
 gateway 192.168.1.1
 network 192.168.1.0
 broadcast 192.168.1.255


2. Install needed packages


You'll need to run a DHCP server on your network, not necessarily on Pi, but you do need one.
Install the following packages:
apt-get install tftpd-hpa nginx apt-cacher-ng
and optionally DHCP server
apt-get install isc-dhcp-server


3. Cache Ubuntu Packages with apt-cacher-ng


edit /etc/apt-cacher-ng/acng.conf
Set port to 9999


4. Configure tftpd-hpa


You'll need to tell tftpd-hpa to start its daemon (which it doesn't by default). To do this, edit the /etc/default/tftpd-hpa file and replace content with below:
TFTP_USERNAME="tftp"
TFTP_DIRECTORY="/var/lib/tftpboot"
TFTP_ADDRESS="0.0.0.0:69"
#Run in chroot environment with "--secure" and only on ipv4
TFTP_OPTIONS="--secure --ipv4"

RUN_DAEMON="yes"
OPTIONS="-l -s /var/lib/tftpboot"
Make sure /var/lib/tftpboot exists, change permissions if necessary.

Copy netboot files from "Ubuntu 14.04 Server" CD or iso file into the tftpboot folder
cp -r /media/cdrom/install/netboot/* /var/lib/tftpboot/
or
mount -o loop ubuntu.iso /home/user/iso
cp -r /home/user/iso/install/netboot/* /var/lib/tftpboot/

/etc/init.d/tftpd-hpa restart


5. Configure DHCP to work with tftpd


If your pxe server is also your dhcp server, you'll need something like this in /etc/dhcp3/dhcpd.conf
subnet 192.168.1.0 netmask 255.255.255.0 {
        range 192.168.1.100 192.168.1.200;
        filename "pxelinux.0";
}
If you have an existing dhcp server, you should point it to your pxe server by doing something like the following.
subnet 192.168.1.0 netmask 255.255.255.0 {
    <other config here>
    filename "pxelinux.0";
    next-server <pxe host>;
}
/etc/init.d/isc-dhcp-server restart



6. Configure web server, nginx


Create /var/www/ubuntu/installer directories
mkdir -p /var/www/ubuntu/installer
edit /etc/nginx/sites-enabled/default
Set root to /var/www

Copy Ubuntu files from Ubuntu Server CD (ubuntu-desktop package will be downloaded from the internet during install and cached for later use)
cp -r /media/cdrom/* /var/www/ubuntu/
/etc/init.d/nginx restart


7. Create kickstart & preseed configuration


Install on your "desktop ubuntu 14.04 pc" GUI to make kickstart configuration simpler
apt-get install system-config-kickstart

TIP: if gui doesn't start you need to follow steps below to fix it
# apt-get remove hwdata
# wget ftp://mirror.ovh.net/mirrors/ftp.debian.org/debian/pool/main/h/hwdata/hwdata_0.234-1_all.deb
# dpkg -i  hwdata_0.234-1_all.deb
# apt-get install system-config-kickstart

Create and save ks.cfg file, replace url below with your Pi's ip address
#Generated by Kickstart Configurator
#platform=AMD64 or Intel EM64T

#System language
lang en_GB
#Language modules to install
langsupport en_GB
#System keyboard
keyboard gb
#System mouse
mouse
#System timezone
timezone Europe/London
#Root password
rootpw --disabled
#Initial user
user test --fullname "test" --iscrypted --password $1$lMjy34wI$W5pfVXd3d4cA83CaZz4W6/
#Reboot after installation
reboot
#Use text mode install
text
#Install OS instead of upgrade
install
#Use Web installation, if you don't want to use apt-cacher-ng set url to http://gb.archive.ubuntu.com/ubuntu
url --url http://192.168.1.2:9999/ubuntu
#Firewall configuration
firewall --disabled
#Do not configure the X Window System
skipx
#Package install information handled by preseed
#%packages
#@ ubuntu-desktop
%pre
#this is my pre installation script
%post
#this is my post installation script


Preseed file, ubuntu.seed

#### Contents of the preconfiguration file

#### Partitioning
d-i partman-auto/disk string /dev/sda
d-i partman-auto/method string regular

# If one of the disks that are going to be automatically partitioned
# contains an old LVM configuration, the user will normally receive a
# warning. This can be preseeded away...
d-i partman-auto/purge_lvm_from_device boolean true
# And the same goes for the confirmation to write the lvm partitions.
d-i partman-lvm/confirm boolean true
d-i partman-lvm/device_remove_lvm boolean true

# You can choose one of the three predefined partitioning recipes:
# - atomic: all files in one partition
# - home:   separate /home partition
# - multi:  separate /home, /usr, /var, and /tmp partitions
d-i partman-auto/choose_recipe select atomic

# If you just want to change the default filesystem from ext4 to something
# else, you can do that without providing a full recipe.
#d-i partman/default_filesystem string ext3

# This makes partman automatically partition without confirmation, provided
# that you told it what to do using one of the methods above.
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

# Set to true if you want to encrypt the first user's home directory.
d-i user-setup/encrypt-home boolean false

### Network configuration
# netcfg will choose an interface that has link if possible. This makes it
# skip displaying a list if there is more than one interface.
d-i netcfg/choose_interface select auto

# To pick a particular interface instead:
#d-i netcfg/choose_interface select eth1

# If you have a slow dhcp server and the installer times out waiting for
# it, this might be useful.
d-i netcfg/dhcp_timeout string 60

### Apt setup
#d-i apt-setup/source boolean false
# Additional repositories, local[0-9] available
#d-i apt-setup/local0/repository string \
#      http://gb.archive.ubuntu.com/ubuntu/ trusty main restricted
#d-i apt-setup/services-select multiselect security
#d-i apt-setup/security_host string security.ubuntu.com
#d-i apt-setup/security_path string /ubuntu

### Package selection
tasksel tasksel/first multiselect ubuntu-desktop
#tasksel tasksel/first multiselect lamp-server, print-server
#tasksel tasksel/first multiselect kubuntu-desktop

# Individual additional packages to install
d-i pkgsel/include string \
    openssh-server

# Policy for applying updates. May be "none" (no automatic updates),
# "unattended-upgrades" (install security updates automatically), or
# "landscape" (manage system with Landscape).
d-i pkgsel/update-policy select unattended-upgrades

### Boot loader installation
# This is fairly safe to set, it makes grub install automatically to the MBR
# if no other operating system is detected on the machine.
d-i grub-installer/only_debian boolean true

# This one makes grub-installer install to the MBR if it also finds some other
# OS, which is less safe as it might not be able to boot that other OS.
d-i grub-installer/with_other_os boolean true

### Finishing up the installation
# Avoid that last message about the install being complete.
d-i finish-install/reboot_in_progress note

# This will prevent the installer from ejecting the CD during the reboot,
# which is useful in some situations.
d-i cdrom-detect/eject boolean false

### Preseeding other packages
# Depending on what software you choose to install, or if things go wrong
# during the installation process, it's possible that other questions may
# be asked. You can preseed those too, of course. To get a list of every
# possible question that could be asked during an install, do an
# installation, and then run these commands:
#   debconf-get-selections --installer > file
#   debconf-get-selections >> file

#### Advanced options
# This command is run just before the install finishes, but when there is
# still a usable /target directory. You can chroot to /target and use it
# directly, or use the apt-install and in-target commands to easily install
# packages and run commands in the target system.
d-i preseed/late_command string \
    in-target wget http://192.168.1.2/ubuntu/installer/post_install.sh -O /root/post_install.sh && \
    in-target chmod 755 /root/post_install.sh && \
    in-target /root/post_install.sh


Post install script, post_install.sh

#!/bin/bash

REPO_IP_ADDR=192.168.19.2

# Use last 4 characters from mac address as part of unique hostname
MACADDR=$(ifconfig | grep HWaddr | head -1 | sed 's/^.*\([0-9a-z][0-9a-z]\):\([0-9a-z][0-9a-z]\)[ \t]*$/\1\2/')
HOSTMAC="host-${MACADDR}"
hostname "${HOSTMAC}"
sed -i 's/kickseed/'"${HOSTMAC}"'/g' /etc/hostname /etc/hosts

# Set apt-get to use apt-cacher-ng installed on pi
echo "Acquire::http::Proxy \"${REPO_IP_ADDR}:9999\";" > /etc/apt/apt.conf.d/01proxy

# Repositories
wget http://${REPO_IP_ADDR}/ubuntu/installer/sources.list -O /etc/apt/sources.list


Repositories, sources.list file

#########################################################
# Proxy to APT set in /etc/apt/apt.conf.d/01proxy as recommended
# https://help.ubuntu.com/community/Apt-Cacher-Server
#########################################################
deb http://gb.archive.ubuntu.com/ubuntu trusty main restricted #universe multiverse
#deb-src http://gb.archive.ubuntu.com/ubuntu trusty main restricted universe multiverse

deb http://gb.archive.ubuntu.com/ubuntu trusty-updates main restricted #universe multiverse
#deb-src http://gb.archive.ubuntu.com/ubuntu trusty-updates main restricted universe multiverse

deb http://gb.archive.ubuntu.com/ubuntu trusty-security main restricted #universe multiverse
#deb-src http://gb.archive.ubuntu.com/ubuntu trusty-security main restricted universe multiverse

deb http://gb.archive.ubuntu.com/ubuntu trusty-backports main restricted #universe multiverse
#deb-src http://gb.archive.ubuntu.com/ubuntu trusty-backports main restricted universe multiverse

## This software is not part of Ubuntu, but is offered by third-party
## developers who want to ship their latest software.
#deb http://extras.ubuntu.com/ubuntu trusty main

copy sources.list & ks.cfg files to /var/www/ubuntu/


8. Update boot menu

Edit /var/lib/tftpboot/pxelinux.cfg/default

# D-I config version 2.0
# Default menu options
#include ubuntu-installer/amd64/boot-screens/menu.cfg
# Fancy look
include ubuntu-installer/amd64/boot-screens/stdmenu.cfg
default ubuntu-installer/amd64/boot-screens/vesamenu.c32
#Uncomment below to start process automatically without a timeout
#default linux
prompt 0
timeout 300

label linux
    menu label ^Wipe data & reload system
        kernel ubuntu-installer/amd64/linux
        append ks=http://192.168.1.2/ubuntu/installer/ks.cfg vga=normal preseed/url=http://192.168.1.2/ubuntu/installer/ubuntu.seed initrd=ubuntu-installer/amd64/initrd.gz

Now restart Raspberry Pi, make sure that all services start and you are done!



Related post:
Certain machines can't boot from linux PXE server, pxelinux.0 file not found

No comments:

Post a comment