
Linux and KVM, part 4: network (NAT, bridge, VLAN) and storage (qcow2, LVM, ZFS) for virtual machines
🌐 Machine-translated from Russian. Show original
In this part:
Network options#
| Mode | How it works | When to use |
|---|---|---|
NAT (default) | VM behind the host "router," network 192.168.122.0/24 | tests, VMs that do not require incoming access |
| Bridge | VM — full participants of the local network | servers for the office: 1C, files, AD, |
| Bridge + VLAN | VMs in different VLANs over a single cable | network separation: servers, video surveillance, guests, |
| Isolated | VMs see each other only | laboratories, test benches, |
| macvtap | VM directly on a physical port without a bridge | quickly set up, but the host and VM do not see each other |
Step 1. Bridge over netplan#
Find out the name of the network card: ip -br link (for example, eno1). Save a copy of the current file and replace the contents of /etc/netplan/50-cloud-init.yaml (your file name may differ):
network:
version: 2
ethernets:
eno1:
dhcp4: false
bridges:
br0:
interfaces: [eno1]
addresses: [192.168.1.10/24]
routes:
- to: default
via: 192.168.1.1
nameservers:
addresses: [192.168.1.1, 1.1.1.1]
parameters:
stp: false
forward-delay: 0
sudo chmod 600 /etc/netplan/*.yaml
sudo netplan try # если связь пропадет — через 120 с вернется старая настройка
Press Enter to confirm. IP now hangs on br0instead of eno1 — that's normal.
⚠️ Switch networks with backup access: monitor, IPMI/iDRAC/iLO, or at least
netplan try.
To prevent cloud-init from overwriting the file during reboot:
echo 'network: {config: disabled}' | sudo tee /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
Connect the VM to the bridge: in Cockpit → the VM → Network interfaces → Edit → Bridge to LAN → br0. Or when creating: --network bridge=br0,model=virtio.
Step 2. VLAN#
The switch port to which the host is connected must be a trunk with the required VLANs. A simple way is to have a separate bridge to each VLAN:
vlans:
vlan20:
id: 20
link: eno1
bridges:
br20:
interfaces: [vlan20]
dhcp4: false
The VM connected to the br20will be in VLAN 20. How to configure trunk on a switch — in the series "Network equipment from different manufacturers."
Step 3. Disc formats#
| Format | Pros | Cons |
|---|---|---|
| qcow2 | snapshots, fine selection, compression, templates, | a bit slower to write |
| raw | maximum speed, just | no internal snapshots, the file is immediately "thick" (if FS does not support sparse files) |
| LVM volume | speed like raw, LVM snapshots, convenient disk splitting, | need free space in the volume group |
| ZFS zvol | checksums, compression, snapshots, and replication | requires memory (ARC), better with ECC |
Recommendation to start: qcow2 on a fast SSD. For 1C and SQL databases — raw or LVM.
Useful commands:
qemu-img info /var/lib/libvirt/images/web01.qcow2
qemu-img resize /var/lib/libvirt/images/web01.qcow2 +20G # ВМ выключена
qemu-img convert -O qcow2 disk.vmdk disk.qcow2 # перенос из VMware
Step 4. libvirt storage pools#
Pool — the place where libvirt creates VM disks. By default — directory /var/lib/libvirt/images.
Separate disk for VM as a directory:
sudo mkfs.xfs /dev/sdb
sudo mkdir -p /vm
echo '/dev/sdb /vm xfs defaults 0 0' | sudo tee -a /etc/fstab # лучше указать UUID из blkid
sudo mount -a
virsh pool-define-as vmstore dir --target /vm
virsh pool-build vmstore && virsh pool-start vmstore && virsh pool-autostart vmstore
Free space in the LVM volume group left in part 2 as a pool:
sudo vgs # группа ubuntu-vg и свободный объем VFree
virsh pool-define-as lvmpool logical --source-name ubuntu-vg --target /dev/ubuntu-vg
virsh pool-start lvmpool && virsh pool-autostart lvmpool
virsh vol-create-as lvmpool db01-disk 100G
Network storage: NFS (virsh pool-define-as nas netfs --source-host 192.168.1.20 --source-path /vm --target /mnt/nas) or iSCSI. It is needed for live migration (part 6).
Step 5. Disk parameters you need to know#
virsh edit web01
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none' io='native' discard='unmap'/>
<source file='/vm/web01.qcow2'/>
<target dev='vda' bus='virtio'/>
</disk>
cache='none'— safe and fast for servers.discard='unmap'— the space freed in the VM is returned to the host (enablefstrim.timerin Linux VM).
Step 6. RAID for the host#
Without a mirror, SSD failure = stopping all VMs. Options: hardware RAID controller, software mdadm (configured directly in the Ubuntu installer via Custom storage layout), or ZFS mirror. RAID does not replace backup — about backups in Linux and KVM, part 5: templates and cloud-init, clones per minute, snapshots, and backups.
Comments · 0
No comments yet.Sign in to comment, rate and see all comments.