Pejman Moghadam / Slackware

Slackware 12.2 - KVM Virtualization (with epkg)

install epkg package manager (encap)

Public domain


checking CPU for Intel VT or AMD-V Support

egrep '^flags.*(vmx|svm)' /proc/cpuinfo

Checking kernel

modprobe tun
modprobe kvm
modprobe kvm_intel
modprobe virtio_balloon
modprobe virtio_ring
modprobe virtio_pci
modprobe virtio
modprobe virtio_blk
modprobe virtio-rng
modprobe virtio_console
modprobe virtio_net

modprobe -l | grep kvm
modprobe -l | grep virtio

kernel

cd /usr/src
wget -c "http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.36.1.tar.bz2"
tar xf linux-2.6.36.1.tar.bz2
cd linux-2.6.36.1
make mrproper
cp /boot/config .config

make menuconfig

    Load an Alternate Configuration File
      .config
        OK
-*- Networking support  --->
      Networking options  --->
        [*] Network packet filtering framework (Netfilter)  --->
          Core Netfilter Configuration  --->
            <M> Transparent proxying support
            <M>   CHECKSUM target support
            <M>   "CT" target support
            <M>   IDLETIMER target support
            <M>   "LED" target support
            <M>   "TEE" - packet cloning to alternate destination
            <M>   "TPROXY" target support
            <M>   "cluster" match support
            <M>   "cpu" match support
            <M>   "ipvs" match support
            <M>   "osf" Passive OS fingerprint match
            <M>   "recent" match support
            <M>   "socket" match support
[*] Virtualization  --->
      <M>   Kernel-based Virtual Machine (KVM) support
      <M>     KVM for Intel processors support
      <M>     KVM for AMD processors support
      <M>   Host kernel accelerator for virtio net
      <M>   Linux hypervisor example code
      <M>   PCI driver for virtio devices
      <M>   Virtio balloon driver
    Device Drivers --->
      [*] Block devices --->
        <M>   Virtio block driver
      [*] Network device support --->
        <M>   Universal TUN/TAP device driver support
        <M>   Virtio network driver
      Character devices --->
        <M> Virtio console
        <M>   VirtIO Random Number Generator support

Compile & Install

time ( make -j64 all && make modules_install )
#/bin/cp arch/x86_64/boot/bzImage /boot/vmlinuz-2.6.36.1
/bin/cp arch/i386/boot/bzImage /boot/vmlinuz-2.6.36.1
/bin/cp System.map /boot/System.map-2.6.36.1
/bin/cp .config /boot/config-2.6.36.1

/etc/lilo.conf

default = S122-2.6.36.1

image = /boot/vmlinuz-2.6.36.1
  root = /dev/sda7
  label = S122-2.6.36.1
  read-only

Boot Loader

lilo
reboot

tunctl

http://tunctl.sourceforge.net/

cd /usr/src
wget http://pmoghadam.com/homepage/Pages/Deposit/Source-packages/tunctl-1.5.tar.gz
su - install
cd /usr/src
tar xf tunctl-1.5.tar.gz
cd tunctl-1.5
make
make install DESTDIR=/usr/local/encap/tunctl-1.5
cd /usr/local/encap/tunctl-1.5/
mv usr/share/man/ usr/
cd /usr/local/encap/
mkencap tunctl-1.5/
logout
cd /usr/local/encap/
epkg tunctl-1.5

Usage

modprobe tun
tunctl -t tap0
ifconfig tap0 up

socat

http://www.dest-unreach.org/socat/

cd /usr/src/
wget "pmoghadam.com/homepage/Pages/Deposit/Source-packages/socat-1.7.1.3.tar.bz2"
su - install
cd /usr/src
tar xf socat-1.7.1.3.tar.bz2
cd socat-1.7.1.3
./configure --prefix=/usr/ && make
make install DESTDIR=/usr/local/encap/socat-1.7.1.3
cd /usr/local/encap/socat-1.7.1.3/
mv usr/share/man/ usr/
cd /usr/local/encap/
mkencap socat-1.7.1.3/
logout
cd /usr/local/encap/
epkg socat-1.7.1.3

qemu-kvm

