]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc-create.in
use pivot_root instead of chroot
[mirror_lxc.git] / src / lxc / lxc-create.in
1 #!/bin/bash
2
3 #
4 # lxc: linux Container library
5
6 # Authors:
7 # Daniel Lezcano <daniel.lezcano@free.fr>
8
9 # This library is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU Lesser General Public
11 # License as published by the Free Software Foundation; either
12 # version 2.1 of the License, or (at your option) any later version.
13
14 # This library is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # Lesser General Public License for more details.
18
19 # You should have received a copy of the GNU Lesser General Public
20 # License along with this library; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
23 #
24 # This script allows to set or remove the capabilities on the lxc tools.
25 # When the capabilities are set, a non root user can manage the containers.
26 #
27
28 usage() {
29 echo "usage: lxc-create -n <name> [-f configuration] [-t template] [-h]"
30 }
31
32 help() {
33 usage
34 echo
35 echo "creates a lxc system object."
36 echo
37 echo "Options:"
38 echo "name : name of the container"
39 echo "configuration: lxc configuration"
40 echo "template : lxc-template is an accessible template script"
41 }
42
43 shortoptions='hn:f:t:'
44 longoptions='help,name:,config:,template:'
45 lxc_path=@LXCPATH@
46
47 getopt=$(getopt -o $shortoptions --longoptions $longoptions -- "$@")
48 if [ $? != 0 ]; then
49 usage
50 exit 1;
51 fi
52
53 eval set -- "$getopt"
54
55 while true; do
56 case "$1" in
57 -h|--help)
58 help
59 exit 1
60 ;;
61 -n|--name)
62 shift
63 lxc_name=$1
64 shift
65 ;;
66 -f|--config)
67 shift
68 lxc_config=$1
69 shift
70 ;;
71 -t|--template)
72 shift
73 lxc_template=$1
74 shift
75 ;;
76 --)
77 shift
78 break;;
79 *)
80 echo $1
81 usage
82 exit 1
83 ;;
84 esac
85 done
86
87 if [ -z "$lxc_name" ]; then
88 echo "no container name specified"
89 usage
90 exit 1
91 fi
92
93 if [ "$(id -u)" != "0" ]; then
94 echo "This command has to be run as root"
95 exit 1
96 fi
97
98 if [ ! -r $lxc_path ]; then
99 echo "no configuration path defined !"
100 exit 1
101 fi
102
103 if [ -d "$lxc_path/$lxc_name" ]; then
104 echo "'$lxc_name' already exists"
105 exit 1
106 fi
107
108 trap "lxc-destroy -n $lxc_name; echo aborted; exit 1" SIGHUP SIGINT SIGTERM
109
110 mkdir -p $lxc_path/$lxc_name
111
112 if [ -z "$lxc_config" ]; then
113 touch $lxc_path/$lxc_name/config
114 else
115 if [ ! -r "$lxc_config" ]; then
116 echo "'$lxc_config' configuration file not found"
117 exit 1
118 fi
119
120 cp $lxc_config $lxc_path/$lxc_name/config
121 fi
122
123 if [ ! -z $lxc_template ]; then
124
125 type lxc-$lxc_template
126 if [ $? -ne 0 ]; then
127 echo "unknown template '$lxc_template'"
128 lxc-destroy -n $lxc_name
129 exit 1
130 fi
131
132 if [ -z "$lxc_config" ]; then
133 echo
134 echo "Warning:"
135 echo "-------"
136 echo "Usually the template option is called with a configuration"
137 echo "file option too, mostly to configure the network."
138 echo "eg. lxc-create -n foo -f lxc.conf -t debian"
139 echo "The configuration file is often:"
140 echo
141 echo "lxc.network.type=macvlan"
142 echo "lxc.network.link=eth0"
143 echo "lxc.network.flags=up"
144 echo
145 echo "or alternatively:"
146 echo
147 echo "lxc.network.type=veth"
148 echo "lxc.network.link=br0"
149 echo "lxc.network.flags=up"
150 echo
151 echo "For more information look at lxc.conf (5)"
152 echo
153 echo "At this point, I assume you know what you do."
154 echo "Press <enter> to continue ..."
155 read dummy
156 fi
157
158 lxc-$lxc_template --path=$lxc_path/$lxc_name --name=$lxc_name
159 if [ $? -ne 0 ]; then
160 echo "failed to execute template '$lxc_template'"
161 lxc-destroy -n $lxc_name
162 exit 1
163 fi
164
165 echo "'$lxc_template' template installed"
166 fi
167
168 echo "'$lxc_name' created"