Unattended Kickstart Installation of CentOS 7 that Works

If you need to install CentOS 7 frequently, with the same configuration, build your own DVD media for unattended kickstart installation. We show you how to make it work. As of CentOS 7 this only works for clean installs. We assume you know how to burn your own .iso files to a DVD-ROM.

It cost hours and a small stash of blank DVD disks to complete this article, because of incomplete documentation and quirks such as the need to preserve volume label name. A significant portion of what I found on the internet did not work. This procedure, however, was tested and verified with a PC running CentOS 7.4 to perform most operations, and a MacOS X 10.9.5 laptop with VirtualBox v5.1.30.

This procedure works for creating disk media for legacy BIOS mode booting as well as for UEFI mode booting. The major steps covered in this post are the following:

[ ] Download official iso image or create a CentOS installer DVD
[ ] Install CentOS attended, configuring things the way you want them
[ ] Create a working directory to compose an unattended installer ISO image
[ ] Copy the required files from original installation medium to working directory
[ ] Copy the kickstart file from the attended CentOS install to the working directory
[ ] Edit the kickstart configuration file further, as needed
[ ] Modify the installer menu
[ ] Create custom ISO image from the working files
[ ] Test on a virtual machine using VirtualBox, and burn to DVD

We need two machines. The first one can be a MacOS, CentOS, or Linux computer, and to burn you final custom DVD-ROM it will need to have an optical drive. The other machine can be a VM (we assume you are using VirtualBox) or a second computer, and is used as a CentOS installation target, and to perform most of the customization and ISO building work.

Download CentOS ISO Image

On MacOS, in your home directory, download a standard image (in this case a “Minimal” version). The URL used here links to the latest release of CentOS 7.

curl -L "http://buildlogs.centos.org/rolling/7/isos/x86_64/CentOS-7-x86_64-Minimal.iso" -o "CentOS-7-x86_64-Minimal.iso"

On CentOS:

sudo yum install -y wget
sudo wget https://buildlogs.centos.org/rolling/7/isos/x86_64/CentOS-7-x86_64-Minimal.iso

We will need to keep the label of our custom installer media the same to avoid issues. So let’s check the label and type.

On CentOS:

file -sL CentOS-7-x86_64-Minimal.iso
CentOS-7-x86_64-Minimal.iso: # ISO 9660 CD-ROM filesystem data 'CentOS 7 x86_64' (bootable)

On MacOS the label is reported with some extra spaces:

CentOS-7-x86_64-Minimal.iso: ISO 9660 CD-ROM filesystem data 'CentOS 7 x86_64                ' (bootable)

If you are not going to use VirtualBox VM as initial installation target – an attended install just for generating the initial kickstart file, you need to burn this standard ISO image to a DVD-ROM disk.

Attended CentOS Installation

This attended custom installation becomes the starting point for your unattended custom installer. You can install CentOS from the downloaded original CentoOS installer iso image in MacOS onto a VirtualBox VM, or install from a DVD onto a VM, or another machine.

Installing an iso image onto a VM is easiest. The following assumes VirtualBox is installed.

Installation to a VM (VirtualBox)

Run Virtualbox, then create a new VM, configuring it as a Linux, Red Hat (64-bit) machine, and create a virtual hard disk of type VDI and size of 8G disk (e.g. test.vdi) which can be of the dynamically allocated kind.

After creating the VM, select in Settings – Storage the IDE controller’s optical drive “Empty” and attach the downloaded .iso file as a virtual DVD (Optical Drive, IDE Secondary Slave) clicking on the DVD icon on the very right and browsing or selecting the ISO file from the previous section. No need to check the Live CD/DVD box.

Next configure networking in Settings – Network using the drop-down menu and selecting Bridged Adapter to make it possible to connect remotely. (“Host-only adapter” would have worked if you did not need to connect to the internet from the VM, and “NAT” would have worked if you did not need to connect from a local machine to your VM.) OK.

