Get Rewarded! We will reward you with up to €50 credit on your account for every tutorial that you write and we publish!

Create and install a custom CentOS 8 image

profile picture
Author
Yuri Moens
Published
2019-10-29
Time to read
5 minutes reading time

Introduction

We'll be preparing a CentOS 8 image on a local VM so we can roll out our own custom CentOS 8 image on Hetzner boxes.

Prerequisites

You will need a virtualisation tool to install CentOS 8. I used GNOME Boxes, a popular alternative would be VirtualBox. I'm assuming all the commands in the VM are run as root.

Step 1 - Install CentOS 8 in a VM

You can download the install image from any of the mirrors: CentOS 8 or CentOS 8 Stream. I strongly recommend getting the much smaller "boot" version.

Settings for the VM box don't matter. Just install it as you normally would.

Step 2 - Preparing the CentOS 8 image

To make the CentOS 8 image work with the installimage script we'll need to make some preparations first.

Step 2.1 - Install systemd with systemd-networkd support

systemd in CentOS 8 is compiled without systemd-networkd support so you'll have to compile it yourself or get it compiled from an unofficial repo. The latter is much easier so we're doing that.

Create /etc/yum.repos.d/systemd-networkd.repo with the following content:

[systemd-networkd]
name=systemd-networkd
baseurl=https://people.redhat.com/~jsynacek/systemd/systemd-rebuild-networkd/rhel-8.dev-x86_64/
gpgcheck=0
enabled =0

This repo contains an older version of systemd so we'll need to downgrade rather than install.

yum --enablerepo=systemd-networkd -y downgrade systemd

The Hetzner installimage script looks for dracut in /sbin. CentOS 8, however, installs this in /usr/bin so we should symlink it.

ln -s /usr/bin/dracut /sbin/dracut

Step 2.3 - Install mdraid module

The Hetzner installimage script uses the mdraid module which is not installed with a minimal CentOS 8 install so let's install it.

yum -y install libblockdev-mdraid

Step 2.4 - Set SELinux to permissive

We're setting it to permissive to avoid headaches at the first boot of our Hetzner box. We'll be setting it back to enforcing later on.

Set the following in /etc/sysconfig/selinux

SELINUX=permissive

Step 2.5 - Configure/install additional things (optional)

At this point you can configure your install any way you like. If you want your base image to have vim for example, you can install it now.

Step 2.6 - Create a compressed tar image of your box

If you did a bare minimum install you may still need to install tar

yum -y install tar

Create an archive of the entire file system, minus /dev, /proc and /sys.

The filename of the archive gets parsed by the installimage script so it should be of the format CentOS-version-arch-optional.tar.gz.

cd /
tar zcvf CentOS-80-64-boot.tar.gz /bin /boot /etc /home /lib /lib64 /media /mnt /opt /root /run /sbin /srv /tmp /usr /var

Step 3 - Installing the image

Now that we have our image we'll finally be installing it on our Hetzner VM. Start of by rebooting your Hetzner VM into rescue mode through the Hetzner cloud dashboard.

Transfer the image from your local CentOS 8 install to your VM in rescue mode. We'll be using scp for this so from your local CentOS 8 box, execute

scp /CentOS-80-64-boot.tar.gz root@<hetzner-vm-ip>:/root

Now on your Hetzner VM in rescue mode, start the installimage script interactively.

installimage

On the image selection screen, choose the Custom image option and you'll get dropped in an editor. You can configure several options of your VM here, like partitioning or your hostname. Do as you please.

At the very end of this file is an IMAGE option. Simply add the path to your image here, press F10 to exit, save when prompted and your VM will be installed.

IMAGE /root/CentOS-80-64-boot.tar.gz

Step 4 - Cleanup (optional)

This step is optional but highly advised. CentOS 8 doesn't come with systemd-networkd support and because we only needed this for installimage to work, I recommend you to reinstall the systemd version from the BaseOS repo. Remove the systemd-networkd repo and install systemd again.

rm -rf /etc/yum.repos.d/systemd-networkd
yum -y install systemd

We should also set SELinux to enforcing again. First, let's relabel the filesystem.

touch /.autorelabel
reboot

Now set SELinux back to enforcing in /etc/sysconfig/selinux and reboot to check if everything's working.

SELINUX=enforcing

Conclusion

You now know how to create a custom (CentOS 8) image you can use to install with the installimage script provided by Hetzner and install said image.

License: MIT
Want to contribute?

Get Rewarded: Get up to €50 in credit! Be a part of the community and contribute. Do it for the money. Do it for the bragging rights. And do it to teach others!

Report Issue

Discover our

Dedicated Servers

Configure your dream server. Top performance with an excellent connection at an unbeatable price!

Want to contribute?

Get Rewarded: Get up to €50 credit on your account for every tutorial you write and we publish!

Find out more