KVM With Gentoo

We use as distribution a gentoo system. The KVM will be installed on a raid1 (mirror) with LVM2. We use the standard procedure to install the gentoo system. I start the installation from an already running system (you can even boot from a boot CD, USB stick or any other linux live system).

The system where I will install is located on /dev/sda. We will start with one harddisk, the second harddisk will be plugged into the system after it is running fine the raid system will mirror the data automatically. The harddisk must be completely empty.

Prepare the harddisk

modprobe raid1
Raid Device /dev/sda missing Type
/dev/sda1 Bios boot partition Bios boot partition 1MiB
/dev/md0 Raid for Gentoo-KVM Raid for Gentoo-KVM Raid-1 (mirror)
parted /dev/sda
mklabel gpt
(say yes if parted asks you)
mkpart boot1 1MiB 2MiB
mkpart kvm1 2MiB 100%
set 1 bios_grub on
set 2 raid on
quit
mdadm --create /dev/md0 --level=mirror --raid-devices=2 /dev/sda1 missing
mdadm --grow /dev/md0 -b internal
pvcreate /dev/md0
vgcreate vg-kvm /dev/md0
lvcreate -L100M -nboot vg-kvm
lvcreate -L4GB -nswap vg-kvm
lvcreate -L30GB -nroot vg-kvm
mkfs.ext2 -L boot /dev/vg-kvm/boot
mkfs.ext4 -L root /dev/vg-kvm/root
mkswap /dev/vg-kvm/swap
swapon /dev/vg-kvm/swap
mount /dev/vg-kvm/root /mnt/gentoo/
mkdir /mnt/gentoo/boot
mount /dev/vg-kvm/boot /mnt/gentoo/boot/

Installation of Gentoo

Make sure you have the following use flags defined in /etc/portage/make.conf

USE="bindist mmx sse sse2 -ldap perl python lvm parted qemu virt-network avahi sasl audit nfs"
MAKEOPTS="-j5"
QEMU_SOFTMMU_TARGETS="i386 x86_64"
QEMU_USER_TARGETS="i386 x86_64"

Follow the normal gentoo install guide here: http://www.gentoo.org/doc/de/handbook/handbook-amd64.xml?part=1&chap=5

Some changes to the gentoo manual (all to be executed in the chroot environment):

echo "=sys-boot/grub-2*">>/etc/portage/package.keywords
echo "sys-boot/grub device-mapper">>/etc/portage/package.use
emerge -av grub lzop lvm2 mdadm genkernel
rc-update add lvm boot
rc-update add mdraid boot

Modify /etc/default/grub:

GRUB_CMDLINE_LINUX_DEFAULT="dolvm domdadm intel_iommu=on,igfx_off,pass-through vga=791 splash=silent,theme:gentoo console=tty1 quiet libata.ignore_hpa=1 cgroup_enable=memory swapaccount=1"

To build the initramfs use:

genkernel --lvm --mdadm --iscsi --disklabel --install initramfs

Setup fstab:

blkid
/dev/loop0: TYPE="squashfs"
/dev/sda2: UUID="9338e0f6-0ed2-8112-a0e2-840b4d9616de" UUID_SUB="fa5fb4fe-6f65-efe6-5cd3-21e7e6c42976" LABEL="Gentoo-20121221:0" TYPE="linux_raid_member"
/dev/md0: UUID="PoGeVn-q9Sb-T7YD-Ysro-oRDG-pB7f-tXRWzr" TYPE="LVM2_member"
/dev/sdb: LABEL="PENDRIVE" UUID="1105-224F" TYPE="vfat"
/dev/mapper/vg--kvm-boot: LABEL="boot" UUID="251d9d9e-2c38-41db-938d-e25ef56afd3a" TYPE="ext2"
/dev/mapper/vg--kvm-swap: UUID="9d77c5f7-ebc8-4a9d-afa9-cd215489aae8" TYPE="swap"
/dev/mapper/vg--kvm-root: LABEL="root" UUID="7702989f-02c0-48dd-b640-3a921797096b" TYPE="ext4"

Now edit the /etc/fstab based on the information:

UUID="251d9d9e-2c38-41db-938d-e25ef56afd3a"             /boot           ext2            noatime  1 2
UUID="7702989f-02c0-48dd-b640-3a921797096b"             /               ext3            noatime         0 1
UUID="9d77c5f7-ebc8-4a9d-afa9-cd215489aae8"             none            swap            sw              0 0

Install GRUB 2

mkdir /boot/grub2
grub-mkconfig -o /boot/grub/grub.cfg
grub-install /dev/sda

Configure KVM

Install required software:

emerge -av qemu usbutils bridge-utils usermode-utilities iptables macchanger virt-manager
rc-update add libvirtd default
gpasswd -a qemu kvm

Enable KSM:

echo "echo 1 > /sys/kernel/mm/ksm/run" >/etc/local.d/ksm.start
chmod +x /etc/local.d/ksm.start

Create certificates:

mkdir -p /tmp/certs
cd /tmp/certs

certtool --generate-privkey > cakey.pem
echo "cn = FM-Data" >ca.info
echo "ca" >>ca.info
echo "cert_signing_key" >>ca.info
certtool --generate-self-signed --load-privkey cakey.pem --template ca.info --outfile cacert.pem

mkdir -p /etc/pki/CA
cp cacert.pem /etc/pki/CA

certtool --generate-privkey > serverkey.pem
echo "organization = FM-Data" >server.info
echo "cn = kvm.idefix.lan" >>server.info
echo "tls_www_server" >>server.info
echo "encryption_key" >>server.info
echo "signing_key" >>server.info
certtool --generate-certificate --load-privkey serverkey.pem --load-ca-certificate cacert.pem --load-ca-privkey cakey.pem --template server.info --outfile servercert.pem

mkdir -p /etc/pki/libvirt/private
cp serverkey.pem /etc/pki/libvirt/private/
cp servercert.pem /etc/pki/libvirt/