Start the VM and it should boot from the image (or you might need to select the boot media with F12).

Go through the installation and configure things they way you want them for future unattended installations. You may want to enable Ethernet, set a default host name and domain, set timezone, root password, and create a userid (as administrator) and password. Later we will be able to edit the details. This is just to create a convenient starting point.

When install is completed (the Reboot button appears), detach the virtual optical drive (in VirtualBox SettingsStorage, click on “CentOS-7…” and the DVD icon on the very right again, and “Remove Disk form Virtual Drive”). OK.

Then click on [Reboot], to reboot the VM. After reboot (if the VM froze just hard-reset it) login and check for the kickstart configuration file (anaconda-ks.cfg), which we will use in a later step.

sudo ls /root/
sudo more /root/anaconda-ks.cfg

Connecting to the CentOS VM

It is often convenient to be working from a local terminal on your MacOS or Linux machine. Determine the ip address of the VM’s network adapter with one of the following on the CentOS VM:

ip addr
hostname -I

Open a terminal on the local system and replace <ip-addres> with the address. Continue connecting by typing yes and then the password. The ssh -l <user-id> option is only needed if your login user-id on the VM is different than that on the system you are connecting from.

ssh <ip-address>

If you get a warning that “Host Identification Has Changed” you need to delete from your known_hosts file a line with an old signature corresponding to the IP address you are trying to connect to. The file is in ~/.ssh/known_hosts. Then try again.

Preparing Staging Directory

Next we will prepare a staging directory on our CentOS system for our customized installer ISO image. We will mount an ISO image of the installer, or a DVD-ROM drive with installer disk inserted, copy files to the staging directory, and edit some files before we finalize the custom image.

First ensure we have a mounting point on our CentOS system for the standard installer at /media/mydrive/ :

sudo mkdir -p /media/mydrive

Option 1: Mount External DVD-ROM Drive

To mount a real DVD-ROM drive on a computer with your new CentOS installation, or a virtual CD-ROM drive or an image on VirtualBox (after you have attached it to the VM as before), you can use:

sudo mount -t iso9660 /dev/sr0 /media/mydrive

After mounting it, check if you can see the files in /media/mydrive.

sudo ls /media/mydrive

Option 2: Mount an iso image from within CentOS

In case you want to mount an iso image file from within your new CentOS install and use that, you need to get the image on the CentOS machine, and then mount it using a “loop device“.

You can get the image onto your CentOS install by downloading it again (Download ISO Image) or copying it from another machine (e.g. using scp.) Then proceed to mounting it:

A loop device is a virtual device node that after creation appears in /dev/ and which can be mounted as any other drive. On CentOS (and Linux, but not MacOS), mount can both create the loop device and mount it in one step, using option -o loop. You can add -t iso9660 option to be more explicit about the type but this may not be necessary.

sudo mount -o loop CentOS-7-x86_64-Minimal.iso /media/mydrive

Just for reference, on MacOS, you can use hdiutil instead of mount -o loop. But you must use the -t iso9660 option with hdiutil for this to work. If hdiutil command then lists a different device than /dev/disk1, use that device in the mount command in the next section.

hdiutil attach -nomount CentOS-7-x86_64-Minimal.iso
sudo mount -t cd9660 /dev/disk1 /media/mydrive

Creating a Working copy of installation media

Next, on the CentOS system, we create the staging directory /var/tmp/mydrive, install rsync, and copy files:

sudo mkdir -p /var/tmp/media/mydrive
sudo yum install -y rsync
sudo rsync -av /media/mydrive/ /var/tmp/media/mydrive/

After copying, this is how the directory /var/tmp/media/mydrive looks like after successful copy operation (I replaced my real userid with user below):

