Dealing with XEN guest templates

Introduction

We’re using LVM2 thin-provisioning for our guests.

There are two ways for us to deal with lvm drbd/thin clones

  1. GNU tar vs. bsdtar with --numeric-owner
  2. partclone (partitions needs to be the EXACT SAME SIZE)

Preliminary steps

eventually make a guest template out of this and unless you want to proceed with a full post-install

    ls -lF lala/etc/ssh/ssh_host_*
    rm -f lala/etc/ssh/ssh_host_*

eventually re-create lost+found

    # xen
    btrfsck $guest.btrfs

    # kvm
    btrfsck ${loop}p1

we’re going to store template in that folder (assuming /data/ is NFS, GFS2 or OCFS2 powered)

    mkdir /data/templates/

deploy as new guest

    guest=dnc9
    mkdir -p /data/guests/$guest/lala/
    cd /data/guests/$guest/

    cat > $guest <<-EOF
    kernel = "/data/kernels/vmlinuz"
    root = "/dev/xvda1 ro console=hvc0 mitigations=off"
    #extra = "init=/bin/bash"
    name = "$guest"
    memory = 1024
    vcpus = 2
    disk = ['phy:/dev/drbd/by-res/$guest/0,xvda1,w']
    vif = [ 'bridge=guestbr0, vifname=$guest' ]
    type = "pvh"
    EOF

PLAN A — with GNU tar

backup as tar

    guest=slack
guest=jammy
guest=bullseye

    mkdir -p /data/guests/$guest/lala/
    cd /data/guests/$guest/

mount $guest.btrfs lala/
    cd lala/
    tar cpSf /data/templates/$guest.tar --numeric-owner *
ls -lhF /data/templates/$guest.tar
    cd ../
    umount lala/
    rmdir lala/

mkfs.btrfs /dev/drbd/by-res/$guest/0
    #mkfs.reiser4 -dy /dev/drbd/by-res/$guest/0

    mount /dev/drbd/by-res/$guest/0 lala/
    tar xpf /data/templates/slack.tar --numeric-owner -C lala/
    umount lala/
rmdir lala/

btrfsck /dev/drbd/by-res/$guest/0
    #fsck.reiser4 -y /dev/drbd/by-res/$guest/0

    xl create /data/guests/$guest/$guest -c

    lsmod | grep tmem
    free -m

PLAN B — with Partclone

WARNING: VOLUME SIZE NEEDS TO BE EXACTLY THE SAME! (which misses the point of doing file-system level backup anyhow)

backup as Partclone archive

tpl=slack
tpl=jammy
tpl=bullseye

rm -f /data/templates/$tpl.pcl
    partclone.btrfs --clone --source $guest.btrfs --output /data/templates/$tpl.pcl
ls -lhF /data/templates/$tpl.pcl
du -sh /data/templates/$tpl.pcl # idem

deploy as new guest (see warning above)

guest=dnc9

    drbdadm status $guest
    partclone.btrfs --restore --source /data/templates/$tpl.pcl --output /dev/drbd/by-res/$guest/0

mkdir -p /data/guests/$guest/lala/
cd /data/guests/$guest/
mount -o compress=lzo /dev/drbd/by-res/$guest/0 lala/
btrfs device usage lala/ | grep -i 'device slack'
btrfs filesystem resize max lala/
umount lala/
rmdir lala/

and clean-up

cd ../
rm -rf $tpl/

Resources

partclone

Thomas-Tsai / partclone https://github.com/Thomas-Tsai/partclone

How to use partclone to create a “smart” partition backup https://linuxconfig.org/how-to-use-partclone-to-create-a-smart-partition-backup

btrfs

https://linuxhint.com/enable-btrfs-filesystem-compression/

https://linuxhint.com/resize_a_btrfs_filesystem/

https://www.thegeekdiary.com/how-to-resize-expand-a-btrfs-volume-filesystem/


HOME | GUIDES | LECTURES | LAB | SMTP HEALTH | NU | HTML5
Copyright © 2022 Pierre-Philipp Braun