certtool --generate-privkey > clientkey.pem
echo "country = DE" >client.info
echo "state = Bayern" >>client.info
echo "locality = Fuerstenfeldbruck" >>client.info
echo "organization = FM-Data" >>client.info
echo "cn = client1" >>client.info
echo "tls_www_client" >>client.info
echo "encryption_key" >>client.info
echo "signing_key" >>client.info
certtool --generate-certificate --load-privkey clientkey.pem --load-ca-certificate cacert.pem --load-ca-privkey cakey.pem --template client.info --outfile clientcert.pem

cp clientkey.pem /etc/pki/libvirt/private/clientkey.pem
cp clientcert.pem /etc/pki/libvirt/clientcert.pem

rm *

Network bridge: Edit vi /etc/conf.d/net

bridge_br0="eth0"
brctl_br0="setfd 0 sethello 0 stp off"
config_br0="dhcp"
cd /etc/init.d/
ln -s net.lo net.br0
rc-update del net.eth0
rc-update add net.br0 default

Enable PCI passthrough:

echo "options kvm allow_unsafe_assigned_interrupts=1" >kvm_iommu_map_guest.conf

Add Second Harddisk and Build Raid1

Install sgdisk:

emerge -av sys-apps/gptfdisk

Copy the GPT partition schema from the original disk to the new disk. The already existing disk is /dev/sda and the new disk is /dev/sdf:

sgdisk -R=/dev/sdf /dev/sda
sgdisk -G /dev/sdf
sgdisk -c 1:"boot2" /dev/sdf
sgdisk -c 2:"kvm2" /dev/sdf

See disk and raid information:

cat /proc/mdstat
blkid
/dev/sdf1: PARTLABEL="boot2" PARTUUID="bea234c8-ce71-4c23-8e67-7faf5d8e50d1"
/dev/sdf2: PARTLABEL="kvm2" PARTUUID="054de73e-7cd6-4c94-860b-3eda62cabc58"

Add the new disk to the raid:

mdadm -a /dev/md126 /dev/disk/by-partuuid/054de73e-7cd6-4c94-860b-3eda62cabc58 

Install boot manager:

grub2-install /dev/sdf

Install Gentoo as Guest

Download the install-amd64-minimal-20130207.iso and place it in the iso directory:

cd /mnt/isos/
wget http://distfiles.gentoo.org/releases/amd64/autobuilds/current-iso/default/20130207/install-amd64-minimal-20130207.iso

Now start virt-manager and we create there the new virtual machine. I created a lvm partition with 30GB and mounted the iso we downloaded. Then follow the normal gentoo installation guide or continue here.

Prepare Harddisk

fdisk /dev/sda
n
p
1
<ENTER>
+100M
n
p
2
<ENTER>
<ENTER>
t
2
8e
a
1
w
pvcreate /dev/sda2
vgcreate vg-knx /dev/sda2
lvcreate -L4GB -nswap vg-knx
lvcreate -l+100%FREE -nroot vg-knx
mkfs.ext2 -L boot /dev/sda1
mkfs.ext4 -L root /dev/vg-knx/root
mkswap -L swap /dev/vg-knx/swap
swapon /dev/vg-knx/swap
mount /dev/vg-knx/root /mnt/gentoo/
mkdir /mnt/gentoo/boot
mount /dev/sda1 /mnt/gentoo/boot
cd /mnt/gentoo

Install Gentoo

links http://www.gentoo.org/main/en/mirrors.xml
(download stage3 in releases/autobuilds/current-stage3/default/..../stage3-amd64....tar.bz2)
q
tar xjpf stage3-*.tar.bz2
echo 'MAKEOPTS="-j2"' >>etc/portage/make.conf
mirrorselect -i -o >> /mnt/gentoo/etc/portage/make.conf
mirrorselect -i -r -o >> /mnt/gentoo/etc/portage/make.conf
cp -L /etc/resolv.conf /mnt/gentoo/etc/
mount -t proc none /mnt/gentoo/proc
mount --rbind /sys /mnt/gentoo/sys
mount --rbind /dev /mnt/gentoo/dev
chroot /mnt/gentoo /bin/bash
source /etc/profile
export PS1="(chroot) $PS1"
mkdir /usr/portage
emerge-webrsync
eselect news list
eselect news read
eselect profile list
eselect profile set 3
emerge --sync --quiet
cp /usr/share/zoneinfo/Europe/Berlin /etc/localtime
echo "Europe/Berlin" > /etc/timezone
emerge =gentoo-sources-3.0.35 eix genkernel syslog-ng vixie-cron dhcpcd grub vim
rc-update add syslog-ng default
rc-update add vixie-cron default
rc-update add sshd default

Prepare the kernel

vi /etc/genkernel.conf
SYMLINK="yes"
MAKEOPTS="-j2"
LVM="yes"
DISKLABEL="yes"
BOOTLOADER="grub"

Compile the kernel now with:

cd /usr/src/linux
genkernel all

Configure the system

vi /etc/fstab
LABEL="boot"            /boot           ext2            noatime         1 2
LABEL="root"            /               ext4            noatime         0 1
LABEL="swap"            none            swap            sw              0 0

Configure hostname:

vi /etc/conf.d/hostname
hostname="knx.idefix.lan"

Configure network:

vi /etc/conf.d/net
config_eth0="dhcp"
cd /etc/init.d
ln -s net.lo net.eth0
rc-update add net.eth0 default

Locale settings:

vi /etc/conf.d/keymaps
vi /etc/locale.gen
en_US ISO-8859-1
en_US.UTF-8 UTF-8
de_DE ISO-8859-1
de_DE@euro ISO-8859-15
de_DE.UTF-8 UTF-8

locale-gen
vi /etc/env.d/02locale
LANG="de_DE.UTF-8"
LC_COLLATE="C"

env-update && source /etc/profile

Install grub