ls -lah /var/tmp/media/mydrive/ 
total 84K
drwxrwxr-x. 8 root root  254 Jan  8 06:58 .
drwxr-xr-x. 3 root root   21 Jan 13 02:05 ..
-rw-rw-r--. 1 root root   14 Sep  5 08:25 CentOS_BuildTag
-rw-rw-r--. 1 root root   29 Sep  5 08:33 .discinfo
drwxrwxr-x. 3 root root   35 Sep  5 08:36 EFI
-rw-rw-r--. 1 root root  227 Aug 30 09:33 EULA
-rw-rw-r--. 1 root root  18K Dec  9  2015 GPL
drwxrwxr-x. 3 root root   57 Sep  5 08:46 images
drwxrwxr-x. 2 root root  198 Sep  5 08:36 isolinux
drwxrwxr-x. 2 root root   43 Sep  5 08:36 LiveOS
drwxrwxr-x. 2 root root  28K Jan  8 06:58 Packages
drwxrwxr-x. 2 root root 4.0K Jan  8 06:58 repodata
-rw-rw-r--. 1 root root 1.7K Dec  9  2015 RPM-GPG-KEY-CentOS-7
-rw-rw-r--. 1 root root 1.7K Dec  9  2015 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r--. 1 root root 2.9K Jan  8 07:02 TRANS.TBL
-rw-rw-r--. 1 root root  354 Sep  5 08:36 .treeinfo

You can skip first to Section Build ISO Image, to check if you can reconstruct an image that boots and installs correctly on VirtualBox without making any modifications. Once that works, you can return to this point, and continue with configuring and customizing kickstart.

Delete or Detach loop device

When we are done copying, we can unmount the source, and detach the loop device (if applicable).

sudo umount /media/mydrive

To remove the loop device on Linux try losetup -d <loopdevice> (this one may not work – I have not tested it). You need to determine the correct loop device name, e.g. /dev/loop0, corresponding to your image. You can check the status of loop devices with losetup -a.

losetup -d /dev/loop0

Just for reference, on MacOS, you can detach the iso file from the loop device as follows, substituting /dev/disk1 with the correct device:

hdiutil detach /dev/disk1

Kickstart Customization

A kickstart configuration file is needed for automated installation and we have created a “draft” by performing a CentOS installation. For instance, getting your root and user passwords inserted this way is really convenient, and you can be sure the format of the file is the latest. The actual file on the install media should, however, be named ks.cfg.

On the CentOS machine copy the anaconda-ks.cfg file from /root/ to your home directory, rename it, and then edit it for instance using vi editor, to meet your needs, referring to the documentation here.

sudo cp /root/anaconda-ks.cfg ~/ks.cfg
sudo vi ~/ks.cfg

After exiting the editor with key sequence <Esc> <:> <w> <q>, verify your modified configuration file using  a tool called ksvalidator. Let us install it and then run the validation. On CentOS:

sudo yum install -y pykickstart
sudo ksvalidator ~/ks.cfg

If the ksvalidator does not respond with any issues, your file is OK. Otherwise, the errors must be resolved (and yes, sometimes even the auto-generated “draft” without any modification fails the test!)

Truly Unattended Installation

A few additions are needed to the kickstart file to enable truly unattended installation. However, this comes with the risk of accidentally erasing data on an existing system by inserting the wrong installer image into a DVD-ROM drive!! Write in red “DANGER – erases all data when booted” on your DVD.

For fully automatic installation, ensure that root password and user passwords are set in the kickstart file (so that the installer does not need to stop to get that input), ensure partitioning is automatic i.e. including commands auto part –type=lvm, clearpart –all, and zerombr, and (optionally) accept the eula with eula –agreed, and finally, have the system reboot after installation by adding the command reboot –eject.

Placing Kickstart file into ISO staging directory

Next move the ks.cfg file to your custom ISO staging directory.

sudo mv ~/ks.cfg /var/tmp/media/mydrive/ks.cfg

My file now looks something like this:

-rw-------   1 root  root   1.4K Jul  9 12:44 ks.cfg

Add Custom Menu Item (BIOS Mode Boot)

Edit the file isolinux.cfg. In some cases you may need to enable writing privileges to the file (sudo chmod +w isolinux.cfg).

