Preface
LiveCDs can be a big help when you need to do a task that cannot be executed from within OS for some reason. Resizing partitions, changing passwords and firmware updates – is just the beginning of the list. It is especially welcome when your machine cannot boot from network ( PXE ).
There are lot of very good options like Knoppix, Parted Magic or LiveCDs of popular distributions. But there are cases when you need custom LiveCD with your scripts, packages and preinstalled software. Ability to modify root partition using writable snapshot on livecd, allows you to add software and modify configuration on the fly.
LiveCD can be easily flashed to usb or converted to pxe images. Hence, there are plenty of ways to get into your custom livecd. Booting LiveCD from network can be a great way to bootstrap your hardware before os installation.
The standard procedure of LiveCD creation requires livecd-tools package together with kickstart file that defines packages list and post installation instructions. All postinstall is done using shell scripts which is far from ideal. Tools like Ansible are better suitable for doing exactly that. Defining system state with DSL language is much cleaner and easily maintanable.
In the next guide I will show how to create your own custom Centos 6.4 LiveCD using step by step instructions.
Installing Requirements
- CentOS 6.X machine.
- livecd-tools, git, python-argparse and ansible – all packages available in EPEL repository.
Install EPEL repository if not already installed:
# yum -y install http://ftp.nluug.nl/pub/os/Linux/distr/fedora-epel/6/i386/epel-release-6-8.noarch.rpm
Install required packages:
# yum -y install livecd-tools git ansible python-argparse
Clone livecd-ansible repository:
$ git clone https://github.com/GR360RY/livecd-ansible.git
Building LiveCD the old way
Let start from getting basic livecd configuration kickstart file for CentOS created by Fabian Arrotin (arrfab):
wget http://people.centos.org/arrfab/CentOS6/LiveCD-DVD/centos6-liveCD-desktop.cfg
livecd-creator
tool from livecd-tools package is used to create custom livecd.
The example follows:
# livecd-creator -c centos6-liveCD-desktop.cfg -f centos6-desktop
This will create centos6-desktop.iso
as defined in centos6-liveCD-desktop.cfg.
The file has standard kickstart format and contains two main parts:
Services configuration and packages selection:
This part is straightforward. Language, Keyboard and Services are self explanatory. The same goes for package list – contains yum groups and individual packages.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Postinstallation stage:
Here the real mess is starting. Triple variable escaping is a pain:
%post
## default LiveCD user
LIVECD_USER="centoslive"
########################################################################
# Create a sub-script so the output can be captured
# Must change "$" to "\$" and "`" to "\`" to avoid shell quoting
########################################################################
cat > /root/post-install << EOF_post
#!/bin/bash
echo ###################################################################
echo ## Creating the livesys init script
echo ###################################################################
Here is an example code snipplet from centos6-liveCD-desktop.cfg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
Maintaining your bash code is not convenient in such a way. The same goes for catching output or just plain debugging. As as result adding your own software to the livecd becomes a laborious task ( in case you need more then just adding an individual package)
Building LiveCD with Ansible
Taking into acount problems presented with the conventional way of creating LiveCD, Ansible was the obvious choise for the task. Ansible supports “chroot path” as transport for playbooks which is ideal for Postinstallation Stage.
Before I get into the details let see how the new build procedure works.
$ cd livecd-ansible
$ ./generate_config.py centos6-mini.yml
$ sudo -s
# livecd-creator -c centos6-mini.ks --cache=cache -f centos6-mini
This will create centos6-mini.iso with only the basic packages.
Before we used kickstart config file to “define” the livecd. From now on we will use Ansible playbook for this purpose.
generate_config.py accepts playbook file as an argument and will generate kickstart config file. All postinstallation steps will be done using Ansible.
Check out the Project Structure below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
centos6-mini.yml will be used to generate config file and run postinstall:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
|
You can copy or modify centos6-mini.yml to create your own livecd by adding your ansible roles below livecd-pre-common. Update lang, keyboard, timezone and repos_list to match your location to reduce download times. If you move livecd-ansible folder to a new location, rerun generate_config.py for every playbook file.
Debugging your Ansible Playbooks
livecd-creator has a hidden fiature – you can get get into the livecd chroot environmnet following the %post stage and before squashfs and iso are created. Let execute livecd-creator with “-l” parameter:
# livecd-creator -l -c centos6-mini.ks --cache=cache -f centos6-mini
You will be dropped to shell after playbook execution:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Open another terminal session and go to livecd-ansible directory. You can modify ansible playbooks directly and rerun playbook as follows:
ansible-playbook -i auto_gen_ansible_hosts-centos6-mini centos6-mini.yml
This procedure can be repeated as many times as you need till you satisfied with the result. Go back to chroot environment and exit the shell with Ctrl+D. It will complete the build procedure. …
Testing Everething with Vagrant
If you don’t have CentOS 6 laying around, you can create livecd using virtualbox and vagrant. Let assume that all the above componenets are already installed. The repository includes Vagrantfile together with provisioning part done with ansible ( Just make sure to install ansible on your machine prior to spinning up the VM).
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
The above setup will configure VM with all the requirements for building livecd. ansible.tags = "host_setup_livecd"
line
will only run “setup” tasks from livecd-builder.yml playbook.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
|
If you already tested your own livecd and want to build it using vagrant, modify the Vagrantfile as in the next example: * Remove ansible.tags line * Add ansible.extra_vars line with the name of your custom playbook without extension.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
livecd-builder will look for centos6-modified.yml and create livecd based on roles included in your file. The resulting iso file will be placed in the source directory.