Linux 4.15.0-191-generic #202-Ubuntu SMP Thu Aug 4 01:49:29 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
Sources: https://www.linuxtechi.com/create-manage-kvm-virtual-machine-cli/ https://serverfault.com/questions/760225/kvm-qemu-connect-to-vm-without-gui
These packages need to be installed
sudo apt install -y qemu qemu-kvm libvirt-daemon libvirt-clients bridge-utils virt-manager
Make sure the libvirtd service is running
sudo systemctl status libvirtd.service
<network>
<name>Secure</name>
<forward mode='nat'>
<nat>
<port start='1024' end='65535'/>
</nat>
</forward>
<bridge name='virbr3' stp='on' delay='0'/>
<mac address='52:54:00:57:64:f4'/>
<ip address='10.120.116.1' netmask='255.255.255.0'>
<dhcp>
<range start='10.120.116.10' end='10.120.116.19'/>
</dhcp>
</ip>
</network>
Assume this file is called net-config.xml for the following command
net-config.xml
is defined in the example definition file above.
virsh net-define net-config.xml
virsh net-list --all
virsh net-start Secure
virsh net-autostart Secure
If you want your VM to be connected directly to the physical LAN, the setup will depend on your Linux distribution. For Ubuntu 18.04, first you can setup a bridge using the brctl
utility, located in the bridge-utils
package. Simply run brctl addbr bridge0
to create the bridge (in this case, bridge0
), then brctl addif bridge0 en0
to add a physical interface (e.g. en0
) to the bridge. Finally, to make the change permanent, we can alter the networking configuration of the machine. For Ubuntu 18.04, you might need to use netplan
. Netplan uses YAML files, located at /lib/netplan
, /etc/netplan
, and /run/netplan
to configure network devices, including bridges.
network:
bridges:
bridge0:
interfaces: [ en0 ]
dhcp4: true
version: 2
Above, bridge0
can be any name (here I used bridge0
to conform to the example in the previous paragraph). en0
is the physical adapter the virtual bridge is connected to (again, as per the example in the previous paragraph). The file will need to have the .yaml
extension. To complete the addition, run netplan apply
as a superuser.
Source: https://wiki.debian.org/BridgeNetworkConnections
virt-install --connect qemu:///system --name "VMName" --memory 4096 --cdrom /home/joesmith/ --disk size=50 --noautoconsole --os-type windows --accelerate --network=bridge:virbr0 --hvm --graphics vnc,port=5900,listen=127.0.0.1
The VM host will be running VNC for the VM on localhost TCP 5900. If you don't have a Desktop environment installed on the host, you can still connect to the VM by SSH tunnelling into the host and using a VNC viewer, like TigerVNC. Simply run the following on the remote machine (assume the VM host is 10.10.10.10, and the user on 10.10.10.10 is joesmith). This command will forward traffic from localhost:5900 to the remote (10.10.10.10) loopback (aka localhost) interface, TCP 5900. The -T
argument will setup the SSH tunnel but not allocate a terminal shell and &
backgrounds (or jobifies) the command so that the terminal can be reused for other commands. Simply take parts of the command out if you want to have a shell AND a tunnel simultaneously.
ssh -T -L localhost:5900:localhost:5900 [email protected] &
Note: If used TigerVNC to connect to the VM, use F8 inside TigerVNC to bring up the context menu and send Ctrl-Alt-Del to the VM
If you want to import VM export files, you will need the libguestfs-tools package
sudo apt install libguestfs-tools_1.36.13-1ubuntu3_amd64
After installing the above, you can convert an OVA to a QCOW2 like this. Assume you're converting machine1.ova to machine1.qcow.
sudo virt-v2v -i ova machine1.ova -o libvirt -of qcow2
Default location of VM hard drives
/var/lib/libvirt/images
List VMs defined
virsh list --all
Check the configuration of a machine (use the VM/domain name from the command above)
virsh dumpxml machinename
Had trouble converting machine2.ova? Assume it is located in /home/joesmith/machine2.ova. The virt-install
attaches the machine to virbr1 in bridged mode
mkdir ~/someVM; cd ~/someVM
tar xvf '/home/joesmith/machine2.ova'
qemu-img convert -O qcow2 'machine2.vmdk' 'machine2.qcow2'
sudo mv machine2.qcow2 /var/lib/libvirt/images
virt-install --connect qemu:///system --name machine2 --memory 2048 --disk "/var/lib/libvirt/images/machine2.qcow2" --import --noautoconsole --vcpus 1 --accelerate --network=bridge:virbr1 --hvm --graphics vnc,port=5900,listen=127.0.0.1
osinfo-query is a command that was suggested for virt-install --os-type flag, but I wasn't able to figure out a value to supply for the above install to satisfy virt-install
Install osinfo-query by installing the libosinfo-bin package.
sudo apt install libosinfo-bin
Create a snapshot with a description `virsh snapshot-create-as DC01 LabSetupComplete "Lab setup completed. Use this snapshot to revert lab to original state if needed."
Create a snapshot then edit the name. For the virsh snapshot-edit
command, a text editor will open.
virsh snapshot-create DC01
virsh snapshot-edit DC01 --current --rename