Create a QEMU/KVM Virtual Machine from the Command Line
You can use a combination of command line tools to create and configure a virtual machine. Here we will use few tools from the QEMU and libvirt packages to do this.
Use QEMU to create a 15GB QCOW disk image:
$ qemu-img create -f qcow2 /home/user/KVM/CentOS-Stream-9.qcow2 15G
Formatting '/home/user/KVM/CentOS-Stream-9.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=16106127360 lazy_refcounts=off refcount_bits=16
Start the installation:
$ sudo virt-install --name=CentOS-Stream-9 --vcpus=1 --memory=1024 --location=/home/user/Downloads/CentOS-Stream-9-20230704.1-x86_64-boot.iso --os-variant=centos8 --network bridge:virbr0 --disk path=/home/user/KVM/CentOS-Stream-9.qcow2 --disk size=15
Password:
WARNING Requested memory 1024 MiB is less than the recommended 1536 MiB for OS centos8
Starting install...
Retrieving 'vmlinuz' | 0 B 00:00:00 ...
Retrieving 'initrd.img' | 0 B 00:00:00 ...
Allocating 'CentOS-Stream-9.qcow2' | 2.5 MB 00:00:03 ...
WARNING Overriding memory to 3072 MiB needed for centos8 network install.
Creating domain... | 0 B 00:00:00
Running graphical console command: virt-viewer --connect qemu:///system --wait CentOS-Stream-9
Once this gets to this point another window should open up by the
virt-viewer
application (which you should install if you
didn’t already) and you can complete the installation and reboot. You’ll
use the same virt-viewer
window to use the shell or desktop
(depending upon the distro you installed).
When you are done and close the virt-viewer
GUI you can
find the VM in the running state using virsh
:
$ sudo virsh list --all
Password:
Id Name State
------------------------------------
5 CentOS-Stream-9 running
- slackware-current shut off
Then you can shut it down:
$ sudo virsh shutdown 5
Password:
Domain '5' is being shutdown
$ sudo virsh list --all
Password:
Id Name State
------------------------------------
- CentOS-Stream-9 shut off
- slackware-current shut off
Optionally, you can delete the VM domain and the QCOW file using the
undefine
parameter:
$ sudo virsh undefine --domain CentOS-Stream-9 --remove-all-storage
Password:
Domain 'CentOS-Stream-9' has been undefined
Volume 'vda'(/home/user/KVM/CentOS-Stream-9.qcow2) removed.