vi /boot/grub/grub.conf
default 0
timeout 0
splashimage=(hd0,0)/grub/splash.xpm.gz

title Gentoo Linux
root (hd0,0)
kernel /kernel real_root=/dev/vg-knx/root dolvm
initrd /initramfs
grep -v rootfs /proc/mounts > /etc/mtab
grub-install --no-floppy /dev/sda

Setup users

passwd
useradd -m -G users,wheel,audio -s /bin/bash idefix
passwd idefix

rm /stage3-*.tar.bz2*

Reboot the system

exit
cd
umount -l /mnt/gentoo/dev{/shm,/pts,}
umount -l /mnt/gentoo{/boot,/proc,}
reboot

Pass Cine-S2 to virtual machine

Find the device with:

lspci

we get:

...
04:00.0 Multimedia controller: Digital Devices GmbH Octopus LE DVB adapter
...

We search for the id of the card:

lspci -n |grep 04:00.0

We get:

04:00.0 0480: dd01:0003

Now we create a startup script to free the card before libvirt tries to start the virtual machine. For this we create a new start script and enable it:

cd /etc/init.d/
echo '#!/sbin/runscript' > pci_passthrough
echo "" >> pci_passthrough
echo "depend() {" >> pci_passthrough
echo "        before libvirtd" >> pci_passthrough
echo "}" >> pci_passthrough
echo "" >> pci_passthrough
echo "start () {" >> pci_passthrough
echo "        echo "dd01 0003" > /sys/bus/pci/drivers/pci-stub/new_id" >> pci_passthrough
echo "        echo 0000:04:00.0 >/sys/bus/pci/devices/0000\:04\:00.0/driver/unbind" >> pci_passthrough
echo "        echo 0000:04:00.0 >/sys/bus/pci/drivers/pci-stub/bind" >> pci_passthrough
echo "}" >> pci_passthrough
echo "" >> pci_passthrough
chmod +x pci_passthrough
rc-update add pci_passthrough default

If you reboot your system now, it frees the pci card with the given id before libvirt tries to start the virtual machines.

Snapshot and upgrade the guest, maybe roll back

We have now the following setup.

vgs
  VG           #PV #LV #SN Attr   VSize   VFree
  boot           1   3   0 wz--n- 148,95g      0
  vg-kvm         1   7   0 wz--n- 232,75g 120,30g
  videostorage   1   1   0 wz--n-   4,00t  20,00g
  LV      VG           Attr     LSize   Pool Origin Data%  Move Log Copy%  Convert
  root    boot         -wi-a---  30,00g
  swap    boot         -wi-a---   2,00g
  video1  boot         -wi-a--- 116,95g
  boot    vg-kvm       -wi-ao-- 100,00m
  freepbx vg-kvm       -wi-a---  19,53g
  iso     vg-kvm       -wi-ao--  10,00g
  knx     vg-kvm       -wi-ao--  29,30g
  root    vg-kvm       -wi-ao--  30,00g
  swap    vg-kvm       -wi-ao--   4,00g
  yavdr   vg-kvm       -wi-ao--  19,53g
  video0  videostorage -wi-ao--   3,98t

Create snapshot

We would like to create now a snapshot of the machine yavdr:

virsh shutdown yavdr

Wait till the virtual machine is shutdown completely. Check it with:

virsh list --all

Create the snapshot:

lvcreate -L4G -s -n /dev/vg-kvm/yavdr-snapshot /dev/vg-kvm/yavdr
virsh start yavdr

We can see the new snapshot now:

lvs vg-kvm
  LV             VG     Attr     LSize   Pool Origin Data%  Move Log Copy%  Convert
  boot           vg-kvm -wi-ao-- 100,00m
  freepbx        vg-kvm -wi-a---  19,53g
  iso            vg-kvm -wi-ao--  10,00g
  knx            vg-kvm -wi-ao--  29,30g
  root           vg-kvm -wi-ao--  30,00g
  swap           vg-kvm -wi-ao--   4,00g
  yavdr          vg-kvm owi-aos-  19,53g
  yavdr-snapshot vg-kvm swi-a-s-   4,00g      yavdr    0,05

Roll back to snapshot

We tested now some stuff and we see it does not work. Stop the virtual machine:

virsh destroy yavdr

Rollback the lvm to the snapshot (this will destroy the snapshot):

lvconvert --merge /dev/vg-kvm/yavdr-snapshot

Start the VM again:

virsh start yavdr

Destroy the snapshot

If everything is fine, destroy the snapshot:

lvremove /dev/vg-kvm/yavdr-snapshot

Atmel

Cross Compile under Gentoo

crossdev avr
sudo crossdev --b 2.19.51.0.8 --g 4.4.1 --without-headers --target avr

Atmel Studio 6

Download and install:

Set WinAVR as Toolchain

Start Atmel Studio 6 and select Tools -> Options. Go to Toolchain Flavour Configuration. Add Flavour and fill in:

Flavour Name: WinAVR 20100110
Toolchain Path: C:\WinAVR-20100110\bin
Make Path: C:\WinAVR-20100110\utils\bin

If you like you can select the new toolchain as default.

AVRDude as One Click Flash Button

To have the possibility to flash our controller with one click and do not have to always go to Device Programming and all other following steps required, I use avrdude from Atmel Studio 6. Go to Tools -> External Tools. Add a new one with the information:

Title: Flash 8-out
Command: avrdude.exe
Arguments: -c stk600 -p m328p -P usb -U flash:w:Debug/app-8-out-full.hex
Initial directory: $(ProjectDir)
Select Use Output window

Select a toolbar group where you want a flash button and click on the small rectangle button -> Add or Remove Buttons -> Customize. Click Add Command, Tools, External Command 1. Put it with the Move Up and Move Down buttons to the position you want it. If you now click on the button it start avrdude and flash the hex file directly to the controller.

 

Compiling a kernel

