libvirt

Table of Contents

Clone disk

virt-clone --original CurrentVM --name NewVM -f /var/lib/libvirt/images/newVM.img
virt-clone --connect=qemu:///system --original CurrentVM --name NewVM -f /var/lib/libvirt/images/newVM.img

SysPrep

sudo aptitude install libguestfs-tools
virt-sysprep --list-operations

Option lvm-uuids fails on ubuntu 14.04, thus:

for i in `virt-sysprep --list-operations |grep '*'|grep -v lvm-uuids| cut -d ' ' -f1`; do echo virt-sysprep -d ubuntu-14.04-server-sysprepped --enable $i; done

In some cases the server does not re-create the SSH server keys. If your installation was done set up with console access then the server keys can be re-created with:

sudo dpkg-reconfigure openssh-server

Inject your SSH keys with:

virt-sysprep -d __HOSTNAME__ --ssh-inject GUESTWM-USERNAME

.. or another names key:

virt-sysprep -d __HOSTNAME__ --ssh-inject GUESTWM-USERNAME:file:~/.ssh/other_id_rsa.pub

Network Information

virsh dumpxml __GUEST_NAME__ |grep 'mac address'
arp -na|grep __MAC_ADR__

OR

arp -na | grep `virsh dumpxml __GUEST_NAME__|grep 'mac address'|cut -d"'" -f2`

OR list all hostnames and IP addresses with:

virsh list|grep -vE '^$| Id\s+Name\s+State|------------------' | awk '{print $2}'| xargs -I{} sh -c "echo {}|tr '\n' ' ';virsh dumpxml {} | grep mac\ address| cut -d\' -f2|xargs -I{} sh -c 'arp -na| grep {}|cut -d\( -f2| cut -d\) -f1'"

virt-install

About these virt-install examples

The following commands will try to install a machine with a text-console in which you manually set up the machine. If you want this to function as a server available on the network then make sure you have the network bridge interface set up before creating the machine.

Ubuntu 14.10 (utopic)

virt-install trusty 14.04 guest

Ubuntu 14.04 (trusty) on virtual server.

Install the guest

virt-install --name __HOSTNAME__ \
--ram 512 \
--disk /var/lib/libvirt/images/__HOSTNAME__.img,size=4 \
--location http://archive.ubuntu.com/ubuntu/dists/trusty/main/installer-amd64/ \
--extra-args "text console=tty0 utf8 console=ttyS0,115200" \
--graphics none

Generate base-image

Clone the newly installed guest to a "base-image".

virt-clone --original __HOSTNAME__ --name base_trusty -f /var/lib/libvirt/images/base_trusty.img

SysPrep the the guest.

for i in `virt-sysprep --list-operations |grep '*'|grep -v lvm-uuids| cut -d ' ' -f1`; do sudo virt-sysprep -d base_trusty --enable $i; done

Deploy from base-image

Clone the base-image to a new guest

virt-clone --original base_trusty --name sample-host -f /var/lib/libvirt/images/sample-host.img

Start the new clone.

virsh start base_trusty

Connect to the console

virsh console base_trusty

Generate host SSH keys (connecting with SSH won't work unless this step is performed).

sudo dpkg-reconfigure openssh-server

Stop the base image instance.

virsh stop base_trusty

Adjust the allocated memory.

virsh setmaxmem base_trusty 1G --config
virsh setmem base_trusty 1G --config

Ubuntu 14.04 (trusty)

Note: Using –nographics fails on Ubuntu 14.10

virt-install with bridged network

virt-install --name server1 \
--ram 512 \
--disk /var/lib/libvirt/images/server1.img,size=4 \
--location http://archive.ubuntu.com/ubuntu/dists/trusty/main/installer-amd64/ \
--extra-args "text console=tty0 utf8 console=ttyS0,115200" \
--network bridge:br0 \
--nographics \
--keymap fo

virt-install without bridged network

virt-install --name __HOSTNAME__ \
--ram 512 \
--disk /var/lib/libvirt/images/__HOSTNAME__.img,size=4 \
--location http://archive.ubuntu.com/ubuntu/dists/trusty/main/installer-amd64/ \
--extra-args "text console=tty0 utf8 console=ttyS0,115200" \
--nographics \
--keymap fo

virt-install with kickstart

virt-install \
--name server1 \
--hvm \
--ram 1024 \
--file /u01/server1.img \
--file-size 10 \
--nographics \
--network bridge:br0 \
--location=/u01/KS/RHEL5ISO \
--extra-args="ks=http://kickstartserver/server1.cfg text console=tty0 utf8
console=ttyS0,115200"

Adding this in kickstart file helps:

bootloader --location=mbr --append="console=tty0 console=ttyS0,9600"

virt-install withpreseed

Note: 256GB disk drive!

virt-install \
--name __GUEST_NAME__ \
--graphics spice,keymap=fo \
--vcpus 2 \
--ram 4096 \
--disk /var/lib/libvirt/images/devenv2.img,size=256 \
--location http://archive.ubuntu.com/ubuntu/dists/trusty/main/installer-amd64/ \
--extra-args="hostname=__GUEST_HOSTNAME__ locale=en_US keyboard-configuration/layoutcode=fo url=http://192.168.1.123/preseed.cfg DEBCONF_DEBUG=5 auto=true priority=critical" \
--extra-args="preseed/file=preseed.cfg text console=tty0 utf8 console=ttyS0,115200"

Network Install URLs

Usable with virt-manager.

Extending disk space

qemu-img resize foo.qcow2 +32G
resize2fs -p /dev/vg_NAME/lv_NAME
fdisk -l
fdisk /dev/vda
reboot
fdisk -l
pvdisplay
pvcreate /dev/vda3
vgdisplay 
vgextend ubuntu-vg /dev/vda3

Note: extend lv with a little less than pv size!!!

vgdisplay 
lvextend -L +248G /dev/ubuntu-vg/root
resize2fs -p /dev/ubuntu-vg/root
df -h .

Author: Rói á Torkilsheyggi

Created: 2016-09-12 Mon 22:02

Emacs 24.5.1 (Org mode 8.2.10)

Validate