Ubuntu Bootable USB with Kickstart

tldr; use isohybrid after repacking the custom iso

To make this usb, I used a virtual machine with Ubuntu 14.04 installed. The goal here is to make a bootable usb that doesn’t require selection of things like keyboard layout, language, etc. We want an automatic install of an ubuntu server.

Many of these commands must be done as root, so we’ll start by dropping into root and going to the home folder (cd will take us to the home folder by default).

$ sudo su
# cd

Now we need to get the ubuntu image that we’re going to be customizing for our installation.

# wget http://releases.ubuntu.com/14.04/ubuntu-14.04.2-server-amd64.iso

We’re going to need a place to mount the unpacked iso file.

# mkdir -p /mnt/iso

After we have created a mount point for it, now we have to mount it. This will expose the files that are held inside the iso. Unfortunately, the files will be read-only

# mount -o loop ubuntu-14.04.2-server-amd64.iso /mnt/iso

In order to modify the files, we need to copy them to a directory where we can modify them, so we’ll create the directory and then copy the files to it.

# mkdir -p /opt/ubuntuiso
# cp -rT /mnt/iso /opt/ubuntuiso

Our new working directory will be /opt/ubuntuiso

# cd /opt/ubuntuiso

In order to avoid being prompted for language selection in the installation process, we need to tell it what language we’re going to use. In this case, en is being used because I speak english.

# echo en >isolinux/lang

Now we are actually going to build the kickstart file with a program called system-config-kickstart. So we make sure it’s installed and then run it.

# apt-get install system-config-kickstart
# system-config-kickstart

Then save the file from the GUI in /opt/ubuntuiso/. This should save a file called ks.cfg. My ks.cfg looks like the following

#Generated by Kickstart Configurator
#platform=AMD64 or Intel EM64T
#System language
lang en_US
#Language modules to install
langsupport en_US
#System keyboard
keyboard us
#System mouse
#System timezone
timezone America/Denver
#Root password
rootpw --disabled
#Initial user
user ubuntu --fullname "ubuntu" --iscrypted --password $1$MQ0zGB4W$pwjX8nolgr2RJch2Omamt.
#Reboot after installation
#Use text mode install
#Install OS instead of upgrade
#Use CDROM installation media
#System bootloader configuration
bootloader --location=mbr
#Clear the Master Boot Record
zerombr yes
#Partition clearing information
clearpart --all --initlabel
#Disk partitioning information
part /boot --fstype ext2 --size 100 --asprimary
part swap --recommended
part / --fstype ext4 --size 1 --grow
#System authorization infomation
auth --useshadow --enablemd5
#Network information
network --bootproto=dhcp --device=eth0
#Firewall configuration
firewall --disabled
#Do not configure the X Window System
exec < /dev/tty6 > /dev/tty6
chvt 6
echo 'Acquire::http { Proxy ""; };' > /etc/apt/apt.conf.d/02proxy
apt-get update
apt-get -y upgrade
apt-get -y dist-upgrade
apt-get install -y git ansible openssh-server vim
chvt 1

There is a little bit of hijacking done in the postscript (everything after %post). The line beginning with exec and the following line (chvt 6) are forcing the installer show the output from the commands that are run after that. The reason for this is that the loading bar will not move during the installation, and it can appear as though the installer has hung. The following line that does the proxy is for the apt-cache on our local network. The final line (chvt 1) reverts the output to the main installer.

Now we need to add the preseed file so that we can avoid other quesitons.

# echo 'd-i partman/confirm_write_new_label boolean true
d-i partman/choose_partition \
select Finish partitioning and write changes to disk
d-i partman/confirm boolean true' > ks.preseed

Now we need to tell the installer about the files that we just saved, so we need to modify isolinux/txt.cfg. First we need to make the file writable.

# chmod +w isolinux/txt.cfg

Then we need to replace the append line of the following section

label install
menu label ^Install Ubuntu Server
kernel /install/vmlinuz
append file=/cdrom/preseed/ubuntu-server.seed vga=788 initrd=/install/initrd.gz quiet --

Replace the append line with

append file=/cdrom/preseed/ubuntu-server.seed initrd=/install/initrd.gz ks=cdrom:/ks.cfg preseed/file=/cdrom/ks.preseed --

Save and exit the file. We also need to make sure that we put the file back in its original state, so remove the write permissions.

# chmod -w isolinux/txt.cfg

Now create the new iso file that we will use to create the bootable usb.

# mkisofs -D -r -V "ATTENDLESS_UBUNTU" -cache-inodes -J -l -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -o /opt/autoinstall.iso /opt/ubuntuiso

In order to finalize the bootable iso for a usb, we need to hybridize it.

# isohybrid /opt/autoinstall.iso

After hybridizing it, we can push it to the usb drive. The usb drive in my virtual machine shows up in /dev/sdb. Make sure that the usb drive is unmounted.

# umount /dev/sdb

And finally push the iso onto the usb drive.

# dd if=/opt/autoinstall.iso of=/dev/sdb

Now your usb drive should be ready for installation!

Credit to Elazar Leibovich for providing an awesome answer on stackoverflow

Ubuntu Bootable USB with Kickstart

3 thoughts on “Ubuntu Bootable USB with Kickstart

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s