cd /usr/src/linux
unset CROSS_COMPILE
genkernel --kernel-config=/usr/src/config-idefix-v3 kernel
or
genkernel --no-mrproper --install kernel

Distcc and ccache

I separate the compiling target with the portnumber.

Overview for ports using crosscompile and distcc

CPU System Port
x86 i686-pc-linux-gnu 3632 (standard)
AMD64 x86_64-pc-linux-gnu 3633

Distcc with AMD64

We use port 3633 for AMD64 compiling, so we need to adapt the configuration:

emerge distcc

Now set the right options in the file /etc/conf.d/distccd:

# /etc/conf.d/distccd: config file for /etc/init.d/distccd

DISTCCD_OPTS=""

# this is the distccd executable 
DISTCCD_EXEC="/usr/bin/distccd"

# this is where distccd will store its pid file
DISTCCD_PIDFILE="/var/run/distccd/distccd.pid"

# set this option to run distccd with extra parameters
# Default port is 3632.  For most people the default is okay.
DISTCCD_OPTS="${DISTCCD_OPTS} --port 3633"

# Logging
# You can change some logging options here:
# --log-file FILE
# --log-level LEVEL  [critical,error,warning, notice, info, debug]
#
# Leaving --log-file blank will log to syslog
# example: --log-file /dev/null --log-level warning
# example: --log-level critical

DISTCCD_OPTS="${DISTCCD_OPTS} --log-level critical"

# SECURITY NOTICE:
# It is HIGHLY recomended that you use the --listen option
# for increased security. You can specify an IP to permit connections 
# from or a CIDR mask
# --listen accepts only a single IP
# --allow is now mandatory as of distcc-2.18.
# example:  --allow 192.168.0.0/24
# example:  --allow 192.168.0.5 --allow 192.168.0.150
# example:  --listen 192.168.0.2
DISTCCD_OPTS="${DISTCCD_OPTS} --allow 192.168.0.0/16"
#DISTCCD_OPTS="${DISTCCD_OPTS} --listen 192.168.0.2"

# set this for niceness
# Default is 15
DISTCCD_NICE="15"

Edit /etc/distcc/hosts:

localhost:3633
or
distcc-config --set-hosts localhost:3633

Then start distcc with:

/etc/init.d/distccd restart

Distcc with X86

I ran here a 64-bit gentoo coompiled for AMD64 (x86_64) so we need to do some configuration to compile for x86. At first we need to generate a port overlay, edit /etc/make.conf:

# To get crossdev running
PORTDIR_OVERLAY="/usr/local/portage"

Create the new directory:

mkdir /usr/local/portage

Install cross-compile environment:

emerge crossdev
crossdev -t i686-pc-linux-gnu

Adapt the configuration:

cd /etc/init.d
cp distccd distccd-x86

Edit /etc/init.d/distccd-x86:

#!/sbin/runscript
# $Header: /var/cvsroot/gentoo-x86/sys-devel/distcc/files/2.18/init,v 1.1 2004/10/12 17:21:43 lisa Exp $

depend() {
       need net
       use ypbind
}

start() {
       [ -e "${DISTCCD_PIDFILE}" ] && rm -f ${DISTCCD_PIDFILE} &>/dev/null

       ebegin "Starting distccd"
       chown distcc `dirname ${DISTCCD_PIDFILE}` &>/dev/null
       TMPDIR="${TMPDIR}" \
       PATH="/usr/i686-pc-linux-gnu/gcc-bin/4.1.1:${PATH}" \
       /sbin/start-stop-daemon --start --quiet --startas ${DISTCCD_EXEC} \
       --pidfile ${DISTCCD_PIDFILE} -- \
       --pid-file ${DISTCCD_PIDFILE} -N ${DISTCCD_NICE} --user distcc \
       ${DISTCCD_OPTS}

       eend $?
}

stop() {
       ebegin "Stopping distccd"
       start-stop-daemon --stop --quiet --pidfile "${DISTCCD_PIDFILE}"
       rm -f "${DISTCCD_PIDFILE}"
       eend $?
}
cd /etc/conf.d
cp distccd distccd-x86

Now set the right options in the file /etc/conf.d/distccd-x86:

# /etc/conf.d/distccd: config file for /etc/init.d/distccd

DISTCCD_OPTS=""

# this is the distccd executable 
DISTCCD_EXEC="/usr/bin/distccd"

# this is where distccd will store its pid file
DISTCCD_PIDFILE="/var/run/distccd/distccd-x86.pid"

# set this option to run distccd with extra parameters
# Default port is 3632.  For most people the default is okay.
DISTCCD_OPTS="${DISTCCD_OPTS} --port 3632"

# Logging
# You can change some logging options here:
# --log-file FILE
# --log-level LEVEL  [critical,error,warning, notice, info, debug]
#
# Leaving --log-file blank will log to syslog
# example: --log-file /dev/null --log-level warning
# example: --log-level critical

DISTCCD_OPTS="${DISTCCD_OPTS} --log-level critical"

# SECURITY NOTICE:
# It is HIGHLY recomended that you use the --listen option
# for increased security. You can specify an IP to permit connections 
# from or a CIDR mask
# --listen accepts only a single IP
# --allow is now mandatory as of distcc-2.18.
# example:  --allow 192.168.0.0/24
# example:  --allow 192.168.0.5 --allow 192.168.0.150
# example:  --listen 192.168.0.2
DISTCCD_OPTS="${DISTCCD_OPTS} --allow 192.168.0.0/16"
#DISTCCD_OPTS="${DISTCCD_OPTS} --listen 192.168.0.2"

# set this for niceness
# Default is 15
DISTCCD_NICE="15"

To configure an i686 computer to use our AMD64 for compiling edit the /etc/distcc/hosts on the i686 computer:

192.168.0.151
or
distcc-config --set-hosts localhost 192.168.0.151

Use localhost only if localhost is fast enough elsewhere omit it.

Start distcc-x86 now with:

