% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
%
\documentclass[
]{article}
\usepackage{amsmath,amssymb}
\usepackage{iftex}
\ifPDFTeX
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
  \usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
  \usepackage{unicode-math} % this also loads fontspec
  \defaultfontfeatures{Scale=MatchLowercase}
  \defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
\usepackage{lmodern}
\ifPDFTeX\else
  % xetex/luatex font selection
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
  \usepackage[]{microtype}
  \UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
  \IfFileExists{parskip.sty}{%
    \usepackage{parskip}
  }{% else
    \setlength{\parindent}{0pt}
    \setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
  \KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
  \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{-\maxdimen} % remove section numbering
\ifLuaTeX
  \usepackage{selnolig}  % disable illegal ligatures
\fi
\IfFileExists{bookmark.sty}{\usepackage{bookmark}}{\usepackage{hyperref}}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\urlstyle{same}
\hypersetup{
  pdftitle={kvm},
  hidelinks,
  pdfcreator={LaTeX via pandoc}}

\title{kvm}
\author{}
\date{}

\begin{document}
\maketitle

\hypertarget{kvm}{%
\section{kvm}\label{kvm}}

\hypertarget{kvm-guest-management}{%
\section{KVM guest management}\label{kvm-guest-management}}

.. contents::

\hypertarget{install-packages}{%
\section{Install packages}\label{install-packages}}

\textbf{Debian or Ubuntu}

\begin{itemize}
\tightlist
\item
  sudo apt-get install qemu-kvm libvirt-bin ubuntu-vm-builder
  bridge-utils
\end{itemize}

\textbf{Redhat or Fedora}

\begin{itemize}
\tightlist
\item
  yum install kvm
\item
  yum install virt-manager libvirt libvirt-python python-virtinst
\item
  chkconfig libvirtd on
\item
  service libvirtd start
\end{itemize}

\hypertarget{kvm-virsh-cheat-sheet}{%
\section{KVM virsh cheat sheet!}\label{kvm-virsh-cheat-sheet}}

This is a list of commands to use in the virsh cli.

To start the virsh cli run the following cmd:

.. code-block:: bash

\$ virsh

or:

.. code-block:: bash

virsh --connect qemu:///system

KVM has its own commands, but we like to use virsh.

Here is a list comparing Xen and KVM (virsh) commands.

\hypertarget{list-running-guests}{%
\subsection{List running guests}\label{list-running-guests}}

\textbf{xen} \textbar{} xm list

\textbf{kvm} \textbar{} virsh list \textbar{} virsh dominfo

\hypertarget{start-a-guest}{%
\subsection{Start a guest}\label{start-a-guest}}

\textbf{xen} \textbar{} xm create /etc/xen/

\textbf{kvm} \textbar{} virsh create /etc/libvirt/qemu/.xml \textbar{}
virsh start

\hypertarget{console-to-guest}{%
\subsection{Console to guest}\label{console-to-guest}}

\textbf{xen} xm console

\textbf{kvm} virsh console

\hypertarget{shutdown-a-guest}{%
\subsection{Shutdown a guest}\label{shutdown-a-guest}}

\textbf{xen} xm shutdown

\textbf{kvm} virsh shutdown

This command doesn't always work\ldots{}

Be sure the guests XML file has the following entry:

\textbar{} \textbar{} \textbar{}

Also make sure the apci is installed on the guest operating system.

\textbf{debian / ubuntu} sudo apt-get install acpid

\textbf{fedora / redhat} yum install acpid

\hypertarget{autostart-guest-on-reboot}{%
\subsection{Autostart guest on reboot}\label{autostart-guest-on-reboot}}

\textbf{xen} ln -s /etc/xen/ /etc/xen/auto/

\textbf{kvm} virsh autostart

\hypertarget{edit-a-guests-configuration-file}{%
\subsection{Edit a guests configuration
file}\label{edit-a-guests-configuration-file}}

\textbf{xen} NA

\textbf{kvm}\\
virsh edit

\hypertarget{pull-the-power-on-a-guest}{%
\subsection{Pull the power on a guest}\label{pull-the-power-on-a-guest}}

\textbf{xen} xm destroy

\textbf{kvm} virsh destroy

\hypertarget{quit-virsh-cli}{%
\subsection{Quit virsh cli}\label{quit-virsh-cli}}

\textbf{kvm} virsh quit

\hypertarget{virtual-serial-console-configuration}{%
\section{Virtual Serial Console
configuration}\label{virtual-serial-console-configuration}}

\textbf{From the KVM host run:}

.. code-block:: xml

virsh edit

Then add the following inside the directives:

.. code-block:: xml

\textbf{From the Guest run:}

.. code-block:: xml

sudo cp -p /etc/init/tty06.conf /etc/init/ttyS0.conf sudo vi
/etc/init/ttyS0.conf sudo diff /etc/init/tty06.conf /etc/init/ttyS0.conf

\hypertarget{vmbuilder-example}{%
\section{Vmbuilder Example}\label{vmbuilder-example}}

This example assumes ubuntu linux.

\hypertarget{install-vmbuilder}{%
\subsection{Install vmbuilder}\label{install-vmbuilder}}

.. code-block:: python

sudo apt-get install python-vm-builder

\hypertarget{display-help-vmbuilder}{%
\subsection{Display Help vmbuilder}\label{display-help-vmbuilder}}

.. code-block:: python

vmbuilder kvm ubuntu --help

\hypertarget{custom-vmbuilder-string}{%
\subsection{Custom vmbuilder string}\label{custom-vmbuilder-string}}

mbison example

.. code-block:: bash

VMNAME=mbison\\
mkdir /vms/\$VMNAME

sudo vmbuilder kvm ubuntu --libvirt=qemu:///system --suite=lucid
--flavour=virtual ~\\
--hostname=\$VMNAME --domain=`foxhop.net' --rootsize=`10240'
--mem=`1024' ~\\
--ip=192.168.1.51 --gw=192.168.1.254 --dns=192.168.1.22 --bridge=br0 ~\\
--addpkg=openssh-server --addpkg=acpid --timezone=EDT --verbose

cammy example

.. code-block:: bash

VMNAME=cammy\\
mkdir /vms/\$VMNAME

sudo vmbuilder kvm ubuntu ~\\
--libvirt=qemu:///system ~\\
--suite=lucid ~\\
--arch=amd64 -o ~\\
--flavour=virtual ~\\
--hostname=\(VMNAME \   --dest=/vms/\)VMNAME ~\\
--rootsize=20480 ~\\
--mem=1024 ~\\
--bridge=br0 ~\\
--ip=192.168.1.52 ~\\
--gw=192.168.1.254 ~\\
--dns=192.168.1.22 ~\\
--user=john ~\\
--pass=doe ~\\
--addpkg=openssh-server ~\\
--addpkg=acpid ~\\
--timezone=EDT ~\\
--verbose

\hypertarget{virt-install}{%
\section{virt-install}\label{virt-install}}

\hypertarget{debian-netboot-example}{%
\subsection{debian netboot example}\label{debian-netboot-example}}

This method shows the virt-install script installing from a debian
netboot image hosted on the internet

.. code-block:: bash

HOSTNAME=tehforum DOMAIN=foxhop.net

sudo virt-install\\
--name=\(HOSTNAME \
 --vcpu=1 \
 --ram=396 \
 --disk=/KVMROOT/\)HOSTNAME.qcow2,size=10\\
--os-type=linux\\
--autostart\\
--location=http://ftp.nl.debian.org/debian/dists/wheezy/main/installer-amd64/\\
--extra-args=``auto=true priority=critical keymap=us locale=en\_US
hostname=\(HOSTNAME domain=\)DOMAIN
url=http://192.168.1.22/foxhop-debconf-preseed.txt''

\hypertarget{ubuntu-netboot-example}{%
\subsection{ubuntu netboot example}\label{ubuntu-netboot-example}}

This method shows the virt-install script installing from an ubuntu
netboot image hosted on the internet

.. code-block:: bash

HOSTNAME=mbison DOMAIN=foxhop.net

sudo virt-install\\
--name=\(HOSTNAME \
 --vcpu=1 \
 --ram=396 \
 --disk=/KVMROOT/\)HOSTNAME.qcow2,size=10\\
--os-type=linux\\
--autostart\\
--location=http://archive.ubuntu.com/ubuntu/dists/raring/main/installer-amd64/\\
--extra-args=``auto=true priority=critical keymap=us locale=en\_US
hostname=\(HOSTNAME domain=\)DOMAIN
url=http://192.168.1.22/foxhop-debconf-preseed.txt''

\hypertarget{mounting-a-qcow2-image-on-the-host}{%
\section{Mounting a qcow2 image on the
host}\label{mounting-a-qcow2-image-on-the-host}}

Sometimes 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.

To 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:

.. code-block:: bash

sudo modprobe nbd max\_part=63 sudo qemu-nbd -c /dev/nbd0 image.img sudo
mount /dev/nbd0p1 /mnt/image

\hypertarget{add-another-disk-image-to-a-guest}{%
\section{Add another disk image to a
guest}\label{add-another-disk-image-to-a-guest}}

This 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.

.. code-block:: bash

fallocate -l 20g /vms/cammy/cammy-aux.img

or

.. code-block:: bash

qemu-img create -f qcow2 /vms/cammy/cammy-aux.img 20G

If you want to look that the image statistics try this command.

.. code-block:: bash

qemu-img info /vms/cammy/cammy-aux.img

Now we need to mount this disk:

.. code-block:: bash

sudo virsh attach-disk cammy /vms/cammy/cammy-aux.img vdb

That command only mounted the disk. To make this change permanent we
need to alter the dom's xml:

.. code-block:: xml

\# sudo virsh edit cammy

SSH to the guest and verify that the disk shows up in \emph{fdisk -l}.
You may now partition this disk how you like. For more information view
\texttt{linux\ raw\ filesystem\ management\ \textless{}http://www.foxhop.net/linux-raw-filesystem-management\textgreater{}}\_.

\hypertarget{shrink-a-qcow2-image}{%
\section{shrink a qcow2 image}\label{shrink-a-qcow2-image}}

http://pve.proxmox.com/wiki/Shrink\_Qcow2\_Disk\_Files

\end{document}