sudo vi /var/tmp/media/mydrive/isolinux/isolinux.cfg

Add just before “label linux” or anywhere else the following. A comment here notes that the volume label must coincide with the int.stage2= parameter. Changing it from the original into something else caused the installer to fail, so first let us preserve the label name.  The last line is one long line including inst.ks=cdrom: and the label below is “CentOS 7 x86_64”. Spaces must be represented by \x20. This works for amd64 systems with a BIOS.

label is
  menu label ^Kickstart
  menu default
  kernel vmlinuz
  append initrd=initrd.img inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 inst.ks=cdrom:/dev/cdrom:/ks.cfg

inst.ks=<location> points to the configuration file. Note that there are many other places you could put this file, such as on another server, but that it outside the scope of this article. The above configuration file assumes that the ks.cfg file is in mydrive. While your system my list the optical drive under /dev/sr0 you still must use “cdrom:” and “/dev/cdrom:” in the above setup.

Remove from the section label check the line menu default. (go to the line and press <Esc><d><d>). Save and exit the editor.

Add Custom Menu Item (UEFI MODE BOOT)

If you want to use UEFI mode boot and run kickstart from there, you need to edit a different menu definition file. The UEFI mode menu is defined in /var/tmp/media/mydrive/EFI/BOOT/grub.cfg and the format is different. To add a menu option that starts kickstart installation insert this:

menuentry 'CentOS 7.2 Custom Kickstart Installation' --class fedora --class gnu-linux --class gnu --class os {
        linuxefi /images/pxeboot/vmlinuz inst.stage2=hd:LABEL=CentOS\x207\x20x86_64 inst.ks=cdrom:/ks.cfg quiet
        initrdefi /images/pxeboot/initrd.img
}

To set the default you can specify it based on the order in the list of menu entries:

set default="0"

There also is an image under /var/tmp/media/mydrive/images/ called efiboot.img with containing the same files, but those did not seem used when installing from DVD ROM. However, in case you want to modify those, you can create a mounting point, say /point , and then mount the image file with:

sudo mount -t vfat -o loop ./efiboot.img /point

Then edit the files and unmount the image with:

sudo unmount /point

Build Custom ISO Image

To build a bootable ISO 9660 image we use genisoimage (or mkisofs) on Linux or CentOS:

sudo yum install -y genisoimage

We tested with v1.1.11-23. Options: -o sets the output path/filename and can be relative to current directory; -b sets the datafile for use as El Torito boot image for PC-BIOS (or use -e for EFI); -c sets the filename for the El Torito Boot Catalog file which is created automatically if boot images are set, -no-emul-boot is needed for ISOLINUX and GRUB2 boot images, -boot-load-size sets boot image 512-byte sector number to be loaded by BIOS, -boot-info-table writes table into boot image, needed for ISOLINUX and GRUB2, -V sets the Volume ID, -R enables normal Unix filenames and attributes (-r does the same but also changes access permissions to make all files available to everybody), -J enables MS-Windows Joliet extension for UCS-2 names, -v for verbose output. The last item specifies source directory. -m can be used to exclude files, and -x to exclude directories. -T creates TRANS.TBL files to aid some systems establishing correct filenames.

We change to our staging directory and build using the following two lines:


cd /var/tmp/media/mydrive
sudo genisoimage -o ../centos-7-custom.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -J -R -v -T -V 'CentOS 7 x86_64' .

That creates a conventional BIOS bootable image.

Creating a UEFI mode boot image

Creating an UEFI mode boot image is a bit more complicated, but the following works for just booting from a DVD-ROM in UEFI mode. Instead of the command in the previous section, do this instead:

cd /var/tmp/media/mydrive
sudo genisoimage -o ../centos-7-custom.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -eltorito-alt-boot -e images/efiboot.img -no-emul-boot -J -R -v -T -V 'CentOS 7 x86_64' .

Adding checksum