/etc/init.d/distccd-x86 restart

To start it at computer restart enter:

rc-update add distccd-x86 default

Get status

View which computer compile on our:

DISTCC_DIR="/var/tmp/portage/.distcc" distccmon-text 1
DISTCC_DIR="/var/tmp/portage/.distcc" distccmon-gui

Enable distcc with Gentoo

Edit /etc/make.conf to enable distcc and ccache for portage:

# for 7 CPUs or PCs (CPU*2 +1)
MAKEOPTS="-j15"
FEATURES="distcc ccache"
CCACHE_SIZE="2G"

eibd

eibd for gentoo

There is already a port available which can be used:

layman -f -o ftp://scponly:scponly@erazor-zone.de/portage/layman.xml
layman -a erazor -o  ftp://scponly:scponly@erazor-zone.de/portage/layman.xml
emerge -av eibd

All the tools are not working, so I recompiled the current head version and installed it over the already installed gentoo portage.

git clone http://www.auto.tuwien.ac.at/~mkoegler/git/bcusdk.git
cd bcusdk/
autoreconf -i
./configure --enable-onlyeibd --disable-ft12 --disable-pei16 --disable-tpuart --disable-pei16s --disable-tpuarts --enable-eibnetip --enable-eibnetiptunnel --enable-eibnetipserver --enable-usb --disable-groupcache --prefix=/usr
make
make install

Layman

Layman can be used to use external portage directories via svn and more different programs and integrate all into portage.

Using Layman

At first we need to install layman:

emerge -av layman

Create Own Layman Repository

At first create a virtual host in your apache config and place there a config file for layman. overlay.xml Now add to the file /etc/layman/layman.conf the line:

overlays  : http://www.gentoo.org/proj/en/overlays/layman-global.txt
        http://overlay.idefix.lan/overlay.xml

Edit /etc/make.conf

source /usr/portage/local/layman/make.conf

Now we create the xml file on the server:

<?xml version="1.0" ?>
<layman>
        <overlay
            contact = "idefix@fechner.net"
            name = "idefix"
            src  = "http://overlay.idefix.lan/"
            type = "svn"
            status = "official">

                <link>
                        http://overlay.idefix.lan/svn/overlay/trunk/
                </link>

                <description>
                        Idefix overlay.
                </description>
        </overlay>
</layman>

LVM2

To move an existing installation to a lvm2 the following steps a necessary:

Build initramfs using dracut

Enable it:

echo "sys-fs/lvm2 static" >>/etc/portage/package.use
echo "sys-fs/mdadm static" >>/etc/portage/package.use
echo "sys-apps/busybox static" >>/etc/portage/package.use
echo "media-libs/freetype static-libs" >>/etc/portage/package.use
echo "sys-kernel/dracut" >> /etc/portage/package.keywords
echo "media-libs/freetype" >> /etc/portage/package.keywords
echo "media-gfx/splashutils" >> /etc/portage/package.keywords
echo "sys-kernel/dracut device-mapper" >> /etc/portage/package.use
echo 'DRACUT_MODULES="gensplash lvm mdraid"' >> /etc/make.conf
emerge -av --autounmask-write sys-kernel/dracut
dispatch-conf
emerge -av sys-kernel/dracut

Generate it with:

dracut "" `uname -r`
ln -s /boot/initramfs-`uname -r`.img /boot/initramfs.img

Modify your grub config:

title Gentoo Linux
root (hd0,0)
kernel /vmlinuz root=/dev/vg/root-new
initrd /initramfs.img

Move root to LVM2

At first we have to backup the complete root partition to a backup partition. You can do this by execute:

find / -depth -xdev -print0 |cpio --null --sparse -pVd  /newroot/

This will copy all files from / to /newroot excluding all external mount points.

Adapt the /etc/fstab to point to the correct root partition, in this case:

/dev/vg/root-new /   ext4  defaults 0  1

Create the real root

After we moved our current root to a lvm2 and booted it from there we can completely destroy the old boot harddisk and can recreate it from scratch. I increased the /boot partition to 100MB and the rest is used for LVM2.

Use fdisk to create a 100MB partition and make it bootable, create the lvm partition as a primary partition, it should look like this:

   Geraet  boot.     Anfang        Ende     Bloecke   Id  System
/dev/sda1   *        2048      206847      102400   83  Linux
/dev/sda2          206848   312581807   156187480   8e  Linux LVM

As next mount /dev/sda1 to /boot and install grub:

mke2fs /dev/sda1
mount /dev/sda1 /boot
grub
root (hd0,0)
setup (hd0)
exit

At first we have to create a PV on the harddisk:

pvcreate /dev/sda2

New we can create a new volume group.

vgcreate boot /dev/sda2

Now we create a logical volume for the swap partition:

lvcreate -L2G -nswap boot
mkswap /dev/boot/swap
swapon /dev/boot/swap

Next is create to new partition for the gentoo system and use the rest for VDR:

lvcreate -L30G -nroot boot
mkfs.ext4 /dev/boot/root
lvcreate -l100%FREE -nvideo1 boot
mkfs.ext4 /dev/boot/video1

Now we copy the backup back to the new root partition:

mount /dev/boot/root /newroot/
find / -depth -xdev -print0 |cpio --null --sparse -pVd  /newroot/

Adapt the /etc/fstab:

/dev/sda1               /boot           ext2    defaults        1 2
/dev/boot/swap          none            swap    sw              0 0
/dev/boot/root          /               ext4    defaults        0 1
/dev/boot/video1        /video1         ext4    defaults        0 0
none                    /proc           proc    defaults        0 0
none                    /dev/shm        tmpfs   defaults        0 0

/dev/vg/video0          /video0         ext4    defaults        1 1

And modify the /boot/grub/grub.conf:

title=Gentoo Linux
root (hd0,0)
kernel /vmlinuz root=/dev/boot/root
initrd /initramfs.img

