This script is used for creating a VM based on libvirtd. Your system will be checked for availability for running virtualization with the following prompts:
- Checking the compatibility system for serving virtualization
- Checking and installing the libvirtd dependency packages
- Checking the compatibility of your system with nested virtualization
- Make sure the 'workdir' directory is ready
After checking is finished, then you must fill out several questions below:
- Give a name for your VM
- How much memory
- Allocate several cores of CPU
- Select a base image and OS
- Create a primary disk for the VM
- Determine many peripheral disks for the VM
- Select a virtual network
- Specify IPv4 for the VM
- Configure a user, password, and key to access your VM
- Validate if the VM is successfully created or not
Attention please, this script currently only supports running on the Ubuntu base system. Next to do:
- Ubuntu 20 or later
- Debian
- Rhel
- CentOS
- Alpine
Don't worry if you are running this script. Your system will install the following dependency packages while running this script:
- cpu-checker
- ipcalc
- whois
- qemu-system
- libvirt-daemon-system
- virtinst
- libosinfo-bin
- If you want to running this script rootless. You must uncomment the
#unix_sock_group = "libvirt"parameter in the/etc/libvirt/libvirtd.conffile
sed -i 's/^#unix_sock_group = "libvirt"/unix_sock_group = "libvirt"/g' /etc/libvirt/libvirtd.conf- Suppose you encounter a problem like this
The solutions is add the uri_default = "qemu:///system" parameter in the .config/libvirt/libvirt.conf file and allow all permissions to the libvirt-sock file. Here is the reference
cat << EOF > .config/libvirt/libvirt.conf
uri_default = "qemu:///system"
EOFsudo chmod 777 /var/run/libvirt/libvirt-sock- Here is the example xml if you don't have a virtual network. You can customize as you wish. Click this reference
<network>
<name>default</name>
<bridge name='virbr0'/>
<forward/>
<ip address='192.168.1.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.1.2' end='192.168.1.254'/>
</dhcp>
</ip>
</network>- Allow all permissions and change owner the
workdirandimagesdirectory to avoid script can't cloning base image.
- The reasons is on bellow line in script
qemu-img create -q -b "${src_dir}/${src_img}" -f qcow2 -F qcow2 "${dst_path}" "${vm_disk1_size}"G- This is way to allow all permissions to
workdirandimagesdirectory
sudo chmod -R 777 /var/lib/libvirt/workdir/sudo chmod -R 777 /var/lib/libvirt/images/- This is way to change owner the
workdirandimagesdirectory
sudo chown -R libvirt-qemu:kvm /var/lib/libvirt/workdir/sudo chown -R libvirt-qemu:kvm /var/lib/libvirt/images/- Clone this repository
git clone https://github.com/uvewexyz/create-vm-automatically.git- Give the execute permissions to the
virtui.shscript
cd ~/create-vm-automaticallychmod +x virtui.sh && ls -l virtui.sh- Before running the
virtui.shscript. These are recommended OS image, if you want to install. Follow bellowing step by step:
- Move the current/working directory
cd /var/lib/libvirt/workdir/- Installing
almalinux9image
wget https://repo.almalinux.org/almalinux/9/cloud/x86_64/images/AlmaLinux-9-GenericCloud-latest.x86_64.qcow2- Installing
centos-stream9image
wget https://cloud.centos.org/altarch/9-stream/x86_64/images/CentOS-Stream-GenericCloud-9-latest.x86_64.qcow2- Installing
alpine-virt-3.21image
wget https://dl-cdn.alpinelinux.org/alpine/v3.21/releases/x86_64/alpine-virt-3.21.3-x86_64.isoqemu-img convert -f raw -O qcow2 alpine-virt-3.21.3-x86_64.iso alpine-virt-3.21.3-x86_64.qcow2- Installing
ubuntu22.04image
wget https://cloud-images.ubuntu.com/jammy/current/jammy-server-cloudimg-amd64.img- Installing
ubuntu20.04image
wget https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img- Installing, extract, and convert extension to qcow2 the
openwrtimage
wget https://downloads.openwrt.org/releases/24.10.1/targets/x86/generic/openwrt-24.10.1-x86-generic-generic-ext4-combined.img.gzgunzip openwrt-24.10.1-x86-generic-generic-ext4-combined.img.gzqemu-img convert -f raw -O qcow2 openwrt-24.10.1-x86-generic-generic-ext4-combined.img openwrt-24.10.1-x86-generic-generic-ext4-combined.qcow2- Run the script bellow in this way
./virtui.sh- Easily to create or change user password, run the command in bellow
echo "user1:Str0ngPass!" | chpasswd- Demonstration
| Legend | Colour |
|---|---|
| INFO | |
| SUCCESS | |
| FAIL | |
| TIPS |
- https://linuxize.com/post/how-to-install-kvm-on-ubuntu-20-04
- https://www.freecodecamp.org/news/turn-ubuntu-2404-into-a-kvm-hypervisor
- https://serverfault.com/questions/803283/how-do-i-list-virsh-networks-without-sudo#:~:text=17,to%20espicify%20%2D%2Dconnect
- https://blog.wikichoon.com/2016/01/qemusystem-vs-qemusession.html?utm_source=chatgpt.com
- https://libvirt.org/drvqemu.html#driver-instances
- https://libvirt.org/formatnetwork.html#routed-network-config
- https://libvirt.org/formatnetwork.html#example-configuration

