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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
|
#!/bin/bash
#
# vserver-new - Create a new virtual server
# Copyright (C) 2005 Benedikt Boehm <hollow@gentoo.org>
# Christian Heim <phreak@gentoo.org>
#
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
: ${APP:=${0##*/}}
: ${UTIL_VSERVER_VARS:=/usr/lib/util-vserver/util-vserver-vars}
if [ ! -e ${UTIL_VSERVER_VARS} ]; then
echo "Cannot find util-vserver installation"
echo "(the file '$UTIL_VSERVER_VARS' would be expected)"
exit 1
fi
source ${UTIL_VSERVER_VARS}
if [ ! -e ${_LIB_GENTOO_FUNCTIONS} ]; then
echo "${_LIB_GENTOO_FUNCTIONS} missing. Are you running Gentoo?"
exit 1
fi
source ${_LIB_GENTOO_FUNCTIONS}
trap "exit 1" INT
#------------------------------------------------------------------------------#
# Constants #
#------------------------------------------------------------------------------#
TARGETS="stage3 clone template"
VDIR=$(readlink /etc/vservers/.defaults/vdirbase)
#------------------------------------------------------------------------------#
# Generic target helpers #
#------------------------------------------------------------------------------#
generic_build() {
local interfaceopts=
for interface in ${dst_ifaces}; do
interfaceopts="${interfaceopts} --interface ${interface}"
done
if [ $dst_vdir_mount -eq 0 ]; then
ebegin "Automagically unmounting ${dst_vdir}"
umount ${dst_vdir}
eend $? || exit 2
rm -rf ${dst_vdir} || die "failed to remove ${dst_vdir}"
fi
ebegin "Building skeleton configuration"
vserver ${dst} build -m skeleton \
--context ${dst_xid} \
--hostname ${dst_hostname:-${dst}} \
${interfaceopts} \
--initstyle plain
eend $? || ${target}_clean "failed to build skeleton"
rm -rf ${dst_vdir}/*
if [ $dst_vdir_mount -eq 0 ]; then
ebegin "Automagically mounting ${dst_vdir}"
mount ${dst_vdir}
eend $? || exit 2
fi
pushd $dst_vdir &>/dev/null || die "cd ${dst_vdir} failed"
}
generic_clean() {
popd &>/dev/null
if [ -n "$1" ]; then
echo
eerror $1
einfo "Please remember to remove these directories if appropriate:"
einfo " /etc/vservers/${dst}"
einfo " /vservers/${dst}"
exit 1
else
local clean="/var/tmp /tmp /var/lib/init.d"
for dir in ${clean}; do
ebegin "Cleaning ${dir}"
rm -rf ${dst_vdir}${dir}/*
eend $?
done
fi
echo
ewarn
ewarn "Please remember to change required settings inside your vserver"
ewarn "(hostname, hosts, ip, etc)"
ewarn
}
generic_boot() {
if [ ${dst_start} -eq 1 ]; then
ebegin "Starting new vserver ${dst}"
vserver ${dst} start
eend $? || exit 2
fi
}
#------------------------------------------------------------------------------#
# Target: stage3 #
#------------------------------------------------------------------------------#
stage3_usage() {
echo "Usage: vserver-new <name> <globalopts> stage3 <tar> <arch>"
echo
echo " <tar> Location of the stage3 tarball"
echo " <arch> Stage architecture"
echo
}
stage3_init() {
if [ "$1" == "--help" ]; then
stage3_usage
exit 0
fi
[ -z "$1" ] && die "Missing required argument <tar>"
# determine PORTDIR/DISTDIR/PKGDIR here, so the PROFILE check gets it
portdir=$(portageq portdir)
distdir=$(portageq distdir)
pkgdir=$(portageq pkgdir)
[ -z "${portdir}" ] && die "host has no portdir!"
[ -z "${distdir}" ] && die "host has no distdir!"
[ -z "${pkgdir}" ] && die "host has no pkgdir!"
tarball=$1
[ "${tarball#/}" == "${tarball}" ] && tarball="$(pwd)/${tarball}"
[ ! -r ${tarball} ] && die "${tarball} is not readable"
# check arch and profile
[ -z "$2" ] && die "Missing required argument <arch>"
profile="default-linux/$2/vserver"
[ ! -d "${portdir}/profiles/${profile}" ] && die "no profile exists for arch $2"
stage3_build
}
stage3_build() {
generic_build
ebegin "Unpacking ${tarball}"
unpack ${tarball}
eend $? || stage3_clean "failed to unpack stage3 tarball"
mkdir -p usr/portage/{distfiles,packages} || stage3_clean "failed to create portdir"
ebegin "Using shared portage tree ${portdir}"
mount -o bind ${portdir} usr/portage || stage3_clean "failed to mount portdir"
if [ "${distdir}" != "${portdir}/distfiles" ]; then
mount -o bind ${distdir} usr/portage/distfiles || stage3_clean "failed to mount distdir"
fi
if [ "${pkgdir}" != "${portdir}/packages" ]; then
mount -o bind ${pkgdir} usr/portage/packages || stage3_clean "failed to mount pkgdir"
fi
eend 0
cp -L /etc/resolv.conf etc/resolv.conf || stage3_clean "failed to copy DNS information"
mount -t proc none proc/ || stage3_clean "failed to mount procfs"
mount --bind /dev dev/ || stage3_clean "failed to mount dev"
cat <<EOF > tmp/vserver-new-stage3.sh
#!/bin/bash
if [ -r /sbin/functions.sh ]; then
source /sbin/functions.sh
else
echo "/sbin/functions.sh missing. Are you running Gentoo?"
fi
die() {
eerror \${1:-<no error message>}
einfo "Type \${APP} --help for details"
exit \${2:-1}
}
env-update || die "failed to update environment"
source /etc/profile || die "failed to source /etc/profile"
pushd /etc &>/dev/null || die "failed to cd /etc"
rm make.profile
ebegin "Switching to vserver portage profile"
ln -s ../usr/portage/profiles/${profile} make.profile
eend \$? || exit 1
echo
emerge -k syslog-ng
echo
pushd syslog-ng &>/dev/null || die "failed to cd /etc/syslog-ng"
sed -i -e 's:pipe("/proc/kmsg"); ::' syslog-ng.conf
sed -i -r 's:(.*console_all.*):#\1:g' syslog-ng.conf
rc-update add syslog-ng default
popd &>/dev/null
popd &>/dev/null
EOF
chmod +x tmp/vserver-new-stage3.sh
echo
einfo "We're chrooting into the unpacked stage3 now!"
ewarn "It is not advisable to hit Ctrl+C during this step!"
ewarn "You have been warned! Continuing in 10 seconds ..."
echo
sleep 10
chroot . /tmp/vserver-new-stage3.sh || stage3_clean "chroot script failed, see above"
stage3_clean
}
stage3_clean() {
rm tmp/vserver-new-stage3.sh || ewarn "failed to remove chroot script"
if [ "${distdir}" != "${portdir}/distfiles" ]; then
umount usr/portage/distfiles || ewarn "failed to umount distdir"
fi
if [ "${pkgdir}" != "${portdir}/packages" ]; then
umount usr/portage/packages || ewarn "failed to umount pkgdir"
fi
umount usr/portage || ewarn "failed to umount portdir"
umount proc/ || ewarn "failed to umount proc/"
umount dev/ || ewarn "failed to umount dev/"
generic_clean $1
stage3_boot
}
stage3_boot() {
generic_boot
}
#------------------------------------------------------------------------------#
# Target: clone #
#------------------------------------------------------------------------------#
clone_usage() {
echo "Usage: vserver-new <name> <globalopts> clone <src>"
echo
echo " <src> Name of the vserver to copy from"
echo
}
clone_init() {
if [ "$1" == "--help" ]; then
clone_usage
exit 0
fi
[ -z "$1" ] && die "Missing required argument <src>"
src=$1
src_vdir="${VDIR}/${src}"
[ ! -d $src_vdir ] && die "<src> does not exist"
stopped=0
if vs_is_running_name ${src} true; then
ebegin "Stopping source vserver"
vserver ${src} stop
eend $? || die "failed to stop vserver ${src}"
stopped=1
fi
clone_build
}
clone_build() {
generic_build
ebegin "Running rsync -a $src_vdir/ $dst_vdir"
rsync -a $src_vdir/ $dst_vdir
eend $? || clone_clean "failed to copy ${src} to ${dst}"
clone_clean
}
clone_clean() {
generic_clean $1
clone_boot
}
clone_boot() {
if [ ${stopped} -eq 1 ]; then
ebegin "Starting source vserver"
vserver ${src} start
eend $? || ewarn "failed to start vserver ${src}"
fi
generic_boot
}
#------------------------------------------------------------------------------#
# Target: template #
#------------------------------------------------------------------------------#
template_usage() {
echo "Usage: vserver-new <name> <globalopts> template <tar>"
echo
echo " <tar> Path to the template tarball"
echo
echo "To build a template, go to your exisiting vserver"
echo "installation and tar the whole rootfs:"
echo
echo "$ cd /vservers/foo"
echo "$ tar cvpf ../foo-template.tar ./"
}
template_init() {
if [ "$1" == "--help" ]; then
template_usage
exit 0
fi
[ -z "$1" ] && die "Missing required argument <tar>"
tarball=$1
[ "${tarball#/}" == "${tarball}" ] && tarball="$(pwd)/${tarball}"
[ ! -r ${tarball} ] && die "${tarball} is not readable"
template_build
}
template_build() {
generic_build
ebegin "Unpacking $(basename ${tarball}) to ${dst_vdir}"
unpack ${tarball}
eend $? || template_clean "failed to unpack template"
template_clean
}
template_clean() {
generic_clean $1
template_boot
}
template_boot() {
generic_boot
}
#------------------------------------------------------------------------------#
# Main app #
#------------------------------------------------------------------------------#
usage() {
echo "Usage: vserver-new <name> <globalopts> <target> [localopts]"
echo
echo " <name> Name of the new vserver"
echo " <globalopts> See Global options below"
echo " <target> See Supported targets below"
echo " [localopts] Target specific options"
echo
echo "Global options:"
echo " --context <id> Static context ID of <name> (required)"
echo " --hostname <name> Hostname of <name>"
echo " --interface [<name-suffix>=][<device>:]<ip>[/<mask|prefixlen>]"
echo " Declare a new network-interface for <name> (this"
echo " option can be specified multiple times)"
echo " --start Start the new vserver"
echo " --destroy Destroy exisiting vserver installation"
echo " (Use this option if you have seperate"
echo " fs mounts for each vserver)"
echo
echo "Supported targets:"
echo " stage3 Build a new vserver using a stage3 tarball"
echo " clone Clone an already exisiting vserver"
echo " template Create a new vserver using a template tarball"
echo
echo "Type vserver-new <target> --help for local options"
}
# 1. Check for "vserver-new <target> --help" construct
if [ "$2" == "--help" ]; then
for i in $TARGETS; do
if [ "$i" == "$1" ]; then
${i}_usage
exit 0
fi
done
die "Selected target not supported"
fi
# 2. Check for "vserver-new --help" construct
if [ "$1" == "--help" ]; then
usage
exit 0
fi
# 3. Checking <name>
[ -z "$1" ] && die "Missing argument <name>"
# 4. Parsing opts
dst=$1 && shift
opts=$(POSIXLY_CORRECT=1 getopt -o h --longoptions help,context:,hostname:,interface:,start,destroy -n $0 -- "$@")
[ "$?" != "0" ] && die "Wrong number of options"
eval set -- "$opts"
dst_vdir="${VDIR}/${dst}"
dst_xid=
dst_hostname=
dst_ifaces=
dst_start=0
destroy=0
dst_vdir_mount=1
while true; do
case "$1" in
-h|--help)
usage
exit 0
;;
--context)
dst_xid=$2
shift 2
;;
--hostname)
dst_hostname=$2
shift 2
;;
--interface)
dst_ifaces="${dst_ifaces} $2"
shift 2
;;
--start)
dst_start=1
shift
;;
--destroy)
destroy=1
shift
;;
--)
shift
break
;;
*)
die "Unknown argument '${1}'"
;;
esac
done
# 5. Checking environment
if [ -d $dst_vdir ]; then
grep ${dst_vdir} /etc/fstab &>/dev/null
dst_vdir_mount=$?
if [ ${destroy} -eq 1 ]; then
ewarn "Existing vserver installation will be destroyed!"
ewarn "Proceeding in 10 seconds ..."
sleep 10
einfo "Removing existing installation ..."
if [ ${dst_vdir_mount} -eq 0 ]; then
ebegin " ${dst_vdir}/*"
rm -rf ${dst_vdir}/*
eend $? || exit 1
else
ebegin " ${dst_vdir}"
rm -rf ${dst_vdir}
eend $? || exit 1
fi
ebegin " /etc/vservers/${dst}"
rm -rf /etc/vservers/${dst}
eend $?
else
eerror "Existing vserver installation found. Try --destroy"
exit 1
fi
fi
[ -d /etc/vservers/${dst} ] && die "vserver config dir already exists"
# 6. Checking required option --context
[ -z "$dst_xid" ] && die "Missing required global option --context"
# 7. Checking <target>
[ -z "$1" ] && die "Missing argument <target>"
target=
for i in $TARGETS; do
[ "$i" == "$1" ] && target=$i
done
shift
[ -z "$target" ] && die "Selected target not supported"
# 8. Start target
trap "${target}_clean 'Caught interrupt'" INT
${target}_init "$@"
|