Saturday, 17 September 2022

Launching RHEL 8 Cloud Image on Libvirt

Launching RHEL 8 Cloud Image on Libvirt

Bring the speed of provisioning from pre-built cloud images to your Libvirt/KVM hypervisor.

Go here: https://access.redhat.com/downloads/content/479/ver=/rhel—8/8.6/x86_64/product-software and download Red Hat Enterprise Linux 8.6 KVM Guest Image. It is 794MB.

Execute the following and you will be prompted for a password to turn into a SHA512 password hash, if you want to use passwords instead of an SSH key.

python -c 'import crypt,getpass; print(crypt.crypt(getpass.getpass(), crypt.mksalt(crypt.METHOD_SHA512)))'

Create a cloud-init Configuration YAML file. The first line must start with “#cloud-config”.

vim config.yaml

(Note that the default user “cloud-user” and SSH password authentication is disabled in the example below.)

#cloud-config  
#password: 1800redhat
#chpasswd: { expire: False }  
ssh_pwauth: False  
hostname: rhel86.the.lab  
#package_upgrade: true  
users:  
- name: spud
groups: wheel  
lock_passwd: false  
passwd: <password_hash>  
shell: /bin/bash  
sudo: ['ALL=(ALL) NOPASSWD:ALL']  
ssh-authorized-keys:  
- <ssh_public_key>

Build the cloud-init ISO image. At the time of writing there is no official cloud-utils package for RHEL 8 so recommend borrowing/installing from Fedora 36 if you are using RHEL 8.

dnf install https://download-ib01.fedoraproject.org/pub/fedora/linux/releases/36/Everything/x86_64/os/Packages/c/cloud-utils-0.31-10.fc36.noarch.rpm

dnf install cloud-utils
# OR the above URL for RHEL 8.

cloud-localds config.iso config.yaml

Clone the RHEL 8 cloud image to create a disk for your VM. Move/copy the ISO as well to a storage location libvirtd can access:

sudo install --owner qemu --group qemu --mode 0600 rhel-8.0-x86_64-kvm.qcow2 /var/lib/libvirt/images/rhel86-vm1.qcow2
sudo install --owner qemu --group qemu --mode 0400 config.iso /var/lib/libvirt/images/rhel86-vm1.iso

Provision the VM.

virt-install --memory 4096 --vcpus 1 --name rhel8-vm1 --disk /var/lib/libvirt/images/rhel86-vm1.qcow2,device=disk --disk /var/lib/libvirt/images/rhel86-vm1.iso,device=cdrom --os-type Linux --os-variant rhel8.6 --virt-type kvm --graphics none --import --network bridge=virbr0 --memballoon driver.iommu=on --rng /dev/random,driver.iommu=on

virt-install automatically attaches to the serial console of the VM so you can see it booting. To exit the console use <Ctrl>+<]>, just like telnet from the olden days.

Login with the user and password combination: cloud-user / 1800redhat

Red Hat Enterprise Linux 8.6 (Ootpa) 
Kernel 4.18.0-372.9.1.el8.x86_64 on an x86_64 

Activate the web console with: systemctl enable --now cockpit.socket 
localhost login: cloud-user 
Password: 
Last login: Fri Sep 16 20:21:18 on ttyS0 

# Switch to root, enable cockpit and determine the ip address of the VM, (eg. 192.168.122.227) 

[cloud-user@localhost ~]$ sudo -i 
[root@localhost ~]# systemctl enable --now cockpit.socket 
Created symlink /etc/systemd/system/sockets.target.wants/cockpit.socket → /usr/lib/systemd/system/cockpit.socket. 
[root@localhost ~]# ip a show eth0 
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 
    link/ether 52:54:00:a2:60:ee brd ff:ff:ff:ff:ff:ff 
    inet 192.168.122.227/24 brd 192.168.122.255 scope global dynamic noprefixroute eth0 
       valid_lft 2771sec preferred_lft 2771sec 
    inet6 fe80::e2bd:c725:232b:fd0e/64 scope link noprefixroute
       valid_lft forever preferred_lft forever 

In a web browser on your laptop go to: https://<ipaddress>:9090/
In the example above the IP address is on the “inet” line and is 192.168.122.227 (leave out the “/24” which signifies the netmask and is not part of the IP address)

Login again with: cloud-user / 1800redhat

To exit that text console that “virt-install” put you into press the keys <Ctrl>+<]>

You can ssh to the VM using: ssh cloud-user@<ip_address>

Change the VM’s hostname if necessary:
hostnamctl set-hostname <new_hostname>

Shutdown the VM to commence resizing the disk to something bigger than the default 10GB.
shutdown now

On your hypervisor enlarge the virtual disk. The example below makes the disk 150GB:
qemu-img resize /var/lib/libvirt/images/rhel86-vm1.qcow2 150G

Start the VM:
virsh start rhel8-vm1

At this stage cockpit does not have the storage plugin installed so CLI it is:

$ ssh cloud-user@192.168.122.227
[cloud-user@localhost ~]$ lsblk
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
sr0     11:0    1   366K  0 rom  
vda    252:0    0   150G  0 disk 
├─vda1 252:1    0     1M  0 part 
├─vda2 252:2    0   100M  0 part /boot/efi
└─vda3 252:3    0 149.9G  0 part /
[cloud-user@localhost ~]$ df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        7.7G     0  7.7G   0% /dev
tmpfs           7.8G     0  7.8G   0% /dev/shm
tmpfs           7.8G   17M  7.7G   1% /run
tmpfs           7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/vda3       150G  3.0G  147G   2% /
/dev/vda2       100M  5.8M   95M   6% /boot/efi
tmpfs           1.6G     0  1.6G   0% /run/user/1000

Note that cloud-init automatically resized the partition /dev/vda3 and root / filesystem to use all of the new storage automatically on boot.

Written with StackEdit.

No comments:

Post a Comment