Skip to content

Instantly share code, notes, and snippets.

@jtyr
Last active February 23, 2024 01:38
Show Gist options
  • Save jtyr/816e46c2c5d9345bd6c9 to your computer and use it in GitHub Desktop.
Save jtyr/816e46c2c5d9345bd6c9 to your computer and use it in GitHub Desktop.
How to use kickstart in VirtualBox

How to use kickstart in VirtualBox

This short howto describes how to install VMs via kickstart in VirtualBox. It's using PXE functionality built into the NAT network mode of the VirtualBox. The following instructions apply to CentOS installation but it should work for any RedHat-based distro.

Prepare directory structure

mkdir -p ~/.config/VirtualBox/TFTP/{pxelinux.cfg,images/centos/6}

Get PXE files from syslinux

Get the latest Syslinux and extract all necessary files:

wget -O - https://www.kernel.org/pub/linux/utils/boot/syslinux/6.xx/syslinux-6.03.tar.gz | \
tar -xzf - -C ~/.config/VirtualBox/TFTP/ --transform='s/.*\///' \
syslinux-6.03/bios/{core/pxelinux.0,com32/{menu/{menu,vesamenu}.c32,libutil/libutil.c32,elflink/ldlinux/ldlinux.c32,chain/chain.c32,lib/libcom32.c32}}

Or you can get these files from already installed system (yum install syslinux and then /usr/share/syslinux/{pxelinux.0,{menu,vesamenu,chain}.c32}).

Get ramdisk and kernel images

You can download them from the web:

cd ~/.config/VirtualBox/TFTP/images/centos/6/
wget http://mirror.centos.org/centos/6/os/x86_64/images/pxeboot/{initrd.img,vmlinuz}

Or you can get them from the installation CD (/images/pxeboot/{initrd.img,vmlinuz}).

Create default PXE menu

You can create nice menu:

cat <<END > ~/.config/VirtualBox/TFTP/pxelinux.cfg/default
PROMPT 0
NOESCAPE 0
ALLOWOPTIONS 0
TIMEOUT 100

### TUI
DEFAULT menu.c32

### GUI
#UI vesamenu.c32
# The splash.png file is a PNG image with resolution of 640x480 px
#MENU BACKGROUND splash.png

MENU TITLE ---===[ Boot Menu ]===---

LABEL local
  MENU DEFAULT
  MENU LABEL ^1. Boot from hard drive
  COM32 chain.c32
  APPEND hd0

LABEL centos6
  MENU LABEL ^2. CentOS 6
  KERNEL images/centos/6/vmlinuz
  APPEND initrd=images/centos/6/initrd.img ks=http://10.0.2.2/kickstart/centos-ks.cfg ip=dhcp ksdevice=eth0 ramdisk_size=10000 ipv6.disable=1 biosdevnames=0 net.ifnames=0 unsupported_hardware text
END

Or you can use menu-less mode if you create a file with the name of the MAC address of the VM prefaced with ARP type code (01 at the beginning of the file name). For example if the VM's MAC address is 08-00-27-81-4E-29, the file will be ~/.config/VirtualBox/TFTP/pxelinux.cfg/01-08-00-27-81-4e-29 and should contain this setting:

DEFAULT centos6

LABEL centos6
  KERNEL images/centos/6/vmlinuz
  APPEND initrd=images/centos/6/initrd.img ks=http://hostip/kickstart/centos-ks.cfg ip=dhcp ksdevice=eth0 ramdisk_size=10000 ipv6.disable=1 biosdevnames=0 net.ifnames=0 unsupported_hardware text

Serve kickstart file via HTTP

Install HTTP server on your host and make the following file accessible through it:

cat <<END > /srv/http/kickstart/centos-ks.cfg
install
cdrom
lang en_US.UTF-8
keyboard us

network --device eth0 --onboot yes --bootproto dhcp

# To generate password use: openssl passwd -1 -salt abc yourpass
rootpw --iscrypted \$1\$abc\$eXT.vKU2cv.5/y/x/JA1H/
firewall --disabled
authconfig --enableshadow --enablemd5
selinux --disabled
timezone --utc US/Central
services --enabled=sshd

bootloader --location=mbr --driveorder=sda

zerombr
clearpart --all --initlabel
part /boot --fstype ext4 --size=200 --ondisk=sda
part pv.0 --size=1 --grow --ondisk=sda
volgroup vg.0 pv.0
logvol swap --fstype swap --name=lv.swap --vgname=vg.0 --size=1024
logvol / --fstype ext4 --name=lv.root --vgname=vg.0 --size=1 --grow

# Reboot after installation
reboot

%packages --ignoremissing
@core
%end
END

Reflect the HTTP path in the ks=... parameter of the file in the pxelinux.cfg directory (default or MAC-address-based file).

Boot VM from PXE

Now the tricky part. In order to make the above working, you have to copy the pxelinux.0 to a file of the name of the VM. For example if the VM's name is Test, name the file like Test.pxe. Avoid spaces in the VM name otherwise the PXE won't load!

cp ~/.config/VirtualBox/TFTP/pxelinux.0 ~/.config/VirtualBox/TFTP/Test.pxe

If you don't want to copy the PXE file for every VM, you can specify the default PXE file with the following command:

VBoxManage modifyvm "Test" --nattftpfile1 /pxelinux.0

That will make sure that VirtualBox will use the pxelinux.0 file as the PXE file for that VM.

Set the VM to use the NAT networking (Network -> Adapter 1 -> Attached to: NAT). Then when you start the VM, press F12 and then l for booking from LAN. Before the installation begins, make sure you have some installation media attached to the VM. Complete and fully automated installation of CentOS usually takes about 4 minutes.

Create VM template

If you don't want to run the modifyvm command everytime you create a new VM, you can export already modified VM as a template (File -> Export Appliance...). I recomend to do that before you install anything on the VM as the template will have only 82kb in size. Then if you want to create a new VM, instead of running the Create Virtual Machine wizard, you can import it from the template (File -> Import Appliance...).

You can also change the boot order to have the Network as the first option (System -> Motherboard -> Boot Order). Then the VM will always boot from the network but as the default option in the default file is the Boot from hard drive, it will boot the OS from the VM if no other option is choosen from the menu.

@nanoscopic
Copy link

With my install of Virtualbox, I had to use:
VBoxManage modifyvm [vm name] --nattftpfile1 pxelinux.0

If I put a slash in front of pxelinux.0, vbox built in gpxe attempts to fetch the file locally using an extra slash from the built in vbox TFTP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment