Example: CentOS Stream image

This example shows you how to install a CentOS Stream image and focuses mainly on CentOS Stream 9. Because the CentOS installation process might differ across versions, the installation steps might differ if you use a different version of CentOS.

Bemerkung

This is just an example, adjust paths and commands according to your environment

Laden Sie ein CentOS-Installations-ISO herunter

  • Navigate to the CentOS mirrors page.

  • Choose one of the mirrors and navigate to 9-stream/BaseOS/x86_64/iso. Download a ISO, choose boot to download packages during install, otherwise choose dvd

Starten Sie den Installationsvorgang

Start the installation process using either the virt-manager or the virt-install command as described in the Tools: libvirt and virsh/virt-manager page.

virt-install

If you use the virt-install command, do not forget to connect your VNC client to the virtual machine.

The command should look something like this:

$ sudo virt-install --virt-type kvm --name my-centos --ram 2048 \
  --network network=default \
  --graphics vnc,listen=0.0.0.0 --noautoconsole \
  --os-type=linux --os-variant=centos-stream9 \
  --location=~/Downloads/CentOS-Stream-9-20240819.0-x86_64-dvd1.iso

virt-manager

Bemerkung

See here for libvirt wiki about new VM creation in virt-manager

When creating a new VM from the downloaded ISO, virt-manager should automatically detect the OS. If it fails, manually select CentOS Stream as OS. Default settings should be fine.

Schritt durch die Installation

Bemerkung

This guide focuses on the steps specific to create a OpenStack image, for a general overview of installation process see CentOS Documentation

In Installation Summary follow the instructions below.

DHCP and hostname

In Network & Host Name ensure that Ethernet is on and in Configure.../IPv4 Settings the Method is set to Automatic (DHCP). The same page allows for host name selection - leave it to default as the cloud-init package will be installed later.

Wählen Sie die Installationsoption

In Software Selection choose what to install, the default is Server with GUI, the smallest choice is Minimal Install.

Create a working user

Configure a root password in Root Password, as this will be needed later to finalize the installation. By default it will be then blocked by cloud-init. It is also possible to create an adiministrator user, as it will be later deleted by virt-sysprep.

Trennen Sie die CD-ROM und starten Sie neu

virt-install

Zum Auswerfen eines Datenträgers mit dem Befehl :comand: virsh erfordert libvirt, dass Sie eine leere Disk an derselben Stelle anhängen, auf der die CD-ROM zuvor angeschlossen war, was möglicherweise` hda` ist. Sie können das entsprechende Ziel mit dem Befehl :command:‘virsh dumpxml vm-image‘ bestätigen.

$ sudo virsh dumpxml my-centos
<domain type='kvm' id='19'>
  <name>centos</name>
...
    <disk type='block' device='cdrom'>
      <driver name='qemu' type='raw'/>
      <target dev='hda' bus='ide'/>
      <readonly/>
      <address type='drive' controller='0' bus='1' target='0' unit='0'/>
    </disk>
...
</domain>

Run the following commands from the host to eject the disk and reboot using virsh, as root.

$ sudo virsh attach-disk --type cdrom --mode readonly my-centos "" hda
$ sudo virsh reboot my-centos

virt-manager

If you are using virt-manager, the commands above will work, but you can also use the GUI to detach and reboot.

Finalize installation

Installiere den ACPI-Dienst

Um den Hypervisor zum Neustarten oder Herunterfahren einer Instanz zu aktivieren, müssen Sie den acpid Service auf dem Gast-System installieren und ausführen.

Log in to the CentOS guest and run the following commands to install the ACPI service and configure it to start when the system boots:

# dnf install acpid
# systemctl enable acpid

Konfigurieren, um Metadaten abzurufen

Bemerkung

Check cloud-init documentation for more information

An instance must interact with the metadata service to perform several tasks on start up. For example, the instance must get the ssh public key and run the user data script. To ensure that the instance performs these tasks, install the cloud-init utility.

Das cloud-init-Paket holt automatisch den öffentlichen Schlüssel vom Metadatenserver und platziert den Schlüssel in ein Konto. Installieren Sie `` cloud-init`` innerhalb des CentOS-Gastes, indem Sie:

# dnf install cloud-init

The account varies by distribution. On CentOS Stream virtual machines, the account is called cloud-user.

