Notes about the preparation, configuration and use of virtual machines.
Useful sites for downloading qcow2 images:
- Create a
bridged-network.xml
configuration file:
<network>
<name>bridged-network</name>
<forward mode="bridge" />
<bridge name="br0" />
</network>
- Define as virtual network:
$ sudo virsh net-define bridged-network.xml
- Activate and set as auto-started:
$ sudo virsh net-start bridged-network
$ sudo virsh net-autostart bridged-network
- Verify
$ sudo virsh net-list
Name State Autostart Persistent
----------------------------------------------------
bridged-network active yes yes
default active yes yes
- Create/Import VM with
bridged-network
as network:
$ sudo virt-install --name vmlab --memory 4096 --vcpus 2 --disk /var/lib/libvirt/images/vmlab.qcow2 \
--import --os-variant centos8 --network network=bridged-network --noautoconsole
- Check image info
qemu-img info image.qcow2
- Resize image
qemu-img resize image.qcow2 +20G
- To resize the underlying filesystems, we need to make a copy of the image, since virt-resize does not resize disk images in-place
cp image.qcow2 image-orig.qcow2
NOTE: In this example /dev/vda3 is not the /boot partition. So be careful you are growing the correct partitions on your qcow.
- Check the partitions to ensure the filesystem to be grown
virt-filesystems --long -h --all -a image.qcow2
- Grow
/dev/vda3
virt-resize --expand /dev/vda3 image-orig.qcow2 image.qcow2
- Inspect new disk
qemu-img info image.qcow2
- Verify the filesystems
virt-filesystems --long -h --all -a image.qcow2
- Setting the qcow2 image
sudo virt-customize -a /var/lib/libvirt/images/image.qcow2 --hostname vm01.rootzilopochtli.lab \
--root-password password:rootpw --ssh-inject 'root:file:labkey.pub' --uninstall cloud-init \
--selinux-relabel
- Installing the VM
sudo virt-install --name vm01 --memory 1024 --vcpus 1 --disk /var/lib/libvirt/images/image.qcow2 --import \
--os-type linux --os-variant generic --noautoconsole
Note: If you want to set the exact os-variant, check with the osinfo-query command:
sudo osinfo-query os
- Using console
sudo virsh console vm01
NOTE: To escape from console use the key combination ctrl+5.
This error , during customization, causes hostkeys not to be generated; therefore sshd
will not start:
root@debian:~# systemctl status sshd
● ssh.service - OpenBSD Secure Shell server
Loaded: loaded (/lib/systemd/system/ssh.service; enabled; vendor preset: e>
Active: failed (Result: exit-code) since Thu 2022-09-08 00:35:29 UTC; 32mi>
Docs: man:sshd(8)
man:sshd_config(5)
Process: 528 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=1/FAILURE)
CPU: 5ms
Sep 08 00:35:29 debian.rootzilopochtli.lab systemd[1]: ssh.service: Scheduled r>
Sep 08 00:35:29 debian.rootzilopochtli.lab systemd[1]: Stopped OpenBSD Secure S>
Sep 08 00:35:29 debian.rootzilopochtli.lab systemd[1]: ssh.service: Start reque>
Sep 08 00:35:29 debian.rootzilopochtli.lab systemd[1]: ssh.service: Failed with>
Sep 08 00:35:29 debian.rootzilopochtli.lab systemd[1]: Failed to start OpenBSD >
root@debian:~# /usr/sbin/sshd
sshd: no hostkeys available -- exiting.
- Generate hostkeys
root@debian:~# ssh-keygen -A
ssh-keygen: generating new host keys: RSA DSA ECDSA ED25519
virsh undefine centos
error: Failed to undefine domain centos
error: Requested operation is not valid: cannot undefine domain with nvram
nvram is a device that is allowed to change its address.
- Solution
virsh undefine --nvram centos
-
Copy qcow2 file from
/var/lib/libvirt/images
to same directory on destination host -
Create a dump xml from domain to export
sudo virsh dumpxml VMNAME > vmname.xml
- On the destination host run
sudo virsh define vmname.xml
- Start VM
-
With cloud-init: Configure hostname, add a user and grant him access by ssh and change the root password:
- Create meta-data:
cat > meta-data <<EOF local-hostname: vm02.rootzilopochtli.lab EOF
- Create user-data:
$ cat > user-data <<EOF # cloud-config users users: - name: dexter ssh_authorized_keys: - ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQD... sudo: ['ALL=(ALL) NOPASSWD:ALL'] groups: sudo shell: /bin/bash chpasswd: list: | root:password expire: False runcmd: - echo "AllowUsers ubuntu" >> /etc/ssh/sshd_config - restart ssh EOF
- Create a disk to attach with cloud-init configuration:
$ sudo genisoimage -output /var/lib/libvirt/images/vm02-cidata.iso -volid cidata -joliet -rock user-data meta-data
-
Launch VM
$ sudo virt-install --name vm02 --memory 2048 --vcpus 2 --disk /var/lib/libvirt/images/vm02.qcow2 \
--disk /var/lib/libvirt/images/vm02-cidata.iso,device=cdrom --import --network network=default --noautoconsole
-
Access the VM
- Verify that the vm is running:
$ sudo virsh list Id Name State ------------------------ 1 vm02 running
- Get his IP address
$ sudo virsh domifaddr vm02 Name MAC address Protocol Address ------------------------------------------------------------------------------- vnet0 52:54:00:19:54:79 ipv4 192.168.122.186/24
- Testing access with ssh