OpenVZ Ubuntu Templates

OpenVZ Logo

OpenVZ is used for Virtualization and is both light weight (minimal software on the host, guests have small footprints on the hard drive, and minimal use of RAM) and fast (native speed).

In this post I will walk you through how I create Ubuntu Templates. I created templates using Jaunty (Ubuntu 9.04) for this walk through, it also works with Karmic (Ubuntu 9.10). You may need to make a few (minor) modifications for earlier versions of Ubuntu.

This walk through is based on OpenVZ Debian Template and assumes you have OpenVZ set up and that you know the basics of chroot, debootstrap, and openvz commands.

To make a template, use Debootstrap. for this how-to the chroot (location to build the template) is /vz/private/777.

Note: On Debian / Ubuntu hosts I mount /var/lib/vz at /vz by adding this line in /etc/fstab :

/var/lib/vz /vz bind bind 0 0

To create a template, follow the Ubuntu wiki Debootstrap page.

I assume you were able thus create what will be a chroot at /vz/private/777 via deboostrtap.


sudo mkdir -p /vz/private/777
sudo debootstrap – -variant=buildd – -arch i386 jaunty /vz/private/777

Template Configuration

Set up the template :

sudo vzctl set 777 – -applyconfig vps.basic – -save
sudo sh -c ‘echo “OSTEMPLATE=ubuntu-9.04-i386-minimal” >> /etc/vz/conf/777.conf’

# Set an ipaddress on the guest (adjust for your network)
sudo vzctl set 777 – -ipadd – -nameserver – -save

Start the template

sudo vzctl start 777

The next series of steps will configure your template. DO NOT RUN THESE COMMANDS ON THE HOST !!!

Enter into the template

sudo vzctl enter 777

Remove unnecessary packages (all one line, broken up with \):

apt-get remove – -purge – -auto-remove busybox-initramfs console-setup dmidecode eject \
initramfs-tools klibc-utils laptop-detect libklibc \
module-init-tools ntpdate pciutils ubuntu-minimal \
udev usbutils xkb-data tasksel tasksel-data dhcp3-client dhcp3-common \

rm -rf /lib/udev

Disable getty.

initctl stop tty1
initctl stop tty2
initctl stop tty3
initctl stop tty4
initctl stop tty5
initctl stop tty6

rm /etc/event.d/tty*
rm /etc/init/tty*

Lock down the root account. If you lock the root account, and wish to access the guest via ssh, you will need to create additional user accounts and configure sudo (in the template). I do this after bringing up a guest and so it is not part of template configuration.

chmod 700 /root
usermod -p ‘!’ root

Install a few packages (you need quota, vim/nano are optional).

apt-get install quota nano vim

Optional: Remove openssh-server. This depends on if you prefer to ssh directly into the guest or ssh into the host and use “vzctl enter” to access the guest .

apt-get remove openssh-server

Edit a few config files

Continuing in the chroot (do not do this on the host).

Edit /etc/apt/sources.list, add these repositories (if you so desire).

deb jaunty main universe multiverse
deb jaunty-security main universe multiverse

Edit /etc/init.d/networking .

# Fix networking
#Add this line near the top, just after “[ -x /sbin/ifup ] || exit 0”
[ -d /var/run/network ] || mkdir /var/run/network

# Makes networking faster
# comment out the line “”usplash_timeout” lines in start)”

log_action_begin_msg “Configuring network interfaces”
# usplash_timeout 120
if [ “$VERBOSE” != no ]; then
if ifup -a; then
log_action_end_msg $?
log_action_end_msg $?
if ifup -a >/dev/null 2>&1; then
log_action_end_msg $?
log_action_end_msg $?
# usplash_timeout 15

Fix ipv6 .

ln -s /bin/true /sbin/modprobe

Set a few aliases. This is optional, but I set these in /root/.bashrc .

alias ll=’ls -l’
alias la=’ls -A’
alias nano=”nano -w”
alias cp=”cp -i”
alias mv=”mv -i”
alias rm=”rm -i”

Set locale (adjust your language accordingly).

apt-get install language-pack-en
locale-gen en_US.UTF-8
/usr/sbin/update-locale LANG=”en_US.UTF-8″ LANGUAGE=”en_US.UTF-8″ LC_ALL=”en_US.UTF-8″

Next edit /etc/environment and define your environmental variables:


Package the template

Run these commands in the TEMPLATE , not the host.

# clean your packages
apt-get clean
apt-get autoremove

#Generate a unique set of ssh (host) keys.
rm -f /etc/ssh/ssh_host_*

cat << EOF > /etc/rc2.d/S15ssh_gen_host_keys
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ”
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ”
rm -f \$0

chmod a+x /etc/rc2.d/S15ssh_gen_host_keys