Reboot your system and enjoy your new structure.

Initramfs with genkernel

genkernel --lvm --mdadm --dmraid --iscsi --disklabel initramfs

Portage

Tips around portage

USE Flags

Get description for the USE-FLAGS install

emerge gentoolkit

then type:

equery uses package

For details on USE flags:

less /usr/portage/profiles/use.desc

Display all version from one package

emerge eix
eix <package>

Deinstall a portage

emerge --unmerge media-libs/faad2

QEmu

Installing QEmu

I use QEmu to have a emulated environment to test Xenomai applications without crashing the main system.

We need to enable sdl for qemu to have all display output available, edit /etc/portage/package.use:

app-emulation/qemu-softmmu sdl

To install QEmu on Gentoo we need at first gcc 3.x:

emerge =gcc-3.4.6*
gcc-config i686-pc-linux-gnu-3.4.6
source /etc/profile
emerge app-emulation/qemu
gcc-config i686-pc-linux-gnu-4.1.1
source /etc/profile

Prepare Linux for Qemu

Do as root:

echo 1024 > /proc/sys/dev/rtc/max-user-freq

Compile kernel with tun support.

CONFIG_TUN=m
CONFIG_BRIDGE=m

Install everything for bridging:

emerge net-misc/bridge-utils
emerge sys-apps/usermode-utilities
modprobe tun
modprobe bridge

Note: If you get an error message about unresolved symbols, reboot easily the system then everything should work.

Configure for bridge device now in /etc/conf.d/net:

bridge_add_eth0="br0"
config_eth0=( "null" )
config_br0=( "192.168.0.151/24" )
brctl_br0=( "setfd 0" "sethello 0" "stp off" )
routes_br0=( "default gw 192.168.0.251" )

Add the tun device to your startup config:

cd /etc/init.d
ln -s net.lo net.br0
rc-update add net.br0 default

/etc/init.d/net.eth0 stop
/etc/init.d/net.br0 start
ifconfig br0
ifconfig eth0

Configure Qemu to use a network interface edit /etc/qemu-ifup

#!/bin/sh
#
# ____Address: 192.168.0.1 (guest01)
# ____Netmask: 255.255.255.0
# ___Wildcard: 0.0.0.255
# ____Gateway: 192.168.0.251
#

if test $(sudo /sbin/ifconfig | grep -c $1) -gt 0; then
        sudo /sbin/brctl delif br0 $1
        sudo ifconfig $1 down
fi

sudo /sbin/ifconfig $1 0.0.0.0 promisc up
sudo /sbin/brctl addif br0 $1
chmod +x /etc/qemu-ifup

Create a persistant tun interface:

tunctl -u root -t tap0

Booting OS in Qemu

We want to boot via network so we create first the needed structure in out tftp server.

The structure of the directory is:

tftpboot/
|-- pxelinux.0
|-- pxelinux.cfg
|   |-- default
|   `-- zimage
`-- zimage

The file default has the following input:

DEFAULT qemu
PROMPT 1
TIMEOUT 10

LABEL qemu
  KERNEL zimage
  APPEND vga=0x314 install=nfs://192.168.0.151/qemu

LABEL local
  LOCALBOOT 0

Now configure your dhcp server:

# settings for booting our qemu image
allow booting;
allow bootp;
next-server 192.168.0.251;
filename "pxelinux.0";
mkdir qemu
cd !$
qemu-img create disk.img 4G
qemu -localtime -hda disk.img -boot d -m 256

You should see now some output. If you get the error message

Could not initialize SDL - exiting

Be sure that $DISPLAY is set and execute on the xserver xhost + to allow all hosts to connect to your xserver.

Now download pxe iso from http://rom-o-matic.net/ .

Now we can boot via network with:

qemu -cdrom eb-5.4.2-ns8390.iso -boot d -net tap -net nic,macaddr=52:54:00:12:34:56

Raid 5

Install at first the raid tools:

emerge -av sys-fs/mdadm

Load the raid module:

modprobe raid5

Create partitions on the three hard-disks with fdisk and type fd.

fdisk /dev/sdb
fdisk /dev/sdc
fdisk /dev/sdd

Create md devices:

cd /dev && MAKEDEV md

New create the raid with:

mdadm --create /dev/md0 --level 5 --raid-devices 3 /dev/sd[[bcd]]1

To see progress on build:

watch -n 1 cat /proc/mdstat

Store raid in config file:

mdadm --detail --scan >> /etc/mdadm.conf

To see more details for raid:

mdadm --detail /dev/md0

Sending EMail in Case of an Error

Edit the file /etc/mdadm.conf and add there:

MAILADDR idefix@server.idefix.lan

Test email delivery:

mdadm -Ft --scan

In Case of Failure

You can see in /proc/mdstat which device failed:

md0 : active raid5 sdb1[3] sdd1[2] sdc1[1]
      2930271872 blocks level 5, 64k chunk, algorithm 2 [3/2] [_UU]

Remove it with:

mdadm -r /dev/md0 /dev/sdb1

Create a partition on the new disk with type 0xFD and add it to the raid with:

mdadm -a /dev/md0 /dev/sdb1

You can see now in /proc/mdstat that the raid is recovering the data:

cat /proc/mdstat

Personalities : [linear] [raid0] [raid1] [raid10] [raid6] [raid5] [raid4] [multipath] [faulty]
md0 : active raid5 sdb1[3] sdd1[2] sdc1[1]
      2930271872 blocks level 5, 64k chunk, algorithm 2 [3/2] [_UU]
      [>....................]  recovery =  0.8% (12625280/1465135936) finish=302.8min speed=79923K/sec

unused devices: <none>

Create LVW2

emerge -av lvm2
rc-update add device-mapper boot
rc-update add lvm boot

Scan for devices:

vgscan

mdadm -Ac partitions -m 0 /dev/md0

Convert a normal raid to lvm2