cd /usr/src
wget -c "pmoghadam.com/homepage/Pages/Deposit/Source-packages/qemu-kvm-1.0.tar.gz"
su - install
cd /usr/src/
tar xf qemu-kvm-1.0.tar.gz
cd qemu-kvm-1.0
./configure --prefix=/usr/ --audio-drv-list="alsa oss" && make
make install DESTDIR=/usr/local/encap/qemu-kvm-1.0
cd /usr/local/encap/qemu-kvm-1.0/
mv usr/share/man/ usr/
mv usr/share/doc/ usr/
cd /usr/local/encap/
mkencap qemu-kvm-1.0/
logout
cd /usr/local/encap/
epkg qemu-kvm-1.0
mkdir /var/run/qemu-kvm

image management

http://en.wikibooks.org/wiki/QEMU/Images

Create

qemu-img create -f qcow2 disk.qcow2 10G

Convert

qemu-img convert -O qcow2 sda.raw sda.qcow2

vmware-vdiskmanager -r image.vmdk -t 0 image-copy.vmdk
qemu-img convert -O qcow2 image-copy.vmdk image.qcow2

Mounting

modprobe nbd max_part=8
qemu-nbd --connect=/dev/nbd0 sda.qcow2
fdisk -l /dev/nbd0
mount /dev/nbd0p2 /mnt
umount /mnt
qemu-nbd --disconnect /dev/nbd0

/usr/local/sbin/mac-random

#!/bin/sh
# generate a random mac address for the qemu nic
# shell script borrowed from user pheldens @ qemu forum
echo $(echo -n DE:AD:BE:EF ; for i in `seq 1 2` ;
do echo -n `echo ":$RANDOM$RANDOM" | cut -n -c -3` ;done)

List available sound cards

qemu-system-x86_64 -soundhw ?

virtual machine installation script (vnc)

#!/bin/bash

NAME="servername"
MEM=1024
SHUTDOWNTIME=60
MAC="DE:AD:BE:EF:94:27"
VNC="127.0.0.1:0"
DRIVE="/storage/images/myserver/disk.qcow2"
ISO="/storage/images/myserver/dvd.iso"
APPEND="-boot d -cdrom ${ISO}"
#APPEND="$APPEND -drive file=/dev/sdb5,cache=none -drive file=/dev/sdb6,cache=none"
PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"
#export QEMU_AUDIO_DRV=alsa
#export QEMU_AUDIO_DRV=pa

start() {
  echo "Starting up ${NAME} virtual machine inside vnc for VM installation ..."
    qemu-system-x86_64                                  \
    -cpu host -m ${MEM}                                 \
    -drive file=${DRIVE},cache=off                      \
    -net nic,vlan=0,model=virtio,macaddr=${MAC}         \
    -net tap,vlan=0,ifname=tap-${NAME},script=no,downscript=no \
    -pidfile /var/run/qemu-kvm/${NAME}.pid              \
    -name qemu-${NAME},process=qemu-${NAME}             \
    -monitor unix:/var/run/qemu-kvm/${NAME}.sock,server,nowait \
    -vnc ${VNC} -daemonize -localtime -no-acpi          \
    -usb -usbdevice tablet \
    -soundhw es1370 \
    ${APPEND}

  # Bridge Networking
  tunctl -t tap-${NAME}
  ifconfig tap-${NAME} up
  brctl addif br0 tap-${NAME}
}

shutdown() {
  echo "Shuting down ${NAME} virtual machine ..."
  echo 'system_powerdown' | socat - unix-connect:"/var/run/qemu-kvm/${NAME}.sock" &> /dev/null
  echo -n "Wating for ${NAME} shutdown: "
  for S in $(seq 1 ${SHUTDOWNTIME}); do
    if ! pgrep -f ",process=qemu-${NAME} " > /dev/null; then break; fi
    echo -n .${S}
    sleep 1
  done
  kill -KILL $(cat "/var/run/qemu-kvm/${NAME}.pid"  2> /dev/null ) &> /dev/null
  rm -rf "/var/run/qemu-kvm/${NAME}.sock" "/var/run/qemu-kvm/${NAME}.pid" &> /dev/null
  echo " ok"

  # Networking Bridge
  brctl delif br0 tap-${NAME}
  ifconfig tap-${NAME} down
  tunctl -d tap-${NAME}
}