To create a new checksum for the custom ISO we used isomd5sum (1.0.10-5):

sudo yum install -y isomd5sum
cd /var/tmp/media/
sudo implantisomd5 centos-7-custom.iso

Check the label again to ensure it is the same as for the original ISO image’s, and make the iso accessible for remote copy. Replace <user_id> with your used id on CentOS machine.

sudo file -sL /var/tmp/media/centos-7-custom.iso
sudo chown :<user_id> /var/tmp/media/centos-7-custom.iso

The first command should produce something like this, showing the label:

/var/tmp/media/centos-7-custom.iso: # ISO 9660 CD-ROM filesystem data 'CentOS 7 x86_64' (bootable)

Custom ISO Test (VirtualBox)

Copy the iso file from your CentOS machine to the machine with VirtualBox installed on it for testing. Using secure copy command (scp) on MacOS for instance, substituting <user_id> by your id on the CentOS machine, and <ip_address> with the ip address of the CentOS machine. Enter the following on the machine you are copying to, from the directory you want to copy to.

scp <user_id>@<ip_address>:/var/tmp/media/centos-7-custom.iso .

Next, create a new VM, same configuration as the one created before configuring it as a Red Hat (64-bit) Linux machine. But now attach to the IDE controller the custom .iso file as a virtual DVD as Secondary Slave.

Start the VM. The initial installer menu should now include the Kickstart option. Select K and let the unattended install complete. Then detach the virtual DVD drive and reboot. If the installation stops you can switch to shell using <Ctrl><cmd><F2> on a MacOS keyboard.

Once this works to your satisfaction, burn your image to a DVD and you are done. On MacOS I use Disk Utility. Connect a DVD drive, put in blank disk (when Finder asks what to do with it select Ignore) and in Disk Utility select the  the DVD drive. Then select Burn, the iso image, and Burn. Congratulations. You can now delete the VMs you created, including their VDI files. Or just keep the first one as a starting point for future customization changes.

Enhancements

Instead of the Minimal image I suppose you can use other CentOS installer versions, such as DVD (4GB) or Everything (7.7GB), but have not tried it yet.

This article does not cover the addition of custom packages to your custom installation media, pre- and post- install scripts and so on. Refer to the documentation.

References

Documentation for CentOS 6 and later is here. Look for Red Hat Enterprise, of the same version number. Information specifically on Kickstart is here. A discussion about creating EUFI bootable media more generally is here and for making USB stick bootable media here. UEFI grub parameter docs. For fixing any issues with CentOS networking you can look here.

updated: 20170309, 20170318; 20170606; 20170709; 20170719; 20170720; 20181213
photo: CentOS.org wiki

6 thoughts on “Unattended Kickstart Installation of CentOS 7 that Works

  1. zapya for pc Reply

    I like the valuable info you provide for your articles. I will bookmark
    your blog and check again right here frequently. I am slightly sure I will be told plenty of new stuff right right here!
    Best of luck for the following!

    • Ideanist_M Post authorReply

      That may be true for the “Minimal” installer but the procedure works also with other installers, such as the “DVD” or “Everything” versions, which are more complete.

  2. Fauxsys Reply

    Thanks for this writeup!

    Instructions to make bootable ISO on Mac (tested!):
    #Install homebrew from brew.sh
    $ /usr/bin/ruby -e “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”

    #Install cdrtools, with contains mkisofs
    $ brew install cdrtools

    #Build Custom ISO Image
    $ mkisofs -o ../centos-7-custom.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -J -R -v -T -V ‘CentOS 7 x86_64’ .

  3. Emmanuel Reply

    This is very good. Tested this out and it worked very well. Thank you for a very clear and concise instructions.

  4. Jared Reply

    Thank you for this very well organized and comprehensive guide. I’ve been struggling to get my custom iso with a kickstart file to work correctly but when I found this guide, I followed it exactly and it worked! Thanks so much!

Leave a Reply

Your email address will not be published. Required fields are marked *