Autoyast: Partitioning that works both on real hardware and inside virtual machines

Continuing a small series on Autoyast tips and tricks after the first post, Retaining/Reusing existing partitions, let’s look at another use case for dynamically modifying the Autoyast profile.

I know, I know, all the world is talking cloud and Kubernetes or serverless and youknownot, but fact is, someone still has to run bare metal, real hardware. And if that party is Amazon and otherwise a NAS at home works fine for you, congratulations, but I still like to run my own hardware. I do like to install it in an automated fashion in spite of a scale of n=1 and I like to test this installation inside a KVM VM before I throw it at bare metal.

The thing is that I do want to use virtio, of course, but then I get different block device names (/dev/vd*) than on real hardware (/dev/sd*). Autoyast does not force us to tell it a concrete device name, e.g. /dev/sda, but then you leave it to Yast to pick the drive to apply the partitioning to and that can or can not end up well. It will probably work if you have a single drive only but suppose you have multiple drives?

So maybe we do want to explicitly specify device names and if for simplicity we can assume that on the real hardware /dev/sda is e.g. always the SSD to install the system on and e.g. /dev/sdb and /dev/sdc are two hard drives to be assembled as a RAID-1 for data storage, we can inside our VM define our hard disks the same way, i.e. /dev/vda should be the drive to install on. In other words, we want the Autoyast control file to dynamically use /dev/vda if run inside the VM and /dev/sda otherwise.

So how do we find out if we’re run inside a VM? Well, we don’t. We’ll just check for the existance of /dev/vda and otherwise just use /dev/sda. Why make things harder than needed?

Let’s again assume an Autoyast control file partitioning fragment such as the following:

    <partitions config:type="list">
      <!-- Partition 1 (EFI system partition) automatically created by Yast -->
        <partition_nr config:type="integer">2</partition_nr>
        <create config:type="boolean">false</create>
        <format config:type="boolean">true</format>
        <filesystem config:type="symbol">ext4</filesystem>
      <!-- Other partitions follow here... -->

Then we can again employ a pre-install script such as the following:

  <pre-scripts config:type="list">
      <notification>Examining hard disks...</notification>
cp /tmp/profile/autoinst.xml /tmp/profile/modified.xml

[ -b /dev/vda ] && SYSDISK="/dev/vda" || SYSDISK="/dev/sda"
sed -i "s,@SYSDISK@,/dev/${SYSDISK},g" /tmp/profile/modified.xml

This will, of course, automagically also work if by accident you added a SATA disk to the VM instead of a Virtio one. If you forgot to define a hard disk at all, Autoyast will eventually complain later on, but maybe you may want to catch such errors beforehand. If so, the next post in this series on error reporting could be something for you.