]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/lxc-create.in
lxc-create to run even if not in PATH
[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 bindir=@BINDIR@
47
48 getopt=$(getopt -o $shortoptions --longoptions $longoptions -- "$@")
49 if [ $? != 0 ]; then
50 usage
51 exit 1;
52 fi
53
54 eval set -- "$getopt"
55
56 while true; do
57 case "$1" in
58 -h|--help)
59 help
60 exit 1
61 ;;
62 -n|--name)
63 shift
64 lxc_name=$1
65 shift
66 ;;
67 -f|--config)
68 shift
69 lxc_config=$1
70 shift
71 ;;
72 -t|--template)
73 shift
74 lxc_template=$1
75 shift
76 ;;
77 --)
78 shift
79 break;;
80 *)
81 echo $1
82 usage
83 exit 1
84 ;;
85 esac
86 done
87
88 if [ -z "$lxc_path" ]; then
89 echo "no configuration path defined !"
90 exit 1
91 fi
92
93 if [ ! -r $lxc_path ]; then
94 echo "configuration path '$lxc_path' not found"
95 exit 1
96 fi
97
98 if [ -z "$lxc_name" ]; then
99 echo "no container name specified"
100 usage
101 exit 1
102 fi
103
104 if [ "$(id -u)" != "0" ]; then
105 echo "This command has to be run as root"
106 exit 1
107 fi
108
109 if [ ! -r $lxc_path ]; then
110 echo "no configuration path defined !"
111 exit 1
112 fi
113
114 if [ -d "$lxc_path/$lxc_name" ]; then
115 echo "'$lxc_name' already exists"
116 exit 1
117 fi
118
119 trap "${bindir}/lxc-destroy -n $lxc_name; echo aborted; exit 1" SIGHUP SIGINT SIGTERM
120
121 mkdir -p $lxc_path/$lxc_name
122
123 if [ -z "$lxc_config" ]; then
124 touch $lxc_path/$lxc_name/config
125 else
126 if [ ! -r "$lxc_config" ]; then
127 echo "'$lxc_config' configuration file not found"
128 exit 1
129 fi
130
131 cp $lxc_config $lxc_path/$lxc_name/config
132 fi
133
134 if [ ! -z $lxc_template ]; then
135
136 type ${bindir}/lxc-$lxc_template >/dev/null
137 if [ $? -ne 0 ]; then
138 echo "unknown template '$lxc_template'"
139 ${bindir}/lxc-destroy -n $lxc_name
140 exit 1
141 fi
142
143 if [ -z "$lxc_config" ]; then
144 echo
145 echo "Warning:"
146 echo "-------"
147 echo "Usually the template option is called with a configuration"
148 echo "file option too, mostly to configure the network."
149 echo "eg. lxc-create -n foo -f lxc.conf -t debian"
150 echo "The configuration file is often:"
151 echo
152 echo "lxc.network.type=macvlan"
153 echo "lxc.network.link=eth0"
154 echo "lxc.network.flags=up"
155 echo
156 echo "or alternatively:"
157 echo
158 echo "lxc.network.type=veth"
159 echo "lxc.network.link=br0"
160 echo "lxc.network.flags=up"
161 echo
162 echo "For more information look at lxc.conf (5)"
163 echo
164 echo "At this point, I assume you know what you do."
165 echo "Press <enter> to continue ..."
166 read dummy
167 fi
168
169 ${bindir}/lxc-$lxc_template --path=$lxc_path/$lxc_name --name=$lxc_name
170 if [ $? -ne 0 ]; then
171 echo "failed to execute template '$lxc_template'"
172 ${bindir}/lxc-destroy -n $lxc_name
173 exit 1
174 fi
175
176 echo "'$lxc_template' template installed"
177 fi
178
179 echo "'$lxc_name' created"