Diskless ubuntu

From Shit we found out
Jump to: navigation, search

Source: how-to-setup-diskless-ubuntu

How to create a diskless ubuntu system with root in RAM

This requires at least two machines:
(1) A boot server --- the server from which the diskless machines will boot.
(2) a diskless machine --- a machine that supports PXE booting and has a fair amount of RAM (I'd recommend at least 4GB, but 2GB should suffice).


Boot server

The boot server requires the following in order to this to work: a dhcp server, a tftp server and an nfs server.
Note that it is not required for these to reside in the same system, and it might also be a good idea to keep the nfs server as a separate machine, at least if you're going to have a lot of diskless machines running.

DHCP

Follow this tutorial to install DHCP server: "DHCP_server" and the optional DNS setup here "DNS_server"

Add the following to your DHCP configuration, either globally, per subnet, or for a specific host:

filename = "gpxelinux.0";
next-server 10.0.0.1;

Change the filename to the name of the pxeimage you have, and the next-server IP to the TFTP servers IP.

TFTP

Follow this tutorial to install TFTP server: "TFTP_server"

Copy pxelinux files to the tftpboot directory

cp <path-to-pxefiles>/{gpxelinux.0,menu.c32} /tftpboot/

Configure pxelinux

Create the directory /tftpboot/pxelinux.cfg

mkdir /tftpboot/pxelinux.cfg

Add a default config file /tftpboot/pxelinux.cfg/default with the following content:

DEFAULT menu.c32
TIMEOUT 1
ONTIMEOUT Diskless
PROMPT 0
MENU TITLE PXE Boot
ALLOWOPTIONS 1
menu width 80
menu rows 15
MENU TABMSGROW 24
MENU MARGIN 10
NOESCAPE 1
LABEL BootLocal
    localboot 0
    TEXT HELP
    Boot to local hdd
    ENDTEXT
LABEL Diskless
    MENU LABEL Ubuntu 12.04 (64-bit) DISKLESS
    KERNEL Ubuntu/12.04/x86_64/vmlinuz-3.5.0-17-generic
    APPEND boot=nfs root=/dev/nfs nfsroot=10.0.0.1:/srv/nfsroot/Ubuntu/12.04/default,ro
initrd=Ubuntu/12.10/x86_64/initrd.img-3.5.0-17-generic ip=dhcp aufs=tmpfs
    TEXT HELP
    Boot Ubuntu 12.04 DISKLESS
    ENDTEXT


With some systems, it might speed up the process if the config filename is equal to the diskless machine's GUID or MAC.

NFS

Follow this tutorial to install NFS server: "NFS_server"

Add nfsroot to exports

Create the folder /srv/nfsroot

mkdir /srv/nfsroot

Add the follwing to /etc/exports:

/srv/nfsroot *(ro,async,no_root_squash,no_subtree_check,no_all_squash) # allow mount as read only
#/srv/nfsroot *(rw,async,no_root_squash,no_subtree_check,no_all_squash) # allow mount as read write

Apply exports:

exportfs -ra

Create diskless Ubuntu

Install Ubuntu to a local hdd, install the programs you want (e.g ssh-server, apache/nginx, etc.)

Prepare the installation for network boot

Network Interfaces

Modify /etc/network/interfaces and set eth0 to manual:

iface eth0 inet manual

File system

Modify /etc/fstab:

# /etc/fstab: static file system information.
#
#
proc        /proc    proc    defaults    0    0
/dev/nfs    /        nfs     defaults    1    1


Initramfs

Change these options in /etc/initramfs-tools/initramfs.conf:

MODULES=netboot
BOOT=nfs
DEVICE=eth0

If the client source installation you copied the files from should remain bootable and usable from local hard disk, restore the former BOOT=local and MODULES=most options you changed in /etc/initramfs-tools/initramfs.conf.

Modules

Create/modify the file /etc/initramfs-tools/modules and add the line:

aufs

Create the folder /etc/initramfs-tools/scripts/modules if it doesn't exist.

mkdir /etc/initramfs-tools/scripts/modules

Add the aufs module to /etc/initramfs-tools/scripts/modules:

cp /lib/modules/$(uname -r)/kernel/ubuntu/aufs/aufs.ko /etc/initramfs-tools/scripts/modules/

Create the file /etc/initramfs-tools/scripts/init-bottom/00_aufs_init and set permissions 0775 on it:

touch /etc/initramfs-tools/scripts/init-bottom/00_aufs_init
chmod 0775 /etc/initramfs-tools/scripts/init-bottom/00_aufs_init

Add the following content to the file /etc/initramfs-tools/scripts/init-bottom/00_aufs_init:

#!/bin/sh -e

case $1 in
  prereqs)
    exit 0
    ;;
