{"revision": {"id": "f1e14d25-2f95-11f1-aaea-e86a64d24d78", "node_id": "f1df5bd8-2f95-11f1-9651-e86a64d24d78", "user_id": "edc3f576-2f95-11f1-900f-e86a64d24d78", "author": "foxhop", "data": "KVM guest management\r\n=======================\r\n\r\n.. contents:: \r\n\r\n\r\nInstall packages \r\n===============================\r\n\r\n**Debian or Ubuntu**\r\n\r\n* sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder bridge-utils\r\n\r\n\r\n**Redhat or Fedora** \r\n\r\n* yum install kvm \r\n* yum install virt-manager libvirt libvirt-python python-virtinst \r\n* chkconfig libvirtd on \r\n* service libvirtd start \r\n\r\n\r\nKVM virsh cheat sheet!\r\n============================\r\n\r\nThis is a list of commands to use in the virsh cli.\r\n\r\nTo start the virsh cli run the following cmd:\r\n\r\n.. code-block:: bash\r\n  \r\n  $ virsh\r\n\r\nor:\r\n\r\n.. code-block:: bash\r\n\r\n  virsh --connect qemu:///system\r\n\r\nKVM has its own commands, but we like to use virsh.   \r\n\r\nHere is a list comparing Xen and KVM (virsh) commands. \r\n\r\nList running guests \r\n---------------------------------------------- \r\n\r\n**xen** \r\n | xm list \r\n\r\n**kvm** \r\n | virsh list \r\n | virsh dominfo <dom name> \r\n\r\nStart a guest \r\n---------------------------------------------- \r\n\r\n**xen** \r\n | xm create /etc/xen/<dom name>\r\n\r\n**kvm** \r\n | virsh create /etc/libvirt/qemu/<dom name>.xml\r\n | virsh start <dom name>\r\n\r\nConsole to guest\r\n---------------------------------------------- \r\n\r\n**xen** \r\n xm console <dom name>\r\n\r\n**kvm** \r\n virsh console <dom name>\r\n\r\nShutdown a guest \r\n---------------------------------------------- \r\n\r\n**xen** \r\n xm shutdown <dom name> \r\n\r\n**kvm** \r\n virsh shutdown <dom name> \r\n\r\nThis command doesn't always work...  \r\n\r\nBe sure the guests XML file has the following entry:\r\n\r\n | <feature> \r\n |  <acpi/>\r\n | </feature>\r\n\r\nAlso make sure the apci is installed on the guest operating system.\r\n\r\n**debian / ubuntu**\r\n sudo apt-get install acpid\r\n\r\n**fedora / redhat**\r\n yum install acpid\r\n\r\nAutostart guest on reboot \r\n---------------------------------------------- \r\n\r\n**xen** \r\n ln -s /etc/xen/<dom name> /etc/xen/auto/<dom name>\r\n\r\n**kvm** \r\n virsh autostart <dom name>\r\n\r\nEdit a guests configuration file\r\n-----------------------------------\r\n**xen**\r\n  NA\r\n\r\n**kvm**  \r\n virsh edit <dom name>\r\n\r\n\r\n\r\n\r\nPull the power on a guest\r\n----------------------------\r\n**xen**\r\n xm destroy <dom name>\r\n\r\n**kvm**\r\n virsh destroy <dom name>\r\n\r\nQuit virsh cli\r\n-----------------\r\n\r\n**kvm** \r\n virsh quit\r\n\r\n\r\n\r\n\r\n\r\nVirtual Serial Console configuration\r\n======================================\r\n\r\n**From the KVM host run:**\r\n\r\n.. code-block:: xml\r\n\r\n virsh edit <dom>\r\n\r\nThen add the following inside the <device></device> directives:\r\n\r\n.. code-block:: xml\r\n\r\n <serial type=\"pty\">\r\n     <source path=\"/dev/pts/3\" />\r\n     <target port=\"1\" />\r\n </serial>\r\n\r\n**From the Guest run:**\r\n\r\n.. code-block:: xml\r\n\r\n sudo cp -p /etc/init/tty06.conf /etc/init/ttyS0.conf\r\n sudo vi /etc/init/ttyS0.conf\r\n sudo diff /etc/init/tty06.conf /etc/init/ttyS0.conf\r\n\r\n\r\n\r\n\r\nVmbuilder Example\r\n======================\r\n\r\nThis example assumes ubuntu linux.\r\n\r\nInstall vmbuilder\r\n---------------------\r\n\r\n.. code-block:: python\r\n\r\n sudo apt-get install python-vm-builder\r\n\r\nDisplay Help vmbuilder\r\n--------------------------------\r\n\r\n.. code-block:: python\r\n\r\n vmbuilder kvm ubuntu --help\r\n\r\nCustom vmbuilder string\t\r\n-------------------------------\t\t\t\r\n \t\t\t\r\nmbison example \t\r\n\r\n.. code-block:: bash\t\t\t\r\n\t\t\r\n VMNAME=mbison\t\t\t\r\n mkdir /vms/$VMNAME\t\t\t\r\n \t\t\t\r\n sudo vmbuilder kvm ubuntu --libvirt=qemu:///system --suite=lucid --flavour=virtual \\\t\t\t\r\n --hostname=$VMNAME --domain='foxhop.net' --rootsize='10240' --mem='1024' \\\t\t\t\r\n --ip=192.168.1.51 --gw=192.168.1.254 --dns=192.168.1.22 --bridge=br0 \\\t\t\t\r\n --addpkg=openssh-server --addpkg=acpid --timezone=EDT --verbose\t\t\t\r\n\t \t\t\t\r\ncammy example\t\t\t\r\n \t\t\t\r\n.. code-block:: bash\t\t\t\r\n \t\t\t\r\n VMNAME=cammy\t\t\t\r\n mkdir /vms/$VMNAME\t\t\t\r\n \t\t\t\r\n sudo vmbuilder kvm ubuntu       \\\t\t\t\r\n     --libvirt=qemu:///system    \\\t\t\t\r\n     --suite=lucid               \\\t\t\t\r\n     --arch=amd64 -o             \\\t\t\t\r\n     --flavour=virtual           \\\t\t\t\r\n     --hostname=$VMNAME          \\\t\t\t\r\n     --dest=/vms/$VMNAME         \\\t\t\t\r\n     --rootsize=20480            \\\t\t\t\r\n     --mem=1024                  \\\t\t\t\r\n     --bridge=br0                \\\t\t\t\r\n     --ip=192.168.1.52           \\\t\t\t\r\n     --gw=192.168.1.254          \\\t\t\t\r\n     --dns=192.168.1.22          \\\t\t\t\r\n     --user=john                 \\\t\t\t\r\n     --pass=doe                  \\\t\t\t\r\n     --addpkg=openssh-server     \\\t\t\t\r\n     --addpkg=acpid              \\\t\t\t\r\n     --timezone=EDT              \\ \t\t\t\r\n     --verbose\r\n\r\n\r\nvirt-install\r\n================\r\n\r\nThis method shows the virt-install script installing from a debian netboot image hosted on the internet\r\n\r\n.. code-block:: bash\r\n\r\n DOMNAME=tehforum\r\n\r\n sudo virt-install --name=$DOMNAME --vcpu=1 --ram=128 --os-type=linux --autostart \\\r\n --disk=/KVMROOT/tehforum.qcow2,size=10 \\\r\n --location=http://ftp.nl.debian.org/debian/dists/wheezy/main/installer-amd64/ \\\r\n --extra-args=\"auto=true priority=critical interface=auto lang=english console-keymaps-at/keymap=us locale=en_US hostname=$DOMNAME domain=foxhop.net url=http://www.debian.org/releases/stable/example-preseed.txt\"\r\n\r\n\r\n\r\n\r\n\r\nMounting a qcow2 image on the host\r\n======================================\r\n\r\nSometimes it is helpful to be able to mount a drive image under the host system. For example, if the guest doesn't have network support, the only way to transfer files into and out of the guest will be by the storage devices it can address or to restore files from a backup image.\r\n\r\nTo mount qcow2 images there is (at least in F-11 qemu) very useful qemu-nbd util. It shares image through kernel network block device protocol and this allows to mount it:\r\n\r\n.. code-block:: bash\r\n   \r\n  sudo modprobe nbd max_part=63\r\n  sudo qemu-nbd -c /dev/nbd0 image.img\r\n  sudo mount /dev/nbd0p1 /mnt/image\r\n\r\n\r\n\r\n\r\nAdd another disk image to a guest\r\n====================================\r\n\r\nThis is how you add an additional disk to a guest.  In this example I will be adding a 20GB disk image to a guest dom named cammy.\r\n\r\n.. code-block:: bash\r\n\r\n fallocate -l 20g /vms/cammy/cammy-aux.img\r\n\r\nor\r\n\r\n.. code-block:: bash\r\n\r\n qemu-img create -f qcow2 /vms/cammy/cammy-aux.img 20G\r\n\r\nIf you want to look that the image statistics try this command.\r\n\r\n.. code-block:: bash\r\n\r\n\r\n qemu-img info /vms/cammy/cammy-aux.img\r\n\r\nNow we need to mount this disk:\r\n\r\n.. code-block:: bash\r\n\r\n sudo virsh attach-disk cammy /vms/cammy/cammy-aux.img vdb\r\n\r\nThat command only mounted the disk.  To make this change permanent we need to alter the dom's xml:\r\n\r\n.. code-block:: xml\r\n\r\n # sudo virsh edit cammy\r\n\r\n  <disk type='file' device='disk'>\r\n    <driver name='qemu' type='qcow2'/>\r\n    <source file='/KVMROOT/cammy.qcow2'/>\r\n    <target dev='vda' bus='virtio'/>\r\n    <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>\r\n  </disk>\r\n\r\n  <disk type='file' device='disk'>\r\n    <driver name='qemu' type='raw'/>\r\n    <source file='/KVMROOT/cammy-var.img'/>\r\n    <target dev='vdb' bus='virtio'/>\r\n  <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>\r\n\r\nSSH to the guest and verify that the disk shows up in *fdisk -l*.  You may now partition this disk how you like.  For more information view `linux raw filesystem management <http://www.foxhop.net/linux-raw-filesystem-management>`_.\r\n\r\n", "source_format": "rst", "revision_number": 64, "created": 1368893989000}}