3 # template script for generating ubuntu container for LXC based on daily cloud
6 # Copyright © 2012 Serge Hallyn <serge.hallyn@canonical.com>
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License version 2, as
10 # published by the Free Software Foundation.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 if [ -r /etc
/default
/lxc
]; then
35 if [ $arch = "i386" ]; then
39 # if there is exactly one veth network entry, make sure it has an
41 nics
=`grep -e '^lxc\.network\.type[ \t]*=[ \t]*veth' $path/config | wc -l`
42 if [ $nics -eq 1 ]; then
43 grep -q "^lxc.network.hwaddr" $path/config ||
cat <<EOF >> $path/config
44 lxc.network.hwaddr= 00:16:3e:$(openssl rand -hex 3| sed 's/\(..\)/\1:/g; s/.$//')
48 cat <<EOF >> $path/config
54 lxc.mount = $path/fstab
56 lxc.cap.drop = sys_module mac_admin
58 lxc.cgroup.devices.deny = a
59 # Allow any mknod (but not using the node)
60 lxc.cgroup.devices.allow = c *:* m
61 lxc.cgroup.devices.allow = b *:* m
63 lxc.cgroup.devices.allow = c 1:3 rwm
64 lxc.cgroup.devices.allow = c 1:5 rwm
66 lxc.cgroup.devices.allow = c 5:1 rwm
67 lxc.cgroup.devices.allow = c 5:0 rwm
68 #lxc.cgroup.devices.allow = c 4:0 rwm
69 #lxc.cgroup.devices.allow = c 4:1 rwm
71 lxc.cgroup.devices.allow = c 1:9 rwm
72 lxc.cgroup.devices.allow = c 1:8 rwm
73 lxc.cgroup.devices.allow = c 136:* rwm
74 lxc.cgroup.devices.allow = c 5:2 rwm
76 lxc.cgroup.devices.allow = c 254:0 rwm
78 lxc.cgroup.devices.allow = c 10:229 rwm
80 lxc.cgroup.devices.allow = c 10:200 rwm
82 lxc.cgroup.devices.allow = c 1:7 rwm
84 lxc.cgroup.devices.allow = c 10:228 rwm
86 lxc.cgroup.devices.allow = c 10:232 rwm
89 cat <<EOF > $path/fstab
90 proc $rootfs/proc proc nodev,noexec,nosuid 0 0
91 sysfs $rootfs/sys sysfs defaults 0 0
100 LXC Container configuration for Ubuntu Cloud images.
103 [ -r | --release <release> ]: Release name of container, defaults to host
104 [ -a | --arch ]: Arhcitecture of container, defaults to host arcitecture
105 [ -C | --cloud ]: Configure container for use with meta-data service, defaults to no
106 [ -T | --tarball ]: Location of tarball
108 Options, mutually exclusive of "-C" and "--cloud":
109 [ -i | --hostid ]: HostID for cloud-init, defaults to random string
110 [ -u | --userdata ]: Cloud-init user-data file to configure container on start
111 [ -S | --auth_key ]: SSH Public key file to inject into container
112 [ -L | --nolocales ]: Do not copy host's locales into container
118 options
=$
(getopt
-o a
:hp
:r
:n
:Fi
:CLS
:T
: -l arch
:,help,path
:,release
:,name
:,flush-cache
,hostid
:,auth-key
:,cloud
,no_locales
,tarball
: -- "$@")
119 if [ $?
-ne 0 ]; then
123 eval set -- "$options"
126 if [ -f /etc
/lsb-release
]; then
128 case "$DISTRIB_CODENAME" in
129 lucid|maverick|natty|oneiric|precise
)
130 release
=$DISTRIB_CODENAME
137 # Code taken from debootstrap
138 if [ -x /usr
/bin
/dpkg
] && /usr
/bin
/dpkg
--print-architecture >/dev
/null
2>&1; then
139 arch
=`/usr/bin/dpkg --print-architecture`
140 elif type udpkg
>/dev
/null
2>&1 && udpkg
--print-architecture >/dev
/null
2>&1; then
141 arch
=`/usr/bin/udpkg --print-architecture`
144 if [ "$arch" = "i686" ]; then
146 elif [ "$arch" = "x86_64" ]; then
148 elif [ "$arch" = "armv7l" ]; then
160 -h|
--help) usage
$0 && exit 0;;
161 -p|
--path) path
=$2; shift 2;;
162 -n|
--name) name
=$2; shift 2;;
163 -F|
--flush-cache) flushcache
=1; shift 1;;
164 -r|
--release) release
=$2; shift 2;;
165 -a|
--arch) arch
=$2; shift 2;;
166 -i|
--hostid) host_id
=$2; shift 2;;
167 -u|
--userdata) userdata
=$2; shift 2;;
168 -C|
--cloud) cloud
=1; shift 1;;
169 -S|
--auth_key) auth_key
=$2; shift 2;;
170 -L|
--no_locales) locales
=0; shift 2;;
171 -T|
--tarball) tarball
=$2; shift 2;;
172 --) shift 1; break ;;
177 if [ "$arch" == "i686" ]; then
181 if [ $hostarch = "i386" -a $arch = "amd64" ]; then
182 echo "can't create amd64 container on i386"
186 if [ $arch != "i386" -a $arch != "amd64" ]; then
187 echo "Only i386 and amd64 are supported by the ubuntu cloud template."
191 if [ -z "$path" ]; then
192 echo "'path' parameter is required"
196 if [ "$(id -u)" != "0" ]; then
197 echo "This script should be run as 'root'"
203 type ubuntu-cloudimg-query
206 # determine the url, tarball, and directory names
208 cache
="/var/cache/lxc/cloud-$release"
212 if [ -n "$tarball" ]; then
215 url1
=`ubuntu-cloudimg-query precise daily $arch --format "%{url}\n"`
216 url2
=`echo $url1 | sed -e 's/.tar.gz/-root\0/'`
219 filename
=`basename $url2`
221 mkdir
-p /var
/lock
/subsys
/
226 if [ $flushcache -eq 1 ]; then
227 echo "Clearing the cached images"
231 if [ ! -f $filename ]; then
235 echo "Extracting rootfs"
238 tar -zxf $cache/$filename
241 if [ $cloud -eq 0 ]; then
242 echo "Configuring for running outside of a cloud environment"
243 echo "If you want to configure for a cloud evironment, please use '-- -C' to create the container"
245 seed_d
=$rootfs/var
/lib
/cloud
/seed
/nocloud-net
246 rhostid
=$
(uuidgen | cut
-c -8)
247 host_id
=${hostid:-$rhostid}
250 cat > "$seed_d/meta-data" <<EOF
251 instance_id: lxc-$host_id
254 rm $rootfs/etc
/hostname
256 if [ $locales -eq 1 ]; then
257 cp /usr
/lib
/locale
/locale-archive
$rootfs/usr
/lib
/locale
/locale-archive
261 if [ -n "$auth_key" -a -f "$auth_key" ]; then
262 u_path
="/home/ubuntu/.ssh"
263 root_u_path
="$rootfs/$u_path"
264 mkdir
-p $root_u_path
265 cp $auth_key "$root_u_path/authorized_keys"
266 chroot
$rootfs chown
-R ubuntu
: "$u_path"
268 echo "Inserted SSH public key from $auth_key into /home/ubuntu/.ssh/authorized_keys"
271 if [ ! -f $userdata ]; then
272 cp $userdata $data_d/user-data
275 if [ -z "$MIRROR" ]; then
276 MIRROR
="http://archive.ubuntu.com/ubuntu"
279 cat > "$seed_d/user-data" <<EOF
281 output: {all: '| tee -a /var/log/cloud-init-output.log'}
283 manage_etc_hosts: localhost
284 locale: $(/usr/bin/locale | awk -F= '/LANG=/ {print$NF}')
289 chroot
$rootfs /usr
/sbin
/usermod
-U ubuntu
290 echo "Please login as user ubuntu with password ubuntu."
294 echo "Configured for running in a cloud environment."
295 echo "If you do not have a meta-data service, this container will likely be useless."
299 ) 200>/var
/lock
/subsys
/lxc-ubucloud
301 copy_configuration
$path $rootfs $name $arch
303 echo "Container $name created."