Skip to content

Instantly share code, notes, and snippets.

@CJCShadowsan
Created August 8, 2024 14:14
Show Gist options
  • Select an option

  • Save CJCShadowsan/94076e9294e72e283848464520526502 to your computer and use it in GitHub Desktop.

Select an option

Save CJCShadowsan/94076e9294e72e283848464520526502 to your computer and use it in GitHub Desktop.
Install KVM and Libvirt on Rocky Linux 9 with bridge networking
#!/bin/bash
## Prerequisites:
# - Rocky Linux 9 install
# - Sudo user setup
## Check virtualisation enabled
cat /proc/cpuinfo | egrep "vmx|svm"
## Ensure up-to-date
sudo dnf upgrade --refresh
## Enable CRB
sudo dnf config-manager --set-enabled crb
## Enable EPEL
sudo dnf install \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm \
https://dl.fedoraproject.org/pub/epel/epel-next-release-latest-9.noarch.rpm
## Install kvm, libvirt, tools to manage bridges and VMs
sudo dnf install qemu-kvm virt-manager libvirt virt-install virt-viewer bridge-utils virt-top libguestfs-tools -y
## Check modules installed
lsmod | grep kvm
## Start and enable libvirtd
sudo systemctl start libvirtd
sudo systemctl enable --now libvirtd
## Check libvirtd is running
sudo systemctl status libvirtd
## Create bridge interface
### Check current interfaces
sudo nmcli connection show
## Change ownership to prevent having to create VMs as root
sudo chown -R $USER:libvirt /var/lib/libvirt/
## Download Ubuntu ISO:
cd /home/$USER
wget https://releases.ubuntu.com/jammy/ubuntu-22.04.4-desktop-amd64.iso
## Create VMs (example below)
virt-install --name Ubuntu --ram 2048 --vcpus 2 --disk path=/var/lib/libvirt/images/ubuntu-22.04.img,size=15 --os-variant ubuntu22.04 --network bridge=virbr0,model=virtio --graphics vnc,listen=0.0.0.0 --console pty,target_type=serial --cdrom /home/$USER/ubuntu-22.04.4-desktop-amd64.iso
## Attach to VMs with virt-viewer with:
virt-viewer --connect qemu:///session --wait Ubuntu
## Profit!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment