Friday, July 8, 2016

boot up a diskless ubuntu using nfs and pxe

I- Prepare PXE

1- install necessary packages
$ sudo apt-get install pxelinux syslinux dnsmasq nfs-kernel-server qemu

2- create a directory to store all data
$ mkdir ~/ubuntudiskless && cd ~/ubuntudiskless

3- copy necessary file to our directory,
$ cp /usr/lib/PXELINUX/pxelinux.0 ~/ubuntudiskless
$ cp /usr/lib/syslinux/modules/bios/*.c32 ~/ubuntudiskless

$ sudo cp /boot/vmlinuz-4.4.0-21-generic ~/ubuntudiskless 
$ sudo cp /boot//boot/initrd.img-4.4.0-21-generic ~/ubuntudiskless
$ sudo chmod  777 ~/ubuntudiskless/vmlinuz-4.4.0-21-generic
$ sudo chmod  777 ~/ubuntudiskless/initrd.img-4.4.0-21-generic

*** note
test with kernel version 3.13.0-68 not work

5- update boot menu
$ mkdir pxelinux.cfg

- create file ~/ubuntudiskless/pxelinux.cfg/default with the following content
default menu.c32
prompt 0
timeout 300
ONTIMEOUT local
menu title ########## PXE Boot Menu ##########
label 1
menu label ^1) ubuntu diskless nfs
kernel vmlinuz-4.4.0-21-generic
APPEND  root=/dev/nfs initrd=initrd.img-4.4.0-21-generic nfsroot=192.168.13.1:/mnt ip=dhcp
menu label ^2) ubuntu diskless nfs


6- create bash script file to start our tftp and dhcp so we name it as dnsmasq-tftp.sh with content below
#/bin/bash
ifconfig vmnet2 192.168.13.1 up
dnsmasq -i vmnet2 --dhcp-range=192.168.13.100,192.168.13.200 \
    --dhcp-boot=pxelinux.0 \
    --enable-tftp --tftp-root=~/ubuntudiskless -d -p0 -K --log-dhcp --bootp-dynamic




II- Prepare NFS server

1- suppose you have installed  a ubuntu on virtualbox and it has hard disk file, ubun.vdi
- mount it to /mnt
$ sudo modprobe nbd max_part=16
$ sudo qemu-nbd -c /dev/nbd0 ubun.vdi
$ sudo mount /dev/nbd0p1 /mnt

2- update nfs exports file, /etc/exports as below
/mnt    192.168.13.0/24(rw,no_root_squash,async,insecure)

3- update fstab, /mnt/etc/fstab, to use nfs as the root partitiion, so the content is below
/dev/nfs       /               nfs    defaults          1       1
none            /tmp            tmpfs   defaults 0 0
none            /var/run        tmpfs   defaults 0 0
none            /var/lock       tmpfs   defaults 0 0
none            /var/tmp        tmpfs   defaults 0 0
none            /media          tmpfs   defaults 0 0



4- restart nfs service
$ sudo /etc/init.d/nfs-kernel-server restart



III- Start PXE and Boot from Client
1- start pxe/dhcp server
$ sudo  sh ~/ubuntudiskless/dnsmasq-tftp.sh

2- boot client and choose boot from network cards,

screenshot of diskless ubuntu after boot  and login




No comments:

Post a Comment