OpenVZ Ubuntu Templates

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.
Basically:
sudo mkdir -p /vz/private/777
sudo debootstrap – -variant=buildd – -arch i386 jaunty /vz/private/777 http://archive.ubuntu.com/ubuntu/
Template Configuration
Set up the template :
ON THE HOST
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 192.168.0.77 – -nameserver 192.168.0.1 – -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 \
avahi-daemon
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 http://us.archive.ubuntu.com/ubuntu/ jaunty main universe multiverse
deb http://us.archive.ubuntu.com/ubuntu/ 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)”
start)
process_options
log_action_begin_msg “Configuring network interfaces”
# usplash_timeout 120
if [ "$VERBOSE" != no ]; then
if ifup -a; then
log_action_end_msg $?
else
log_action_end_msg $?
fi
else
if ifup -a >/dev/null 2>&1; then
log_action_end_msg $?
else
log_action_end_msg $?
fi
fi
# 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:
LANG=”en_US.UTF-8″
LANGUAGE=”en_US.UTF-8″
LC_ALL=”en_US.UTF-8″
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
#!/bin/sh
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
EOF
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 mtab.sh remove
update-rc.d -f klogd remove
update-rc.d -f ondemand remove
# Set hostname to “localhost”
echo “localhost” > /etc/hostname
echo “127.0.0.1 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.
exit
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 192.168.0.88 – -nameserver 192.168.0.1 – -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.
Posted in Linux
[...] Original post: Shadows of epiphany » Blog Archive » OpenVZ Ubuntu Templates [...]
Pingback by Shadows of epiphany » Blog Archive » OpenVZ Ubuntu Templates | Webmaster Tools — August 28, 2009 @ 5:19 am
[...] See the rest here: Shadows of epiphany » Blog Archive » OpenVZ Ubuntu Templates [...]
Pingback by Webmaster Crap » Blog Archive » Shadows of epiphany » Blog Archive » OpenVZ Ubuntu Templates — August 28, 2009 @ 5:54 am
Very nice, thanks!
Comment by toggles — August 28, 2009 @ 6:15 am
[...] may make a template as outlined here, but the guest will not start [...]
Pingback by Shadows of epiphany » Blog Archive » OpenVZ Ubuntu 9.10 Templates — October 27, 2009 @ 2:45 pm
[...] templates have been generated according to this how to and include this init script (which is modified from the script provieded by Stephane [...]
Pingback by Shadows of epiphany » Blog Archive » Download Ubuntu 9.10 OpenVZ Templates — November 13, 2009 @ 11:48 am
I’m seeing /sbin/init core dumps in my Ubuntu 9.10 amd64 images, is anyone else having similar issues?
Thanks,
-Dustin
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/libdbus-1.so.3…(no debugging symbols found)…done.
Loaded symbols for /lib/libdbus-1.so.3
Reading symbols from /lib/libpthread.so.0…(no debugging symbols found)…done.
Loaded symbols for /lib/libpthread.so.0
Reading symbols from /lib/librt.so.1…(no debugging symbols found)…done.
Loaded symbols for /lib/librt.so.1
Reading symbols from /lib/libc.so.6…(no debugging symbols found)…done.
Loaded symbols for /lib/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2…(no debugging symbols found)…done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
Core was generated by `init -z ‘.
Program terminated with signal 6, Aborted.
#0 0×00002b091dee7770 in sigprocmask () from /lib/libc.so.6
(gdb) backtrace
#0 0×00002b091dee7770 in sigprocmask () from /lib/libc.so.6
#1 0×00002b091d406be8 in ?? () from /sbin/init
#2
#3 0×00002b091dee74b5 in raise () from /lib/libc.so.6
#4 0×00002b091deeaf50 in abort () from /lib/libc.so.6
#5 0×00002b091d4212be in ?? () from /sbin/init
#6 0×00002b091deecc12 in exit () from /lib/libc.so.6
#7 0×00002b091d40ac29 in ?? () from /sbin/init
#8 0×00002b091d40b360 in ?? () from /sbin/init
#9 0×00002b091d40b92a in ?? () from /sbin/init
#10 0×00002b091d40cb01 in ?? () from /sbin/init
#11 0×00002b091d40df42 in event_poll () from /sbin/init
#12 0×00002b091d41e325 in ?? () from /sbin/init
#13 0×00002b091d407268 in main () from /sbin/init
Comment by Dustin — February 1, 2010 @ 5:46 pm
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…
Comment by Alexander — February 12, 2010 @ 8:55 am