####### Libvirt ####### Shared folder ============= * In virt-viewer add hardware type filesystem (type: mount, mode: mapped, target: tagname) * In the vm .. code-block:: bash mount -t 9p -o trans=virtio tagname /path/to/mount_point * Or add following to fstab .. code-block:: bash tagname /path/to/mount_point 9p trans=virtio 0 0 Set up bridged network for direct access ======================================== * Edit ``/etc//etc/sysconfig/network-scripts/ifcfg-eno1`` .. code-block:: bash DEVICE=eno1 ONBOOT=yes BRIDGE=br0 NM_CONTROLLED=no * Create ``/etc//etc/sysconfig/network-scripts/ifcfg-br0`` .. code-block:: bash DEVICE=br0 TYPE=Bridge BOOTPROTO=dhcp ONBOOT=yes DELAY=0 NM_CONTROLLED=no * Restart network .. code-block:: bash systemctl restart network Connect to a remote libvirtd via ssh ==================================== .. code-block:: bash virsh -c qemu+ssh://username@host/system Quickinstall ============ * This needs ``libguestfs-tools`` * Build a new disk .. code-block:: bash virt-builder centos-7.0 -o mydisk.img --format qcow2 --size 20G \ --root-password file:/tmp/rootpw \ --update \ --run-command 'rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm' \ --install cloud-utils,cloud-init * Install a whole cluster .. code-block:: bash for i in {1..3}; do cp centos7.img node$i.img; virt-install --import --name "Node$i" --os-type=linux --ram=512 --disk path=node$i.img,size=2; done Create a machine ================ * Configure bridged interface br0 * Create a virtual machine with 20 gb disk and 512 mb ram * Via ISO file .. code-block:: bash virt-install --name="TestVM" --os-type=linux --ram=512 --disk path=test-vm.img,size=20 --cdrom * Using PXE .. code-block:: bash virt-install --name="TestVM" --os-type=linux --os-variant=rhel6 --network bridge=br0,mac=aa:bb:cc:aa:bb:cc --ram=512 --disk path=test-vm.img,size=20 --pxe Remove a machine ================ .. code-block:: bash virsh undefine List all virtual machines ========================= .. code-block:: bash virsh list Start or stop a machine ======================= .. code-block:: bash virsh start virsh shutdown virsh destroy * To shutdown a machine it must have acpid running Autostart a machine =================== .. code-block:: bash virsh autostart Info about a machine ==================== .. code-block:: bash virsh dominfo Info about host system ====================== .. code-block:: bash virsh nodeinfo Connect to a machine ==================== * ``virt-viewer`` or ``virt-manager`` Rename a machine ================ .. code-block:: bash virsh dumpxml > muh.xml virsh undefine virsh define muh.xml Attach a cdrom image ==================== .. code-block:: bash virsh attach-disk hdc --type cdrom --mode readonly Update boot order ================= * First dump machine settings as XML .. code-block:: bash virsh dumpxml > blah.xml * Edit XML file * Update machine settings .. code-block:: bash virsh define blah.xml Configure RAM ============== .. code-block:: bash virsh setmem Configure number of CPUs ======================== .. code-block:: bash virsh setvcpus Resize disk =========== * Switch off the machine * and convert old image to new one and expand sda2 to max size .. code-block:: bash virt-resize --expand /dev/sda2 old-disk.img new-disk.img Update a machines config ======================== .. code-block:: bash virsh edit Backup ====== * Save a machines RAM state to a file .. code-block:: bash virsh save * Take a snapshot of disk and ram (must be supported by disk image format e.g. qcow2 and this will PAUSE the machine if ram is backuped! use --disk-only to avoid this) .. code-block:: bash virsh snapshot-create-as * or by using qemu tools (but only when vm is off!) .. code-block:: bash qemu-img snapshot -c my-backup disk.img qemu-img snapshot -l disk.img * Extract snapshot of qcow2 image file .. code-block:: bash qemu-img convert -f qcow2 -s -O qcow2 * Another possibility is to install libguestfs-tools and create a tar archive of / .. code-block:: bash virt-tar -z -x / machine-backup.tgz Restore ======= .. code-block:: bash virsh snapshot-list virsh snapshot-revert Disk tricks =========== * Install libguestfs-tools .. code-block:: bash virt-df virt-df -d * Get content of a file .. code-block:: bash virt-cat -d * Edit a file (vm must be off) .. code-block:: bash virt-edit -d * Or even get a shell on a disk image .. code-block:: bash guestfish \ add disk.img : run : mount /dev/vg_guest/lv_root / : \ write /etc/resolv.conf "nameserver 8.8.8.8" Change root password with guestfish =================================== * Generate a new password hash .. code-block:: bash openssl passwd -1 topsecretpassword * Edit shadow file on image .. code-block:: bash guestfish --rw -a > run > list-filesystems /dev/sda1: ext4 > mount /dev/sda1 / > vi /etc/shadow Convert VirtIO to IDE disk and vice versa ========================================== * Make sure the machine is powered off .. code-block:: bash virsh edit * For IDE disk .. code-block:: bash
* For VirtIO disk .. code-block:: bash
* Afterwards update the systems ``/etc/fstab`` .. code-block:: bash virt-edit /path/to/image-file /etc/fstab Cloning ======= * Will copy a whole machine and its properties and gives it a new mac address * The machine must be switched off .. code-block:: bash virt-clone -o -n Migration ========= * By default, migration only transfers in-memory state of a running domain (memory, CPU state, ...). Disk images are not transferred during migration but they need to be accessible at the same path from both hosts. * Live migration needs shared network storage via NFS, iSCSI, GFS2 or Fibre Channel .. code-block:: bash virsh migrate --live qemu://example.com/system Performance overview ===================== * Use ``virt-top`` Performance tuning ================== * Use virtio driver for disk and net this will give a machine direct hardware access (no emulation - only for linux guests) * Maybe you have to load the kernel modules .. code-block:: bash modprobe virtio_blk modprobe virtio_net modprobe virtio_pci * If one dont want to use snapshots use `raw` as image type * Use `Writethrough` as caching type * Use MacVtab bridge as network device with virtio model * Use Spice and QXL driver for display Grant normal user permission to qemu:///system ============================================== * Create file ``/etc/polkit-1/localauthority/30-site.d/libvirt.pkla`` .. code-block:: bash [User update perms] Identity=unix-user:basti Action=org.libvirt.unix.manage ResultAny=no ResultInactive=no ResultActive=yes Scripting with Python2 ====================== * Just a sample script to shutdown all active instances and boot all that were inactive .. code-block:: python import libvirt #conn=libvirt.open("qemu:///system") conn = libvirt.open("qemu+ssh://root@127.0.0.1/system") print "Active instances" active_instances = [] for id in conn.listDomainsID(): instance = conn.lookupByID(id) instance_name = instance.name() active_instances.append(instance_name) print "Deactivating ", instance_name instance.destroy() print "Activating inactive instances" inactive_instances = [instance for instance in conn.listDefinedDomains() if instance not in active_instances] for instance_name in inactive_instances: print "Activating ", instance_name instance = dom = conn.lookupByName(instance_name) instance.create() conn.close() * A script to create / delete a new instances .. code-block:: bash import sys import libvirt dom_name = "testme" dom_mem = 512 dom_cpu = 1 dom_disk = "/data/virtualbox/centos64.img" qemu_disk_type = "raw" if len(sys.argv) < 2: print sys.argv[0], " up/down" sys.exit(0) conn = libvirt.open("qemu+ssh://root@127.0.0.1/system") if sys.argv[1] == "down": dom = conn.lookupByName(dom_name) if dom: dom.undefine() else: print "Cannot find domain ", dom_name conn.close() sys.exit(1) else: xml = """ """ + dom_name + """ """ + str(dom_mem * 1024) + """ """ + str(dom_cpu) + """ hvm destroy restart restart /usr/libexec/qemu-kvm