Sie können den Namen des von cloud-init` verwendeten Kontos ändern, indem Sie die Datei ``/etc/cloud/cloud.cfg bearbeiten und eine Zeile mit einem anderen Benutzer hinzufügen. Um beispielsweise cloud-init zu konfigurieren, um den Schlüssel in ein Konto mit dem Namen admin zu setzen, verwenden Sie die folgende Syntax in der Konfigurationsdatei:

users:
  - name: admin
    (...)

Cloud-init alternatives

Warnung

This method is not recommended as only gets the ssh public key from the metadata server. It does not get user data, which is optional data that can be passed by the user when requesting a new instance. User data is often used to run a custom script when an instance boots.

Wenn Sie das cloud-init-Paket nicht in Ihrem Abbild installieren können, um den ssh-public Key abzurufen und dem Root-Account hinzuzufügen, bearbeiten Sie das /etc/rc.d/rc.local Datei und fügen Sie die folgenden Zeilen vor der Zeile touch /var/lock/subsys/local:

if [ ! -d /root/.ssh ]; then
  mkdir -p /root/.ssh
  chmod 700 /root/.ssh
fi

# Fetch public key using HTTP
ATTEMPTS=30
FAILED=0
while [ ! -f /root/.ssh/authorized_keys ]; do
  curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key \
    > /tmp/metadata-key 2>/dev/null
  if [ \$? -eq 0 ]; then
    cat /tmp/metadata-key >> /root/.ssh/authorized_keys
    chmod 0600 /root/.ssh/authorized_keys
    restorecon /root/.ssh/authorized_keys
    rm -f /tmp/metadata-key
    echo "Successfully retrieved public key from instance metadata"
    echo "*****************"
    echo "AUTHORIZED KEYS"
    echo "*****************"
    cat /root/.ssh/authorized_keys
    echo "*****************"
  fi
done

Bemerkung

Einige VNC-Clients ersetzen den Doppelpunkt (:) mit einem Semikolon (;) und den Unterstrich (_) mit einem Bindestrich (-). Stellen Sie sicher, dass http: und nicht http; heisst. Achten Sie darauf, authorized_keys und nicht authorized-keys zu spezifizieren.

Bemerkung

With this method, previous CentOS versions needed to disable the zeroconf route, this is not needed as of EL8.

Allow partitions to resize

Damit die Root-Partition richtig vergrössert werden kann, installieren Sie das cloud-utils-growpart-Paket, das die richtigen Tools enthält, damit der Datenträger die Größe von cloud-init ändern kann.

# dnf install cloud-utils-growpart

Konfigurieren der Konsole

Warnung

Actually this does not work even following https://access.redhat.com/solutions/3443551

For the openstack console log command to work properly, instance must be configured to sent output to a serial console.

  1. Edit the /etc/default/grub file and append console=tty0 console=ttyS0,115200n8 to GRUB_CMDLINE_LINUX.

    Beispielsweise:

    ...
    GRUB_CMDLINE_LINUX="resume=/dev/mapper/cs-swap rd.lvm.lv=cs/root rd.lvm.lv=cs/swap console=tty0 console=ttyS0,115200n8"
    
  2. Then regenerate GRUB configuration:

    # grub2-mkconfig -o /boot/grub2/grub.cfg --update-bls-cmdline
    

Customize

If you are building an image manually, you are probably interested in customizing it, now it’s time to apply your edits.

Schalten Sie die Instanz aus

Shutdown the VM:

# shutdown now

Clean up

The image needs to be cleaned up of details such as the MAC address - the tool used is virt-sysprep, part of libguestfs

sudo virt-sysprep -d my-centos

Resize the image

Resize and compress the image to remove unused space (the image will then use available space after initialization) using virt-sparsify, part of libguestfs

sudo virt-sparsify --compress /var/lib/libvirt/images/my-centos.qcow2 ~/upload.qcow2

Abbild ist komplett

The resized image is now ready to be uploaded using openstack image create. For more information, see the python-openstackclient command list.

Undefinieren Sie die libvirt Domain

Now that you can upload the image to the Image service, you no longer need to have this virtual machine image managed by libvirt.

$ sudo virsh undefine my-centos

Or if you used virt-manager, delete it through the GUI.