load_kvm_modules() {
  MODULES="
    8021q
    tun
    kvm
    kvm_intel
    virtio_balloon
    virtio_ring
    virtio_pci
    virtio
    virtio_blk
    virtio-rng
    virtio_console
    virtio_net
    9pnet_virtio
  "
  for MOD in $MODULES; do modprobe $MOD; done
}

load_kvm_modules
case "$1" in
  'start')
    start
    ;;

  'shutdown')
    shutdown
    ;;

  'restart')
    shutdown
    sleep 1
    start
    ;;
  *)
    echo "usage $0 [start|shutdown|restart]"
    ;;
esac

eject cd (qemu monitor)

CTL-ALT-2
info block
eject ide1-cd0
change ide1-cd0 /storage/slackware-12.2-install-d2.iso
CTL-ALT-1

eject cd (monitor socket)

echo 'info block' | socat - unix-connect:/var/run/qemu-kvm/servername.sock
echo 'eject ide1-cd0' | socat - unix-connect:/var/run/qemu-kvm/servername.sock
echo 'change ide1-cd0 /storage/slackware-12.2-install-d2.iso' | socat - unix-connect:/var/run/qemu-kvm/servername.sock

eject cd (monitor socket intractive)

# socat - unix-connect:/var/run/qemu-kvm/servername.sock
QEMU 0.13.0 monitor - type 'help' for more information
(qemu) info block
info block
ide0-hd0: type=hd removable=0 file=/e/new_cache/disk.qcow2 ro=0 drv=qcow2 encrypted=0
ide1-cd0: type=cdrom removable=1 locked=0 file=/e/iso/slackware-12.2-install-cd1.iso ro=1 drv=raw encrypted=0
floppy0: type=floppy removable=1 locked=0 [not inserted]
sd0: type=floppy removable=1 locked=0 [not inserted]
(qemu) eject ide1-cd0
eject ide1-cd0
(qemu) change ide1-cd0 /e/iso/slackware-12.2-install-cd2.iso
change ide1-cd0 /e/iso/slackware-12.2-install-cd2.iso
(qemu) quit
quit

USB passthrough

# lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 005 Device 002: ID 04da:1680 Panasonic (Matsushita) 

# socat - unix-connect:/var/run/qemu-kvm/servername.sock
QEMU 0.13.0 monitor - type 'help' for more information
(qemu) info usbhost
info usbhost
husb: using sys file-system with /dev/bus/usb
  Device 1.1, speed 480 Mb/s
    Hub: USB device 1d6b:0002, EHCI Host Controller
  Device 2.1, speed 12 Mb/s
    Hub: USB device 1d6b:0001, UHCI Host Controller
  Device 3.1, speed 12 Mb/s
    Hub: USB device 1d6b:0001, UHCI Host Controller
  Device 4.1, speed 12 Mb/s
    Hub: USB device 1d6b:0001, UHCI Host Controller
  Device 5.1, speed 12 Mb/s
    Hub: USB device 1d6b:0001, UHCI Host Controller
  Device 5.2, speed 12 Mb/s
    Class 00: USB device 04da:1680, KX-TDA100/200
(qemu) usb_add host:04da:1680
usb_add host:04da:1680
(qemu) <ctrl+d>

-usb -usbdevice host:08ec:2039


serial console output

slackware

/etc/lilo.conf

append=" vt.default_utf8=0 console=ttyS0,115200"

/etc/inittab

s0:2345:respawn:/sbin/agetty -L 115200 ttyS0 vt100

/etc/securetty

ttyS0

ubuntu

/etc/init/ttyS0.conf

start on runlevel [2345]
stop on runlevel [!2345]
respawn
exec /sbin/getty -L 115200 ttyS0 vt100

/boot/grub/grub.cfg

linux /boot/vmlinuz-2.6.32-21-generic-pae root=UUID=1cff7e37-f9d7-431f-ae39-1660558adfcb ro console=ttyS0,115200

simple install script

ISO="/storage/archlinux-2009.08-netinstall-i686.iso"

modprobe tun
modprobe kvm
modprobe kvm_intel
modprobe virtio_balloon
modprobe virtio_ring
modprobe virtio_pci
modprobe virtio
modprobe virtio_blk
modprobe virtio-rng
modprobe virtio_console
modprobe virtio_net
modprobe 9pnet_virtio

