Tweak templates for quantal 64 to ease integration with vagrant

This commit is contained in:
Fabio Rehm 2013-03-07 01:58:34 -03:00
parent 3a2ffbd06f
commit 8e1a656cc6
5 changed files with 120 additions and 112 deletions

1
.gitignore vendored
View file

@ -25,4 +25,5 @@ doc/
Vagrantfile
/boxes/**/*.tar.gz
/boxes/quantal64/rootfs-amd64/
/boxes/output/

102
boxes/quantal64/download-ubuntu Executable file
View file

@ -0,0 +1,102 @@
#!/bin/bash
# This is the code extracted from /usr/share/lxc/templates/lxc-ubuntu
# that comes with Ubuntu 12.10 which is responsible for downloading the
# rootfs files / packages
set -e
suggest_flush()
{
echo "Container upgrade failed. The container cache may be out of date,"
echo "in which case flushing the case (see -F in the hep output) may help."
}
cleanup()
{
rm -rf $cache/partial-$arch
rm -rf $cache/rootfs-$arch
}
write_sourceslist()
{
# $1 => path to the rootfs
# $2 => architecture we want to add
MIRROR=${MIRROR:-http://archive.ubuntu.com/ubuntu}
SECURITY_MIRROR=${SECURITY_MIRROR:-http://security.ubuntu.com/ubuntu}
cat >> "$1/etc/apt/sources.list" << EOF
deb $MIRROR ${release} main restricted universe multiverse
deb $MIRROR ${release}-updates main restricted universe multiverse
deb $SECURITY_MIRROR ${release}-security main restricted universe multiverse
EOF
}
download_ubuntu()
{
packages=vim,ssh
echo "installing packages: $packages"
trap cleanup EXIT SIGHUP SIGINT SIGTERM
# check the mini ubuntu was not already downloaded
mkdir -p "$cache/partial-$arch"
if [ $? -ne 0 ]; then
echo "Failed to create '$cache/partial-$arch' directory"
return 1
fi
# download a mini ubuntu into a cache
echo "Downloading ubuntu $release minimal ..."
if [ -n "$(which qemu-debootstrap)" ]; then
qemu-debootstrap --verbose --components=main,universe --arch=$arch --include=$packages $release $cache/partial-$arch $MIRROR
else
debootstrap --verbose --components=main,universe --arch=$arch --include=$packages $release $cache/partial-$arch $MIRROR
fi
if [ $? -ne 0 ]; then
echo "Failed to download the rootfs, aborting."
return 1
fi
# Serge isn't sure whether we should avoid doing this when
# $release == `distro-info -d`
echo "Installing updates"
> $cache/partial-$arch/etc/apt/sources.list
write_sourceslist $cache/partial-$arch/ $arch
chroot "$1/partial-${arch}" apt-get update
if [ $? -ne 0 ]; then
echo "Failed to update the apt cache"
return 1
fi
cat > "$1/partial-${arch}"/usr/sbin/policy-rc.d << EOF
#!/bin/sh
exit 101
EOF
chmod +x "$1/partial-${arch}"/usr/sbin/policy-rc.d
lxc-unshare -s MOUNT -- chroot "$1/partial-${arch}" apt-get dist-upgrade -y || { suggest_flush; false; }
rm -f "$1/partial-${arch}"/usr/sbin/policy-rc.d
chroot "$1/partial-${arch}" apt-get clean
mv "$1/partial-$arch" "$1/rootfs-$arch"
trap EXIT
trap SIGINT
trap SIGTERM
trap SIGHUP
echo "Download complete"
return 0
}
declare cache=`readlink -f .` \
arch=amd64 \
release=quantal
if [ -d "${cache}/rootfs-${arch}" ]; then
echo 'The cache has been downloaded already, please remove it if you want to update'
exit 1
fi
download_ubuntu $cache $arch $release

View file

@ -104,6 +104,11 @@ finalize_user()
chroot $rootfs adduser ${user} $group >/dev/null 2>&1 || true
done
chroot $rootfs cp /etc/sudoers /etc/sudoers.orig >/dev/null 2>&1 || true
chroot $rootfs sed -i -e 's/%sudo\s\+ALL=(ALL:ALL)\s\+ALL/%sudo ALL=NOPASSWD:ALL/g' /etc/sudoers >/dev/null 2>&1 || true
chroot $rootfs locale-gen en_US en_US.UTF-8 hu_HU hu_HU.UTF-8 >/dev/null 2>&1 || true
chroot $rootfs dpkg-reconfigure locales >/dev/null 2>&1 || true
if [ -n "$auth_key" -a -f "$auth_key" ]; then
u_path="/home/${user}/.ssh"
root_u_path="$rootfs/$u_path"
@ -148,79 +153,6 @@ EOF
fi
}
cleanup()
{
rm -rf $cache/partial-$arch
rm -rf $cache/rootfs-$arch
}
suggest_flush()
{
echo "Container upgrade failed. The container cache may be out of date,"
echo "in which case flushing the case (see -F in the hep output) may help."
}
download_ubuntu()
{
cache=$1
arch=$2
release=$3
packages=vim,ssh
echo "installing packages: $packages"
trap cleanup EXIT SIGHUP SIGINT SIGTERM
# check the mini ubuntu was not already downloaded
mkdir -p "$cache/partial-$arch"
if [ $? -ne 0 ]; then
echo "Failed to create '$cache/partial-$arch' directory"
return 1
fi
# download a mini ubuntu into a cache
echo "Downloading ubuntu $release minimal ..."
if [ -n "$(which qemu-debootstrap)" ]; then
qemu-debootstrap --verbose --components=main,universe --arch=$arch --include=$packages $release $cache/partial-$arch $MIRROR
else
debootstrap --verbose --components=main,universe --arch=$arch --include=$packages $release $cache/partial-$arch $MIRROR
fi
if [ $? -ne 0 ]; then
echo "Failed to download the rootfs, aborting."
return 1
fi
# Serge isn't sure whether we should avoid doing this when
# $release == `distro-info -d`
echo "Installing updates"
> $cache/partial-$arch/etc/apt/sources.list
write_sourceslist $cache/partial-$arch/ $arch
chroot "$1/partial-${arch}" apt-get update
if [ $? -ne 0 ]; then
echo "Failed to update the apt cache"
return 1
fi
cat > "$1/partial-${arch}"/usr/sbin/policy-rc.d << EOF
#!/bin/sh
exit 101
EOF
chmod +x "$1/partial-${arch}"/usr/sbin/policy-rc.d
lxc-unshare -s MOUNT -- chroot "$1/partial-${arch}" apt-get dist-upgrade -y || { suggest_flush; false; }
rm -f "$1/partial-${arch}"/usr/sbin/policy-rc.d
chroot "$1/partial-${arch}" apt-get clean
mv "$1/partial-$arch" "$1/rootfs-$arch"
trap EXIT
trap SIGINT
trap SIGTERM
trap SIGHUP
echo "Download complete"
return 0
}
copy_ubuntu()
{
cache=$1
@ -238,8 +170,7 @@ install_ubuntu()
{
rootfs=$1
release=$2
flushcache=$3
cache="/var/cache/lxc/$release"
cache=$3 # "/var/cache/lxc/$release"
mkdir -p /var/lock/subsys/
(
@ -249,22 +180,6 @@ install_ubuntu()
return 1
fi
if [ $flushcache -eq 1 ]; then
echo "Flushing cache..."
rm -rf "$cache/partial-$arch"
rm -rf "$cache/rootfs-$arch"
fi
echo "Checking cache download in $cache/rootfs-$arch ... "
if [ ! -e "$cache/rootfs-$arch" ]; then
download_ubuntu $cache $arch $release
if [ $? -ne 0 ]; then
echo "Failed to download 'ubuntu $release base'"
return 1
fi
fi
echo "Copy $cache/rootfs-$arch to $rootfs ... "
copy_ubuntu $cache $arch $rootfs
if [ $? -ne 0 ]; then
@ -537,7 +452,7 @@ EOF
return 0
}
options=$(getopt -o a:b:hp:r:xn:FS:d -l arch:,help,path:,release:,trim,name:,flush-cache,auth-key:,debug -- "$@")
options=$(getopt -o a:b:hp:r:xn:FS:d:C -l arch:,help,path:,release:,trim,name:,flush-cache,auth-key:,debug:,cache: -- "$@")
if [ $? -ne 0 ]; then
usage $(basename $0)
exit 1
@ -573,14 +488,13 @@ fi
debug=0
trim_container=0
hostarch=$arch
flushcache=0
while true
do
case "$1" in
-h|--help) usage $0 && exit 0;;
-p|--path) path=$2; shift 2;;
-n|--name) name=$2; shift 2;;
-F|--flush-cache) flushcache=1; shift 1;;
-C|--cache) cache=$2; shift 2;;
-r|--release) release=$2; shift 2;;
-a|--arch) arch=$2; shift 2;;
-x|--trim) trim_container=1; shift 1;;
@ -605,12 +519,6 @@ if [ $hostarch = "i386" -a $arch = "amd64" ]; then
exit 1
fi
type debootstrap
if [ $? -ne 0 ]; then
echo "'debootstrap' command is missing"
exit 1
fi
if [ -z "$path" ]; then
echo "'path' parameter is required"
exit 1
@ -629,7 +537,7 @@ else
rootfs=$path/rootfs
fi
install_ubuntu $rootfs $release $flushcache
install_ubuntu $rootfs $release $cache
if [ $? -ne 0 ]; then
echo "failed to install ubuntu $release"
exit 1

