PXE Booting
===========

How to use pxe_manager.py
-------------------------

0. setup configuration file for tftp server and permissions

.. code-block:: bash

  sudo apt-get install dhcp3-server tftpd-hpa syslinux nfs-kernel-server initramfs-tools

.. code-block:: bash

  cat /etc/default/tftpd-hpa

the output is

.. code-block:: bash

  # /etc/default/tftpd-hpa
  TFTP_USERNAME="tftp"
  TFTP_DIRECTORY="/data/tftpboot"
  TFTP_ADDRESS="0.0.0.0:69"
  TFTP_OPTIONS="-v --secure"
  RUN_DAEMON="yes"
  #OPTIONS="-l -s /data/tftpboot"


in order to run without superuser permission, we need change the permissions.

.. code-block:: bash

  sudo usermod -G tftp YOURNAME        # add your account to tftp group
  sudo chgrp tftp /data/tftpboot
  sudo chmod g+rw /data/tftpboot
  sudo chgrp -R tftp /data/tftpboot/pxelinux.cfg
  sudo chmod -R g+rw /data/tftpboot/pxelinux.cfg
  sudo chgrp tftp /etc/dhcp3/dhcpd.conf
  sudo chmod g+rw /etc/dhcp3/dhcpd.conf

.. code-block:: bash

  ls -l /data
  > drwxrwxrwx 9 root tftp  4096 2011-08-29 11:46 tftpboot


1. generate a filesystem for pxe boot

.. code-block:: bash

  ./pxe_manager.py --generate-pxe-filesystem /data/tftpboot/root_you

2. add a host to DB

.. code-block:: bash

  ./pxe_manager.py --auto-add root_you --db=/data/tftpboot/pxe.db
  602dhcp7.jsk.t.u-tokyo.ac.jp

After that, you need to restart dhcp service

.. code-block:: bash

  sudo service dhcp3-server restart
  
3. generate a virtualbox image

.. code-block:: bash

  ./pxe_manager.py --generate-virtualbox-image VMNAME --db=/data/tftpboot/pxe.db

The MAC address of VMNAME is estimated from DB, which was generated by --auto-add command.
You can use --virtualbox-macaddress option to specify the MAC address directly.

.. code-block:: bash

  ./pxe_manager.py --generate-virtualbox-image VMNAME --virtualbox-macaddress=00:11:22:33:44:55

If you specify a hostname of a physical machine, pxe_manager.py will decide the parameters of vm according to
the spec of the machine. The parameters are the number of cpu and the size of memory (80% of the physical machine).

.. code-block:: bash

  ./pxe_manager.py --generate-virtualbox-image VMNAME --refer-physical-machine PHYS_MACHINE

References
==========

- https://help.ubuntu.com/community/DisklessUbuntuHowto

- http://neil.franklin.ch/Projects/dphys-swapfile/
  
Setting up VirtualBox with PXE Boot
-----------------------------------

1. Install VirtualBox:

.. code-block:: bash

  wget -q  http://download.virtualbox.org/virtualbox/debian/oracle_vbox.asc -O- | sudo apt-key add -
  echo "deb http://download.virtualbox.org/virtualbox/debian `lsb_release -cs` contrib non-free" | sudo tee /etc/apt/sources.list.d/virtualbox.list
  sudo apt-get update
  sudo apt-get install virtualbox-4.1

2. Configure your OS:

.. code-block:: bash

  sudo /etc/init.d/vboxdrv setup

3. Add your user to the vboxusers group in /etc/group

4. Create a bridge ethernet setup:

.. code-block:: bash

  sudo apt-get install bridge-utils

5. Restart networking:

.. code-block:: bash

  sudo /etc/init.d/networking restart

6. Start virtualbox, create a new OS with no local hard drive. Set the Network Adapter to::

  Attached to: Bridged Adapter
  Name: eth0
  Adapter Type: PCnet-PCI II
  Click on Cable connected 

Make sure to enable 3D acceleration and allocate the right amount of cpu and memory resources.

NOTE: It would be great to have a script that configures the new VM.

Commandline Interface to VirtualBox
-----------------------------------

1. listing the registered vms

.. code-block:: bash

  VBoxManage list vms
  "pxetest" {341ef969-487a-4827-a59e-905bb3fb8f7a}
  "pxetest2" {904d947a-8723-433b-b27e-5cf9241c523b}

2. start vm with GUI

.. code-block:: bash

  virtualbox --startvm VMNAME

3. start vm without GUI (headless)

.. code-block:: bash

  VBoxManage startvm VMNAME --type headless

4. register a new vm (.vms file) to your virtualbox

.. code-block:: bash

  VBoxManage registervm foo.vms
  
5. connect to a headless VM

.. code-block:: bash

  rdesktop-vrdb HYPERVISORNAME:8888
  
6. restart a VM

.. code-block:: bash

  VBoxManage controlvm VMNAME reset

7. restart all VMs using vboxapi

.. code-block:: python

  import vboxapi
  # todo
