#linux #virtualizationThe virsh program is the main interface for managing virsh guest domains. The program can be used to create, pause, and shutdown domains. It can also be used to list current domains.It is part of LibVirt and becomes available after the installation of LibVirt. Before working with virsh, make sure it is connected to the correct context:- qemu:///system: Connects to the system-wide instance of libvirt, which runs as a system service. This requires root privileges.
- qemu:///user: Connects to the user-specific instance of libvirt, which runs within the user's own session.
âšī¸ You may need to set one of these environment variables: debian:~$ echo $LIBVIRT_DEFAULT_URI to qemu:///system.
Commands
- Show host node information: sudo virsh nodeinfo
- List all VMs: sudo virsh list --all (--all to see also inactive VMs)
- Information about a VM: sudo virsh dominfo <VM-NAME>
- Start a VM: sudo virsh start <VM-NAME>
- Autostart a VM: sudo virsh autostart <VM-NAME> to disable sudo virsh autostart --disable <VM-NAME>
- Shutdown gracefully a VM: sudo virsh shutdown <VM-NAME>
- Shutdown forcefully a VM: sudo virsh destroy <VM-NAME>
- Shutdown all running VMs:
for i in `sudo virsh list | grep running | awk '{print $2}'` do
sudo virsh shutdown $i
done - Reboot a VM: sudo virsh reboot <VM-NAME>
- Remove a VM:
sudo virsh destroy <VM-NAME> 2> /dev/null # Stop the machine
sudo virsh undefine <VM-NAME> # Remove its definition from the hypervisor
sudo virsh pool-refresh default # Update the storage pool to reflect any changes
sudo virsh vol-delete --pool default <VM-NAME>.qcow2 # Delete the storage volume
# here the storage volume is named /var/lib/libvirt/images/<VM-TEST>.qcow2
# ALTERNATIVE:
sudo virsh undefine <VM-NAME> --remove-all-storage
# If this does not work because of nvram:
sudo virsh undefine --nvram "name of VM"
- Install a new VM (with virt-install). This is the install command for home assistant os:
virt-install \
--name haos \
--description "Home Assistant OS" \
--os-variant=generic \
--ram=4096 \
--vcpus=2 \
--disk <PATH TO QCOW2 FILE>,bus=scsi \
--controller type=scsi,model=virtio-scsi \
--import \
--graphics none \
--boot uefi \
# In one line: /var/lib/libvirt/images/
virt-install --name haos --description "Home Assistant OS" --os-variant=generic --ram=4096 --vcpus=2 --disk <PATH TO QCOW2 FILE>,bus=scsi --controller type=scsi,model=virtio-scsi --import --graphics none --boot uefi
- Connect to VM console: sudo virsh console <VM-NAME>
- Edit a VM XML: sudo virsh edit <VM-NAME> # Set the default editor to vim with: export EDITOR=vim
- Save a VM: sudo virsh save <VM-NAME> <PATH where to save the VM> # e.g. ~/vm-name.save
- Restoring a saved VM: sudo virsh restore <Path to the saved VM>
Snapshot Management
Virsh Network management
link: https://linuxconfig.org/how-to-use-bridged-networking-with-libvirt-and-kvmFirst a short definition: NAT (Network Address Translation- Isolation: VMs are isolated from the local network. They are assigned private IP addresses that are not directly accessible from the outside network.
- Access: VMs can access external networks (like the internet) through the host machine's IP address. The host translates the VM's private IP address to its own public IP address for outgoing traffic.
- Use Case: This mode is useful for scenarios where you want to limit the exposure of VMs to the local network or when you have limited IP addresses available.
Bridged Networking- Direct Access: VMs are connected directly to the local network and can obtain IP addresses from the same DHCP server as other devices on the network (e.g., your router).
- Visibility: VMs are fully visible to other devices on the local network, allowing them to communicate directly with other machines without going through the host.
- Use Case: This mode is ideal for scenarios where VMs need to be accessible from other devices on the local network, such as for testing network services or running servers.
List all networks: sudo virsh net-list --all This shows also the inactive networks.
To obtain detailed information about the network, and to modify it: sudo virsh net-edit defaultThe default network should be NAT networking. The XML looks like this: <network>
<name>default</name>
<uuid>168f6909-715c-4333-a34b-f74584d26328</uuid>
<forward mode='nat'/>
<bridge name='virbr0' stp='on' delay='0'/>
<mac address='52:54:00:48:3f:0c'/>
<ip address='192.168.122.1' netmask='255.255.255.0'>
<dhcp>
<range start='192.168.122.2' end='192.168.122.254'/>
</dhcp>
</ip>
</network>
The default network is based on the use of the virbr0 virtual bridge, and uses NAT based connectivity to connect the virtual machines. Verify that the bridge exists using: ip link show type bridge- Define a network (without starting it): sudo virsh net-define <PATH TO XML>
- Start a network: sudo virsh net-start <NET-NAME>
- Set a network to autostart: sudo virsh net-autostart <NET-NAME>
- Autostart a network: sudo virsh net-start <NET-NAME>
Create a bridged network with network manager (nmcli)
- Create a Bridge on the host called br0:
- sudo nmcli con add type bridge con-name br0 ifname br0
This example demonstrates adding a bridge master conection and one slave. - sudo nmcli con add type bridge-slave ifname eth0 master br0
- nmcli con show
- IMPORTANT: Disable STP: sudo nmcli con modify br0 bridge.stp no
-> View with nmcli -f bridge con show br0 - Turn on the bridge interface:
- sudo nmcli con down eth0
- sudo nmcli con up br0
- nmcli con show
Other stuff:- Set the bridge to be up: nmcli connection up br0
- Use DHCP: nmcli connection modify br0 ipv4.method auto
- Verify the configuration: nmcli connection show
- Let the start on boot: nmcli connection modify br0 connection.autoconnect yes
- Configure an existing VM to use this interface
- Define a new network: vim /tmp/br0.xml
With this content:
<network>
<name>br0</name>
<forward mode="bridge"/>
<bridge name="br0" />
</network>
- virsh net-define /tmp/br0.xml
- virsh net-start br0
- virsh net-autostart br0
- virsh net-list --all
Link: https://www.cyberciti.biz/faq/how-to-add-network-bridge-with-nmcli-networkmanager-on-linux/