Oclane blog
Talking about software

Creating an Alpine Linux OpenVZ container template

Thanks to this post, I've managed to create a wonderfull tiny OpenVZ container template for running the latest Alpine Linux on ProxMox VE

If you don't know yet Alpine Linux:

Alpine Linux is a security-oriented, lightweight Linux distribution based on musl libc and busybox.

Following the mentionned post, I've written a small bash script that create the initial image... Here it is:

#!/bin/bash
# (c)2015, didier Belot <dib @ oclane . net>

# create a minimalist Alpine Linux image for using as an OpenVZ template
# adapted from: http://xmeblog.blogspot.fr/2015/02/openvz-create-alpine-linux-container.html

# ensure we are root or sudo

[ "`id -u`" == "0" ] || { echo "You must be root for this."; exit 1; }

export mirror="http://nl.alpinelinux.org/alpine/"
export chroot_dir="/tmp/alpine32"

[ -z "$EDITOR" ] && EDITOR=vi

# step 2
download_static_apk() {
    echo "## Downloading static apk..."
    wget ${mirror}/v3.2/main/x86_64/apk-tools-static-2.6.3-r0.apk
    # ignore normal error about unknow headers...
    tar -xzf apk-tools-static-2.6.3-r0.apk 2>/dev/null
    test -f ./sbin/apk.static &&
    echo "* done"
}

# step 3
create_base_image() {
    echo "## Creating base image in ${chroot_dir}..."
    mkdir -p ${chroot_dir}
    ./sbin/apk.static -X ${mirror}/v3.2/main -U --allow-untrusted --root ${chroot_dir} --initdb add alpine-base
    echo "* done"
}

# step 4
create_image_devices() {
    echo "## Creating /dev in ${chroot_dir}..."
    mknod -m 666 ${chroot_dir}/dev/full c 1 7
    mknod -m 666 ${chroot_dir}/dev/ptmx c 5 2
    mknod -m 644 ${chroot_dir}/dev/random c 1 8
    mknod -m 644 ${chroot_dir}/dev/urandom c 1 9
    mknod -m 666 ${chroot_dir}/dev/zero c 1 5
    mknod -m 666 ${chroot_dir}/dev/tty c 5 0
    rm -rf  ${chroot_dir}/dev/null
    mknod -m 666 ${chroot_dir}/dev/null c 1 3
    echo "* done"
}

# step 5
create_fstab() {
    echo "## Creating /etc/fstab..."
cat >${chroot_dir}/etc/fstab <<EOF
# START -------------------------------------
none /dev/pts devpts rw,gid=5,mode=620 0 0
none /dev/shm tmpfs defaults 0 0
# END ---------------------------------------
EOF
    read -p '<ENTER> to edit/verify the inittab'
    $EDITOR ${chroot_dir}/etc/fstab
    echo "* done"
}

# step 5, cont.
patch_inittab() {
    echo "## Patching inittab..."
    TAB=${chroot_dir}/etc/inittab
    sed -i 's/^tty/#tty/g' $TAB
    sed -i 's/^::ctrl/#::ctrl/g' $TAB
    read -p '<ENTER> to edit the inittab'
    $EDITOR $TAB
    echo "* done"
}

# step 6 (optional)
set_repositories() {
    echo "## Setup repositories..."
    echo "${mirror}/v3.2/main/" > ${chroot_dir}/etc/apk/repositories
    echo "* done"
}

# step 7
add_openssh() {
    echo "## Adding openssh..."
    ./sbin/apk.static -X ${mirror}/v3.2/main -U --allow-untrusted --root ${chroot_dir} add openssh
    echo "* done"
}

# step 8 (in chroot)
go_chroot() {
    echo "## Your job in chroot..."
    mount -t proc none ${chroot_dir}/proc
    mount -o bind /sys ${chroot_dir}/sys

cat << EOF4
#------------------------------------------
# what you need to type while in in chroot:
#------------------------------------------

rc-update add hostname default
rc-update add localmount default
rc-update add klogd default
rc-update add networking default
rc-update add syslog default
rc-update add dmesg default
rc-update add sshd default

# (Then Hit Ctrl-D to exit the chroot)
EOF4
    chroot ${chroot_dir} /bin/sh -l
    echo "* done"
}

# step 9
archive() {
    echo "## Creating the image archive..."
    #exit #exit from chroot
    umount ${chroot_dir}/proc
    umount ${chroot_dir}/sys
    tar zcf ./alpine-3.2.3-x86_64.tar.gz -C ${chroot_dir} .
    echo "* done"
}

download_static_apk &&
create_base_image &&
create_image_devices &&
create_fstab &&
patch_inittab &&
set_repositories &&
add_openssh &&
go_chroot &&
archive &&
echo "* All done, here is your alpine template: "
ls -lh  alpine*.tar.gz
echo "Congratulations."


Create a file named 'build_alpine_iamge.sh', for example, cut/paste the code, and run it with:

sh build_alpine_image.sh

Enjoy!

Written by didier Belot on Friday December 18, 2015
Permalink - Tags: alpine-linux, openvz, bash

Leave a comment

comments powered by Disqus

-