# Set mtab and
rm -f /etc/mtab
ln -s /proc/mounts /etc/mtab

# disable some unnecessary boot scripts
update-rc.d -f remove
update-rc.d -f klogd remove
update-rc.d -f ondemand remove

# Set hostname to “localhost”
echo “localhost” > /etc/hostname
echo “ localhost.localdomain localhost” >> /etc/hosts

# Make ptyp
cd /dev && /sbin/MAKEDEV ptyp

# Clear log files
> /etc/resolv.conf \
> /var/log/messages; > /var/log/auth.log; > /var/log/kern.log; > /var/log/bootstrap.log; \
> /var/log/dpkg.log; > /var/log/syslog; > /var/log/daemon.log; > /var/log/apt/term.log; rm -f /var/log/*.0 /var/log/*.1

Exit the template.


On the HOST stop the template and package.

vzctl set 777 – -ipdel all – -save
vzctl stop 777

Package with tar

cd /vz/private/777
tar czf /vz/template/cache/ubuntu-9.04-<arch>-minimal.tar.gz .

Test the template

sudo vzctl create 888 – -ostemplate ubuntu-9.04-i386-minimal
sudo vzctl set 888 – -ipadd – -nameserver – -hostname ubuntu-minimal – -save
sudo vzctl start 888

If all went well you should have a working ubuntu template.

I am always looking for comments or feedback on my templates.

This entry was posted in Linux and tagged , . Bookmark the permalink.

7 Responses to OpenVZ Ubuntu Templates

  1. Pingback: Shadows of epiphany » Blog Archive » OpenVZ Ubuntu Templates | Webmaster Tools

  2. Pingback: Webmaster Crap » Blog Archive » Shadows of epiphany » Blog Archive » OpenVZ Ubuntu Templates

  3. toggles says:

    Very nice, thanks!

  4. Pingback: Shadows of epiphany » Blog Archive » OpenVZ Ubuntu 9.10 Templates

  5. Pingback: Shadows of epiphany » Blog Archive » Download Ubuntu 9.10 OpenVZ Templates

  6. Dustin says:

    I’m seeing /sbin/init core dumps in my Ubuntu 9.10 amd64 images, is anyone else having similar issues?



    root@ubuntu-9:/# ls -l /core.1632*
    -rw——- 1 root root 348160 Feb 1 23:39 /core.16326
    -rw——- 1 root root 348160 Feb 1 23:39 /core.16328
    root@ubuntu-9:/# gdb /sbin/init core.16326
    GNU gdb (GDB) 7.0-ubuntu
    Copyright (C) 2009 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law. Type “show copying”
    and “show warranty” for details.
    This GDB was configured as “x86_64-linux-gnu”.
    For bug reporting instructions, please see:

    Reading symbols from /sbin/init…(no debugging symbols found)…done.
    Loaded symbols for /sbin/init
    Reading symbols from /lib/…(no debugging symbols found)…done.
    Loaded symbols for /lib/
    Reading symbols from /lib/…(no debugging symbols found)…done.
    Loaded symbols for /lib/
    Reading symbols from /lib/…(no debugging symbols found)…done.
    Loaded symbols for /lib/
    Reading symbols from /lib/…(no debugging symbols found)…done.
    Loaded symbols for /lib/
    Reading symbols from /lib64/…(no debugging symbols found)…done.
    Loaded symbols for /lib64/
    Core was generated by `init -z ‘.
    Program terminated with signal 6, Aborted.
    #0 0x00002b091dee7770 in sigprocmask () from /lib/
    (gdb) backtrace
    #0 0x00002b091dee7770 in sigprocmask () from /lib/
    #1 0x00002b091d406be8 in ?? () from /sbin/init
    #3 0x00002b091dee74b5 in raise () from /lib/
    #4 0x00002b091deeaf50 in abort () from /lib/
    #5 0x00002b091d4212be in ?? () from /sbin/init
    #6 0x00002b091deecc12 in exit () from /lib/
    #7 0x00002b091d40ac29 in ?? () from /sbin/init
    #8 0x00002b091d40b360 in ?? () from /sbin/init
    #9 0x00002b091d40b92a in ?? () from /sbin/init
    #10 0x00002b091d40cb01 in ?? () from /sbin/init
    #11 0x00002b091d40df42 in event_poll () from /sbin/init
    #12 0x00002b091d41e325 in ?? () from /sbin/init
    #13 0x00002b091d407268 in main () from /sbin/init

  7. Alexander says:

    I’m having a similar problem with the core dumps from init in a container that I upgraded from 9.04. So far I’ve just been ignoring it and deleting the core files…

Leave a Reply

Your email address will not be published. Required fields are marked *