tunctl -t tap0
ifconfig tap0 192.168.1.1 netmask 255.255.255.0

/usr/local/qemu/bin/qemu-system-x86_64           \
  -m 512                                         \
  -cpu host                                      \
  -drive file=/dev/sda8,cache=none               \
  -net nic,vlan=0,model=virtio                          \
  -net tap,vlan=0,ifname=tap0,script=no                 \
  -boot d                                        \
  -cdrom ${ISO}

Virtual machine startup script (screen)

#!/bin/bash

NAME="servername"
MEM=1024
SHUTDOWNTIME=100
PREFIX="/storage/images/${NAME}"
DRIVE="${PREFIX}/sda.qcow2"
APPEND="-drive file=/dev/sdb5,cache=none -drive file=/dev/sdb6,cache=none"
MAC="DE:AD:BE:EF:15:51"
PATH="/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin"

poweron() {
  echo "Starting up ${NAME} virtual machine inside screen ..."
  env SCREENDIR="/root/.screen"                         \
    screen -h 4000 -dmS ${NAME}                         \
    qemu-system-x86_64                                  \
    -cpu host -m ${MEM}                                 \
    -drive file=${DRIVE},cache=off                      \
    -net nic,vlan=0,model=virtio,macaddr=${MAC}                \
    -net tap,vlan=0,ifname=tap-${NAME},script=no,downscript=no \
    -localtime -nographic                               \
    -pidfile ${PREFIX}/${NAME}.pid                      \
    -name qemu-${NAME},process=qemu-${NAME}             \
    -monitor unix:${PREFIX}/${NAME}.sock,server,nowait  \
    ${APPEND}

  # Bridge Networking
  tunctl -t tap-${NAME}
  ifconfig tap-${NAME} up
  brctl addif br0 tap-${NAME}
}

shutdown() {
  echo "Shuting down ${NAME} virtual machine inside screen ..."
  echo 'system_powerdown' | socat - unix-connect:"${PREFIX}/${NAME}.sock" &> /dev/null
  echo -n "Wating for ${NAME} shutdown: "
  for S in $(seq 1 ${SHUTDOWNTIME}); do
    if ! pgrep -f ",process=qemu-${NAME} " > /dev/null; then break; fi
    echo -n .${S}
    sleep 1
  done
  kill -KILL $(cat "${PREFIX}/${NAME}.pid"  2> /dev/null ) &> /dev/null
  rm -rf "${PREFIX}/${NAME}.sock" "${PREFIX}/${NAME}.pid" &> /dev/null
  echo " ok"

  # Networking Bridge
  brctl delif br0 tap-${NAME}
  ifconfig tap-${NAME} down
  tunctl -d tap-${NAME}
}

load_kvm_modules() {
  MODULES="
    8021q
    tun
    kvm
    kvm_intel
    virtio_balloon
    virtio_ring
    virtio_pci
    virtio
    virtio_blk
    virtio-rng
    virtio_console
    virtio_net
    9pnet_virtio
  "
  for MOD in $MODULES; do modprobe $MOD; done
}

load_kvm_modules
case "$1" in
  'poweron')
    poweron
    ;;

  'shutdown')
    shutdown
    ;;

  'restart')
    shutdown
    sleep 1
    poweron
    ;;
  *)
    echo "usage $0 [poweron|shutdown|restart]"
    ;;
esac

Bookmarks

http://www.linux-kvm.org/page/Documents
http://www.linux-kvm.org/page/FAQ
http://www.linux-kvm.org/page/Networking
http://www.linux-kvm.org/page/TuningKVM
http://www.linux-kvm.org/page/Migration
http://www.linux-kvm.org/page/USB
http://www.linux-kvm.org/page/Qcow2
http://www.linux-kvm.org/page/Guest
SupportStatus
http://www.linux-kvm.org/page/Management
Tools
http://www.linux-kvm.net/content/latest-release-windows-virtio-network-drivers
http://www.linux-kvm.org/page/Sound
http://en.wikibooks.org/wiki/QEMU/Monitor


BY: Pejman Moghadam
TAG: kvm, kernel, tunctl, socat, qemu, qcow2, serial, epkg, virtualization
DATE: 2011-01-17 14:24:26


Pejman Moghadam / Slackware [ TXT ]