]> git.proxmox.com Git - mirror_lxc.git/blame - scripts/lxc-debian.in
Change access mount and check on the console
[mirror_lxc.git] / scripts / lxc-debian.in
CommitLineData
bad69158 1#!/bin/bash
2# set -ex
3
4NAME="debian"
5CONFFILE="lxc.conf"
6MNTFILE="mount.conf"
7UTSNAME=
8IPV4="172.20.0.21"
9GATEWAY="172.20.0.1"
10INTERFACES="/etc/network/interfaces"
11INITTAB="/etc/inittab"
12HOSTNAME="/etc/hostname"
13FSTAB="/etc/fstab"
7ad641d5 14CACHE="@LOCALSTATEDIR@/cache/lxc/debian"
bad69158 15
16create() {
17
18 # choose a container name, default is 'debian'
19 echo -n "What is the name for the container ? [$NAME] "
20 read _NAME_
21
22 if [ ! -z "$_NAME_" ]; then
23 NAME=$_NAME_
24 fi
25
26 # choose a hostname, default is the container name
27 echo -n "What hostname do you wish for this container ? [$NAME] "
28 read _UTSNAME_
29
30 if [ ! -z "$_UTSNAME_" ]; then
31 UTSNAME=$_UTSNAME_
32 else
33 UTSNAME=$NAME
34 fi
35
36 # choose an ipv4 address, better to choose the same network than
37 # your host
38 echo -n "What IP address do you wish for this container ? [$IPV4] "
39 read _IPV4_
40
41 if [ ! -z "$_IPV4_" ]; then
42 IPV4=$_IPV4_
43 fi
44
45 # choose the gateway ip address
46 echo -n "What is the gateway IP address ? [$GATEWAY] "
47 read _GATEWAY_
48
49 if [ ! -z "$_GATEWAY_" ]; then
50 GATEWAY=$_GATEWAY_
51 fi
52
53 # the rootfs name will be build with the container name
54 ROOTFS="./rootfs.$NAME"
55
56 # check if the rootfs does already exist
57 if [ ! -e "$ROOTFS" ]; then
58 (
59 flock -n -x 200
60
61 RES=$?
62 if [ "$RES" != "0" ]; then
63 echo "Cache repository is busy."
64 break
65 fi
66
67 echo "Choose your architecture"
68 select ARCH in amd64 i386; do
69 echo "Architecture $ARCH selected"
70 break;
71 done
72
73 # check the mini debian was not already downloaded
74 echo -n "Checking cache download ..."
75 if [ ! -e "$CACHE/rootfs-$ARCH" ]; then
76
77 echo "not cached"
78
79 mkdir -p "$CACHE/rootfs-$ARCH"
80
81 # download a mini debian into a cache
82 echo "Downloading debian minimal ..."
83 debootstrap --verbose --variant=minbase --arch=$ARCH \
84 --include apache,netbase,net-tools,iproute,openssh-server \
85 etch $CACHE/rootfs-$ARCH http://ftp.debian.org/debian
86
87 RESULT=$?
88 if [ "$RESULT" != "0" ]; then
89 echo "Failed to download the rootfs, aborting."
90 exit 1
91 fi
92 echo "Download complete."
93 else
94 echo "Found."
95 fi
96
97 # make a local copy of the minidebian
98 echo -n "Copying rootfs ..."
99 cp -a $CACHE/rootfs-$ARCH $ROOTFS && echo "Done." || exit
100 ) 200>/var/lock/subsys/lxc
101 fi
102
103
104########################################
105# lxc configuration files
106########################################
107
108# lxc mount point
109
110cat <<EOF > $MNTFILE
111/dev $(pwd)/$ROOTFS/dev none bind 0 0
112/dev/pts $(pwd)/$ROOTFS/dev/pts none bind 0 0
113/etc/resolv.conf $(pwd)/$ROOTFS/etc/resolv.conf none ro,bind 0 0
114EOF
115
116# lxc configuration
117
118cat <<EOF > $CONFFILE
85cbaa06 119
bad69158 120lxc.utsname = $UTSNAME
85cbaa06 121
bad69158 122lxc.network.type = veth
123lxc.network.flags = up
124lxc.network.link = br0
125lxc.network.name = eth0
85cbaa06 126
bad69158 127lxc.mount = $MNTFILE
85cbaa06 128
bad69158 129lxc.rootfs = $ROOTFS
85cbaa06 130
131lxc.cgroup.devices.deny = a
132
133# /dev/null and zero
134lxc.cgroup.devices.allow = c 1:3 rwm
135lxc.cgroup.devices.allow = c 1:5 rwm
136
137# consoles
138lxc.cgroup.devices.allow = c 5:1 rwm
139lxc.cgroup.devices.allow = c 5:0 rwm
140lxc.cgroup.devices.allow = c 4:0 rwm
141lxc.cgroup.devices.allow = c 4:1 rwm
142
143# /dev/{,u}random
144lxc.cgroup.devices.allow = c 1:9 rwm
145lxc.cgroup.devices.allow = c 1:8 rwm
146
147# /dev/pts/* - pts namespaces are "coming soon"
148lxc.cgroup.devices.allow = c 136:* rwm
719c46e1 149lxc.cgroup.devices.allow = c 5:2 rwm
85cbaa06 150
151# rtc
152lxc.cgroup.devices.allow = c 254:0 rwm
153
bad69158 154EOF
155
156
157########################################
158# rootfs configuration files tweak
159########################################
160
161# inittab
162
163cat <<EOF > $ROOTFS/$INITTAB
164id:3:initdefault:
165si::sysinit:/etc/init.d/rcS
166l0:0:wait:/etc/init.d/rc 0
167l1:1:wait:/etc/init.d/rc 1
168l2:2:wait:/etc/init.d/rc 2
169l3:3:wait:/etc/init.d/rc 3
170l4:4:wait:/etc/init.d/rc 4
171l5:5:wait:/etc/init.d/rc 5
172l6:6:wait:/etc/init.d/rc 6
173# Normally not reached, but fallthrough in case of emergency.
174z6:6:respawn:/sbin/sulogin
1751:2345:respawn:/sbin/getty 38400 console
176EOF
177
178# hostname
179cat <<EOF > $ROOTFS/$HOSTNAME
180$UTSNAME
181EOF
182
183# fstab
184
185cat <<EOF > $ROOTFS/$FSTAB
186tmpfs /dev/shm tmpfs defaults 0 0
187EOF
188
189# network
190
191cat <<EOF > $ROOTFS/$INTERFACES
192auto eth0 lo
193iface eth0 inet static
194address $IPV4
195netmask 255.255.255.0
196broadcast 0.0.0.0
197up route add default gw $GATEWAY
198iface lo inet loopback
199EOF
200
201# create the container object
202
85cbaa06 203@BINDIR@/lxc-create -n $NAME -f $CONFFILE
204RES=$?
bad69158 205
206# remove the configuration files
bad69158 207rm -f $CONFFILE
208rm -f $MNTFILE
209
85cbaa06 210if [ "$RES" != "0" ]; then
211 echo "Failed to create '$NAME'"
212 exit 1
213fi
214
bad69158 215echo "Done."
216echo -e "\nYou can run your container with the 'lxc-start -n $NAME'\n"
217}
218
219destroy() {
220
221 echo -n "What is the name for the container ? [$NAME] "
222 read _NAME_
223
224 if [ ! -z "$_NAME_" ]; then
225 NAME=$_NAME_
226 fi
227
7ad641d5 228 @BINDIR@/lxc-destroy -n $NAME
bad69158 229 RETVAL=$?
230 if [ ! $RETVAL -eq 0 ]; then
231 echo "Failed to destroyed '$NAME'"
232 return $RETVAL;
233 fi
234
235 ROOTFS="./rootfs.$NAME"
236
237 echo -n "Shall I remove the rootfs [y/n] ? "
238 read
239 if [ "$REPLY" = "y" ]; then
240 rm -rf $ROOTFS
241 fi
242
243 return 0
244}
245
246help() {
247 cat <<EOF
248
249This script is a helper to create debian system containers.
250
251The script will create the container configuration file following
252the informations submitted interactively with 'lxc-debian create'
253
254The first creation will download, with debootstrap, a debian
255minimal and store it into a cache.
256
257The script will copy from the cache the root filesystem to the
258current directory.
259
260If there is a problem with the container, (bad configuration for
261example), you can destroy the container with 'lxc-debian destroy'
262but without removing the rootfs and recreate it again with
263'lxc-debian create'.
264
265If you want to create another debian container, call the 'lxc-debian
266 create' again, specifying another name and new parameters.
267
268At any time you can purge the debian cache download by calling
269'lxc-debian purge'
270
271Have fun :)
272
273EOF
274}
275
276purge() {
277
278 if [ ! -e $CACHE ]; then
279 exit 0
280 fi
281
282 # lock, so we won't purge while someone is creating a repository
283 (
284 flock -n -x 200
285
286 RES=$?
287 if [ "$RES" != "0" ]; then
288 echo "Cache repository is busy."
289 exit 1
290 fi
291
292 echo -n "Purging the download cache..."
7ad641d5 293 rm --preserve-root --one-file-system -rf $CACHE && echo "Done." || exit 1
bad69158 294 exit 0
295
296 ) 200>/var/lock/subsys/lxc
297}
298
299if [ "$(id -u)" != "0" ]; then
300 echo "This script should be run as 'root'"
301 exit 1
302fi
303
304case "$1" in
305 create)
306 create;;
307 destroy)
308 destroy;;
309 help)
310 help;;
311 purge)
312 purge;;
313 *)
314 echo "Usage: $0 {create|destroy|purge|help}"
315 exit 1;;
316esac