View file

@ -3,7 +3,7 @@
"vagrant-lxc-version": "0.0.1",
"template-opts": {
"--arch": "x86_64",
"--arch": "amd64",
"--release": "quantal"
}
}

View file

@ -19,19 +19,16 @@ namespace :boxes do
sh 'cd boxes/ubuntu-cloud && tar -czf ../output/ubuntu-cloud.box ./*'
end
desc 'Build Ubuntu Quantal x64 Vagrant LXC box'
task 'quantal-64' do
unless File.exists?('/var/cache/lxc/quantal/rootfs-amd64')
puts "Right now you need to run `lxc-create` with the right arguments to build debootstrap's cache " +
"prior to building the box.\n" +
"Please contact me at the mail you'll find at https://github.com/fgrehm/vagrant-lxc/issues\n" +
"if you want to find out how to get this going."
exit 1
desc 'Build Ubuntu Quantal 64 bits Vagrant LXC box'
task 'quantal64' do
unless File.exists?('./boxes/quantal64/rootfs-amd64')
sh 'cd boxes/quantal64 && ./download-ubuntu'
end
sh 'mkdir -p boxes/output'
sh 'rm -f output/lxc-quantal-64.box'
sh 'cd boxes/quantal-64 && tar -czf ../output/lxc-quantal-64.box ./*'
sh 'sudo rm -f output/lxc-quantal64.box boxes/quantal64/rootfs.tar.gz'
sh 'cd boxes/quantal64 && sudo tar --numeric-owner -czf rootfs.tar.gz ./rootfs-amd64/*'
sh "cd boxes/quantal64 && sudo chown #{ENV['USER']}:#{ENV['USER']} rootfs.tar.gz && tar -czf ../output/lxc-quantal64.box ./* --exclude=rootfs-amd64 --exclude=download-ubuntu"
end
end
end