esac

for x in $(cat /proc/cmdline); do
  case $x in
    root=*)
      ROOTNAME=${x#root=}
      ;;
    aufs=*)
      UNION=${x#aufs=}
        case $UNION in
          LABEL=*)
            UNION="/dev/disk/by-label/${UNION#LABEL=}"
            ;;
          UUID=*)
            UNION="/dev/disk/by-uuid/${UNION#UUID=}"
            ;;
        esac    
      ;;
  esac
done

echo "Union=$UNION"

if [ -z "$UNION" ]; then
    exit 0
fi

modprobe -b aufs && echo "OK: modprobe -b aufs" || echo "ERR: modprobe -b aufs"

# make the mount points on the init root file system
mkdir /aufs /ro /rw && echo "OK: mkdir /aufs /ro /rw" || echo "ERR: mkdir /aufs /ro /rw"

# mount read-write file system
if [ "$UNION" = "tmpfs" ]; then
  mount -t tmpfs rw /rw -o noatime,mode=0755 && echo "OK:  mount -t tmpfs rw /rw -o noatime,mode=0755 " || 
echo "ERR:  mount -t  tmpfs rw /rw -o noatime,mode=0755"
else
  mount $UNION /rw -o noatime
fi

# move real root out of the way
mount --move ${rootmnt} /ro && echo "OK: mount --move ${rootmnt} /ro" || echo "ERR: mount --move ${rootmnt} /ro"

mount -t aufs aufs /aufs -o noatime,dirs=/rw:/ro=ro && echo "OK: mount -t aufs aufs /aufs -o noatime,dirs=/rw:/ro=ro" ||
echo  "ERR: mount -t aufs aufs /aufs -o noatime,dirs=/rw:/ro=ro"

# test for mount points on union file system
[ -d /aufs/ro ] || mkdir /aufs/ro
[ -d /aufs/rw ] || mkdir /aufs/rw

mount --move /ro /aufs/ro && echo "OK: mount --move /ro /aufs/ro" || echo "ERR: mount --move /ro /aufs/ro"
mount --move /rw /aufs/rw && echo "OK: mount --move /rw /aufs/rw" || echo "ERR: mount --move /rw /aufs/rw"

# strip fstab off of root partition
grep -v $ROOTNAME /aufs/ro/etc/fstab > /aufs/etc/fstab

mount --move /aufs /root && echo "OK: mount --move /aufs /root" || echo "ERR: mount --move /aufs /root"

exit 0

Build a new initrd image

Create new image and store in /root:

update-initramfs -k $(uname -r) -c -b /root/

Copy the system to the nfsroot on the boot server

On the NFS server, create the folder /srv/nfsroot/Ubuntu/12.04/x86_64 and make sure that 'username' has write permissions to this folder.

Use rsync to copy all files to the nfsroot:

rsync -a --exclude=tmp/* --exclude=proc/* --exclude=sys/* --exclude=dev/* / \
username@192.168.1.10:/srv/nfsroot/Ubuntu/12.04/x86_64/

Copy kernel and initrd to the tftproot on the boot server

cp /srv/nfsroot/Ubuntu/12.04/x86_64/boot/vmlinuz-$(uname -r) /tftpboot/Ubuntu/12.04/x86_64/
cp /srv/nfsroot/Ubuntu/12.04/x86_64/root/initrd.img-$(uname -r) /tftpboot/Ubuntu/12.04/x86_64/

Make sure you copy the NEW initrd, which is placed in /root, not /boot, as it will cause you errors that make you commit suicide.


Referenced sites

DHCP_server DNS_server TFTP_server NFS_server