1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
#!/bin/sh
# Copyright (C) 2014-2021 Daniel Baumann <daniel.baumann@open-infrastructure.net>
# 2021 Simon Spöhel <simon.spoehel@open-infrastructure.net>
#
# SPDX-License-Identifier: GPL-3.0+
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
set -e
PROJECT="open-infrastructure"
SOFTWARE="compute-tools"
PROGRAM="container"
CONFIG="/etc/${SOFTWARE}/config"
HOOKS="/etc/${SOFTWARE}/hooks"
MACHINES="/var/lib/machines"
SCRIPT="${0}"
export SCRIPT
Parameters ()
{
GETOPT_LONGOPTIONS="name:,cnt.container-server:,id:,script:,shortname:,config-template:,verbose,"
GETOPT_OPTIONS="n:,i:,s:,c:,v,"
PARAMETERS="$(getopt --longoptions ${GETOPT_LONGOPTIONS} --name=${COMMAND} --options ${GETOPT_OPTIONS} --shell sh -- ${@})"
if [ "${?}" != "0" ]
then
echo "'${COMMAND}': getopt exit" >&2
exit 1
fi
eval set -- "${PARAMETERS}"
while true
do
case "${1}" in
-n|--name)
NAME="${2}"
shift 2
;;
--cnt.container-server)
CNT_CONTAINER_SERVER="${2}"
shift 2
;;
-i|--id)
ID="${2}"
shift 2
;;
-s|--script)
SCRIPT="${2}"
shift 2
;;
--shortname)
SHORTNAME="${2}"
shift 2
;;
-t|--config-template)
CONFIG_TEMPLATE="${2}"
shift 2
;;
-v|--verbose)
VERBOSE="true"
shift 1
;;
--)
shift 1
break
;;
*)
echo "'${COMMAND}': getopt error" >&2
exit 1
;;
esac
done
}
Usage ()
{
# FIXME
echo WIP
echo "Usage: container create -n|--name NAME -s|--script ${SCRIPT} -- [-p|--preseed-file FILE]" >&2
exit 1
}
Parameters "${@}"
if [ -z "${NAME}" ]
then
Usage
fi
if [ -e "${MACHINES}/${NAME}.vmdk" ]
then
echo "'${NAME}': container already exists" >&2
exit 1
fi
if [ "$(id -u)" -ne 0 ]
then
echo "'${NAME}': need root privileges" >&2
exit 1
fi
Commands ()
{
DIRECTORY="${1}"
# maximum of 15 characters, prefix is 'veth-'
HOSTNAME_SHORT="$(echo ${NAME} | cut -c-8)"
HOST_INTERFACE_NAME="$(echo ${NETWORK1_VETH:-veth-${HOSTNAME_SHORT}-0})"
sed -i -e "s|^cnt.auto=.*|cnt.auto=${CNT_AUTO}|g" \
-e "s|^cnt.container-server=.*|cnt.container-server=${CNT_CONTAINER_SERVER}|g" \
-e "s|^cnt.network-bridge=.*|cnt.network-bridge=${HOST_INTERFACE_NAME}:${NETWORK1_BRIDGE:-bridge0}|g" \
-e "s|^cnt.overlay=.*|cnt.overlay=${CNT_OVERLAY}|g" \
-e "s|^cnt.overlay-options=.*|cnt.overlay-options=${CNT_OVERLAY_OPTIONS}|g" \
-e "s|^bind=.*|bind=${BIND}|g" \
-e "s|^bind-ro=.*|bind-ro=${BIND_RO}|g" \
-e "s|^network-veth-extra=.*|network-veth-extra=${HOST_INTERFACE_NAME}:eno1|g" \
"${CONFIG}/${NAME}.conf"
if [ "${NETWORK_NUMBER}" -ge 2 ]
then
for NUMBER in $(seq 2 ${NETWORK_NUMBER})
do
eval IPV4_METHOD="$`echo NETWORK${NUMBER}_IPV4_METHOD`"
eval IPV6_METHOD="$`echo NETWORK${NUMBER}_IPV6_METHOD`"
if [ -z "${IPV4_METHOD}" ] && [ -z "${IPV6_METHOD}" ]
then
continue
fi
eval HOST_INTERFACE_NAME="$`echo NETWORK${NUMBER}_VETH`"
HOST_INTERFACE_NAME="$(echo ${HOST_INTERFACE_NAME:-veth-${HOSTNAME_SHORT}-${NUMBER}})"
CONTAINER_INTERFACE_NAME="eno${NUMBER}"
sed -i -e "/^register=.*/ a network-veth-extra=${HOST_INTERFACE_NAME}:${CONTAINER_INTERFACE_NAME}" "${CONFIG}/${NAME}.conf"
eval BRIDGE="$`echo NETWORK${NUMBER}_BRIDGE`"
sed -i -e "/^register=.*/ a cnt.network-bridge=${HOST_INTERFACE_NAME}:${BRIDGE:-bridge${NUMBER}}" "${CONFIG}/${NAME}.conf"
done
fi
}
## Generic parts
#copy base image
BASE_IMAGE="/srv/qemu/vEOS-lab.vmdk"
cp "${BASE_IMAGE}" "${MACHINES}/${NAME}.vmdk"
|