At first we create our backup storage. For this create partition on the disks and select as type raid autodetect. Now we create the raid with:

mdadm --create /dev/md0 --level=stripe --raid-devices=2 /dev/sde1 /dev/sdf1

Now we create the lvm2:

vgscan
vgchange -a y
pvcreate /dev/md0
vgcreate vg /dev/md0
lvcreate -L20G -nyavdr vg
lvcreate -L3T -nbackup-video0 vg
mkfs.ext4 /dev/vg/backup-video0

Now we mount the disks:

mount /dev/vg/backup-video0 /mnt/backup-video0
mount video:/video0 /video0

Now we copy the data:

cd /video0
rsync -av --delete . /mnt/backup-video0/

Online Resize Raid5

Replace the first disk with a new one like it is descriped in case of a disk failure. Wait till the raid is restored. Repeat that step for every disk in your raid.

Now we have to resize the raid:

mdadm -G /dev/md0 -z max

You have to wait till the resize is complete, check with:

cat /proc/mdstat

Now we resize the lvm:

pvresize /dev/md0

Now we resize the logical volume:

lvresize -l +100%FREE /dev/vg/video0

Now we resize the filesystem, I use ext4 so do:

resize2fs /dev/vg/video0

The RC System

Add script to startup:

rc-update add postfix default

Show which script is executed

rc-update show

Remove package from startup

rc-update del postfix default

Update Gentoo

To update the ports enter:

emerge --sync

or if a firewall blacks you enter:

emerge-webrsync

To display packages which need an update enter:

emerge --pretend --verbose world

To update all packages where the USE-FLAG has changed:

emerge --update --deep --newuse world

To update the complete system enter:

emerge --update --deep world

To Update the /etc directory

etc-update

To update dependency execute:

revdep-rebuild

Upgrade to gcc 4.1

see here

VDR

General

I have here an AMD64 but with the 64-bit version of Gentoo I had several problem with some VDR plugins so I went back to the 32-bit version. To install Gentoo download the livecd and use the GUI installer, see Gentoo documentation how to do it (I used 2006.1).

After the installation is finished boot your new gentoo.

Edit make.conf and add:

LIRC_DEVICES="serial"

Now we must install some packages first which VDR or the plugins I use needs to compile correctly.

emerge --sync
emerge portage
etc-update
emerge gentoolkit eix gentoo-sources mc app-admin/sudo

Now compile the kernel with your DVB drivers and everything else, reboot your new kernel.

Now we install software needed by VDR:

emerge lirc imagemagick sys-libs/libcap media-libs/libmad media-libs/libid3tag media-libs/libvorbis media-libs/libsndfile \
       media-libs/libdvdnav media-libs/a52dec dev-libs/boost media-libs/gd cvs subversion

Driver for new Cards

http://mercurial.intuxication.org/hg/s2-liplianin

Compile new Driver

To get the driver:

cd /usr/local/src/
mkdir hvr4000
mkdir -p hvr4000/liblianindvb-test
cd hvr4000/liblianindvb-test
hg init
hg pull http://arvdr-dev.free-x.de:8080/testdvb/
hg update

Compile the driver:

make
make install

To compile VDR:

cd /usr/local/src/hvr4000/liblianindvb-test/linux/include/linux
ln -s /usr/src/linux/include/linux/compiler.h .

vdpau Support

Make sure you have a current gcc on the system, I used gcc-4.3.4.

gcc-config -l
gcc-config 2
source /etc/profile

I use the following versions:

nvidia 190.53 (install with sh /usr/portage/distfiles/NVIDIA-Linux-x86-190.53-pkg0.run)
kernel 2.6.32-gentoo-r1
vdr 1.7.10

Required libs:

emerge -av media-video/vcdimager
emerge -av media-sound/musepack-tools

Make sure you do not have any xine or xine-lib installed, search with the command and decide to delete necessary files:

cd /usr
find . -name "*xine*"

Download the following files: xine-lib from hg repository http://hg.debian.org/hg/xine-lib/xine-lib-1.2-vdpau .

cd /usr/local/src
hg pull http://hg.debian.org/hg/xine-lib/xine-lib-1.2-vdpau

Compile xine-lib:

cd /usr/local/src/xine-lib-1.2-vdpau
./autogen.sh --prefix=/usr --disable-xinerama
make
make install

Compile Plugin for VDR:

cvs -z3 -d:pserver:anonymous@xineliboutput.cvs.sourceforge.net:/cvsroot/xineliboutput co vdr-xineliboutput

Copy the checkout to you vdr/PLUGINS/src folder and compile it with make plugins.

Copy to your /etc/vdr/setup.conf:

xineliboutput.Audio.Compression = 100
xineliboutput.Audio.Delay = -120
xineliboutput.Audio.Headphone = 0
xineliboutput.Audio.SoftwareVolumeControl = 0
xineliboutput.Audio.Speakers = Pass Through
xineliboutput.Audio.Surround = 0
xineliboutput.Audio.Upmix = 0
xineliboutput.Audio.Visualization = goom
xineliboutput.Audio.Visualization.GoomOpts = width=400,height=300,fps=25
xineliboutput.Frontend  ======
xineliboutput.Media.BrowseFilesDir = /video0/storage/videos/erotik/neu/Real.Wife.Stories.-.The.Switcheroo
xineliboutput.Media.BrowseImagesDir = /video0
xineliboutput.Media.BrowseMusicDir = /video0/storage/mp3s-server/Doogie
xineliboutput.Media.CacheImplicitPlaylists = 1
xineliboutput.Media.DVD.ArrowKeysControlPlayback = 1
xineliboutput.Media.EnableID3Scanner = 1
xineliboutput.Media.MenuItems = -1
xineliboutput.OSD.AlphaCorrection = 0
xineliboutput.OSD.AlphaCorrectionAbs = 0
xineliboutput.OSD.Blending = 1
xineliboutput.OSD.BlendingLowRes = 0
xineliboutput.OSD.DvbSubtitles = 0
xineliboutput.OSD.ExtSubSize = -1
xineliboutput.OSD.Height = 576
xineliboutput.OSD.HideMainMenu = 0
xineliboutput.OSD.LayersVisible = 4
xineliboutput.OSD.Scaling = 2
xineliboutput.OSD.Size = automatic
xineliboutput.OSD.Width = 720
xineliboutput.Playlist.Album = 1
xineliboutput.Playlist.Artist = 1
xineliboutput.Playlist.Tracknumber = 0
xineliboutput.Post.denoise3d.chroma = 30
xineliboutput.Post.denoise3d.Enable = 0
xineliboutput.Post.denoise3d.luma = 40
xineliboutput.Post.denoise3d.time = 60
xineliboutput.Post.pp.Enable = 0
xineliboutput.Post.pp.Mode = de
xineliboutput.Post.pp.Quality = 3
xineliboutput.Post.unsharp.chroma_amount = 0
xineliboutput.Post.unsharp.chroma_matrix_height = 3
xineliboutput.Post.unsharp.chroma_matrix_width = 3
xineliboutput.Post.unsharp.Enable = 0
xineliboutput.Post.unsharp.luma_amount = 0
xineliboutput.Post.unsharp.luma_matrix_height = 5
xineliboutput.Post.unsharp.luma_matrix_width = 5
xineliboutput.Remote.AllowHttp = 1
xineliboutput.Remote.AllowHttpCtrl = 0
xineliboutput.Remote.AllowRtsp = 1
xineliboutput.Remote.AllowRtspCtrl = 0
xineliboutput.Remote.Iface  ======
xineliboutput.Remote.Keyboard = 1
xineliboutput.Remote.ListenPort = 37890
xineliboutput.Remote.LocalIP  ======
xineliboutput.Remote.MaxClients = 10
xineliboutput.Remote.Rtp.Address = 224.0.1.9
xineliboutput.Remote.Rtp.AlwaysOn = 0
xineliboutput.Remote.Rtp.Port = 37890
xineliboutput.Remote.Rtp.SapAnnouncements = 1
xineliboutput.Remote.Rtp.TTL = 1
xineliboutput.Remote.UseBroadcast = 1
xineliboutput.Remote.UseHttp = 1
xineliboutput.Remote.UsePipe = 1
xineliboutput.Remote.UseRtp = 0
xineliboutput.Remote.UseTcp = 1
xineliboutput.Remote.UseUdp = 1
xineliboutput.RemoteMode = 1
xineliboutput.Video.AspectRatio = 0
xineliboutput.Video.AutoCrop = 0
xineliboutput.Video.AutoCrop.AutoDetect = 1
xineliboutput.Video.AutoCrop.DetectSubs = 1
xineliboutput.Video.AutoCrop.FixedSize = 1
xineliboutput.Video.AutoCrop.SoftStart = 1
xineliboutput.Video.Brightness = -1
xineliboutput.Video.Contrast = -1
xineliboutput.Video.Decoder.H264 = automatic
xineliboutput.Video.Decoder.MPEG2 = automatic
xineliboutput.Video.Deinterlace = tvtime
xineliboutput.Video.DeinterlaceOptions = method=Linear,cheap_mode=1,pulldown=none,framerate_mode=full,judder_correction=1,use_progressive_frame_flag=1,chroma_filter=0,enabled=1
xineliboutput.Video.HUE = -1
xineliboutput.Video.IBPTrickSpeed = 1
xineliboutput.Video.MaxTrickSpeed = 12
xineliboutput.Video.NoiseReduction = -1
xineliboutput.Video.Overscan = 0
xineliboutput.Video.Saturation = -1
xineliboutput.Video.Sharpness = -1
xineliboutput.Video.SwScale = 0
xineliboutput.Video.SwScale.Aspect = 0
xineliboutput.Video.SwScale.Downscale = 0
xineliboutput.Video.SwScale.Height = 576
xineliboutput.Video.SwScale.Resize = 0
xineliboutput.Video.SwScale.Width = 720

Edit the file ~/.xine/config_xineliboutput

.version:2
audio.device.alsa_default_device:hw:0,3
audio.device.alsa_front_device:hw:0,3
audio.device.alsa_passthrough_device:hdmi:CARD=NVidia,DEV=0
audio.output.speaker_arrangement:Pass Through
audio.synchronization.av_sync_method:resample
video.processing.ffmpeg_thread_count:4
effects.goom.fps:25
effects.goom.height:576
effects.goom.width:720
engine.buffers.video_num_buffers:250
engine.buffers.video_num_frames:22
engine.performance.memcpy_method:mmxext

To test everything, start the X server with:

xinit

Start VDR with

runvdr

Start the display with

/usr/local/bin/vdr-sxfe --post tvtime:use_vo_driver,cheap_mode=0,pulldown=0,use_progressive_frame_flag=1 --fullscreen --reconnect xvdr+tcp://127.0.0.1:37890 --video=vdpau

You should see know a picture on your display.

To start everything automatically copy the vdr-sxfe line into a file /usr/local/bin/start-vdr-sxfe and make it executable. Change /etc/inittab and add the line:

sxfe:3:respawn:/usr/local/bin/start-vdr-sxfe

VDPAU Support Gentoo Way

Add an overlay:

layman -a vdr-xine
layman -S
eix-sync

Edit /etc/portage/package.unmask

media-libs/xine-lib

Edit /etc/portage/package.keywords

media-libs/xine-lib

Install it with:

emerge -av xine-lib

make sure here that really version 9999 is installed (the mecurial version) and check there in the configure output while compiling that vdpau is shown.

PS3 Remote Control and VDR

Install required software:

emerge -av dev-python/dbus-python dev-python/pygobject net-wireless/bluez

Add bluetooth to start-up:

rc-update add bluetooth boot
/etc/init.d/bluetooth start

Gentoo