-
Notifications
You must be signed in to change notification settings - Fork 0
/
lxct-create
executable file
·663 lines (615 loc) · 15.7 KB
/
lxct-create
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
#!/bin/bash
# ================================================================================
# LXC-tools: Appliance and LXC container builder for Debian GNU/Linux (and derivatives)
#
# Copyright © 2010 Jesús Lara Giménez (phenobarbital) <jesuslarag@gmail.com>
# Version: 0.5
#
# Developed by Jesus Lara (phenobarbital) <jesuslara@phenobarbital.info>
# https://github.com/phenobarbital/lxc-tools
#
# This script is based on the lxc-debian example script that ships with lxc.
# Copyright (C) 2010 Nigel McNie
#
# License: GNU GPL version 3 <http://gnu.org/licenses/gpl.html>.
# This is free software: you are free to change and redistribute it.
# There is NO WARRANTY, to the extent permitted by law.
# ================================================================================
###
# lxct_create
# create a lxc container
##
# get configuration
if [ -e /etc/lxc-tools/lxc-tools.conf ]; then
. /etc/lxc-tools/lxc-tools.conf
else
. ./etc/lxc-tools.conf
fi
#
# all common functions (for all templates)
#
if [ -e /usr/lib/lxc-tools/lxct_common.sh ]; then
. /usr/lib/lxc-tools/lxct_common.sh
else
. ./lib/lxct_common.sh
fi
### functions
# get template
get_template()
{
templatedir
if [ -z "$TEMPLATE" ]; then
# creating a container from host-based
DIST=`get_distribution`
case "$DIST" in
"debian"|"Debian"|"DEBIAN")
debug "- using lxc-debian as template"
TEMPLATE="$TEMPLATEDIR/lxc-debian"
;;
"ubuntu"|"Ubuntu"|"UBUNTU")
debug "- using lxc-ubuntu as template"
TEMPLATE="$TEMPLATEDIR/lxc-ubuntu"
;;
"centos"|"Centos"|"CENTOS"|"CentOS")
debug "- using lxc-centos as template"
TEMPLATE="$TEMPLATEDIR/lxc-centos"
;;
*)
error "Unknown distribution: $DIST"
exit 1
;;
esac
elif [ -f "$TEMPLATEDIR/lxc-$TEMPLATE" ]; then
debug "- using lxc-$TEMPLATE as template"
TEMPLATE="$TEMPLATEDIR/lxc-$TEMPLATE"
else
error "$(basename $0) template $TEMPLATE not exists, aborted"
exit 1
fi
}
# run all roles defined in --role option
execute_roles()
{
roledir
if [ ! -z "$ROLENAME" ]; then
IFS=',' read -a roles <<< "$ROLENAME"
for role in "${roles[@]}"
do
role_file="$ROLEDIR/$role"
# existe este rol?
if [ -f "$role_file" ]; then
#verifico esta permisologia
if [ ! -x "$role_file" ]; then
error "error: role '$role' is not executable, try chmod o+x $role_file"
return 1
else
info " = Executing role $role = "
. $role_file "$ROOTFS"
fi
else
error "error: role '$role' not exists in $ROLEDIR"
return 1
fi
done
else
return 0
fi
}
function create_lxc_config() {
if [ -z "$SECURE" ]; then
CAPABILITIES='sys_admin sys_module sys_time sys_nice mac_admin mac_override'
else
CAPABILITIES='sys_admin audit_control audit_write fsetid ipc_lock ipc_owner lease linux_immutable mac_admin mac_override mknod setfcap setpcap sys_module sys_nice sys_pacct sys_ptrace sys_rawio sys_tty_config sys_time'
fi
cat <<EOF > $CONFIGFILE
# $CONFIGFILE
## Container
lxc.utsname = $NAME
lxc.arch = $ARCH
# lxc.console = /var/log/lxc/$NAME.console
lxc.tty = 2
lxc.pts = 1024
## network
lxc.network.type = $NET_TYPE
$NET_OPTIONS
## Capabilities
lxc.cap.drop = $CAPABILITIES
## Devices
# Allow all devices
#lxc.cgroup.devices.allow = a
# Deny all devices
lxc.cgroup.devices.deny = a
# Allow to mknod all devices (but not using them)
lxc.cgroup.devices.allow = c *:* m
lxc.cgroup.devices.allow = b *:* m
# /dev/console
lxc.cgroup.devices.allow = c 5:1 rwm
# /dev/fuse
lxc.cgroup.devices.allow = c 10:229 rwm
# /dev/null
lxc.cgroup.devices.allow = c 1:3 rwm
# /dev/ptmx
lxc.cgroup.devices.allow = c 5:2 rwm
# /dev/pts/*
lxc.cgroup.devices.allow = c 136:* rwm
# /dev/random
lxc.cgroup.devices.allow = c 1:8 rwm
# /dev/rtc
lxc.cgroup.devices.allow = c 254:0 rwm
# /dev/tty
lxc.cgroup.devices.allow = c 5:0 rwm
# /dev/urandom
lxc.cgroup.devices.allow = c 1:9 rwm
# /dev/zero
lxc.cgroup.devices.allow = c 1:5 rwm
## Limits
lxc.cgroup.cpu.shares = 1024
lxc.cgroup.cpuset.cpus = 1
lxc.cgroup.memory.limit_in_bytes = $MEMORY
## Filesystem
EOF
if [ "$BACKEND_METHOD" == 'lvm' ]; then
cat <<EOF >> $CONFIGFILE
lxc.rootfs = $LVROOTFS
EOF
else
cat <<EOF >> $CONFIGFILE
lxc.rootfs = $ROOTFS
lxc.mount.entry = proc $ROOTFS/proc proc nodev,noexec,nosuid 0 0
lxc.mount.entry = sysfs $ROOTFS/sys sysfs defaults,ro 0 0
lxc.mount.entry = devpts $ROOTFS/dev/pts devpts gid=5,mode=620,newinstance,ptmxmode=0666 0 0
EOF
fi
if [ "$DIST" == 'gentoo' ]; then
cat <<EOF >> $CONFIGFILE
lxc.mount.entry = portagetmp $ROOTFS/var/portage/tmp tmpfs rw,nosuid,nodev 0 0
lxc.mount.entry = run $ROOTFS/run tmpfs rw,nosuid,nodev,relatime,mode=755 0 0
EOF
fi
if [ "$DIST" == 'fedora' ]; then
cat <<EOF >> $CONFIGFILE
lxc.mount.entry = devtmpfs $ROOTFS/dev devtmpfs defaults 0 0
EOF
fi
if [ $? -ne 0 ]; then
error "Failed to add configuration"
return 1
fi
return 0
}
# get volume and create lvm volume
function create_lvm_volume() {
if [ "$BACKEND_METHOD" = 'lvm' ]; then
cat <<EOF
Volume Group: $LVM
LVM Name : $NAME
Size : $SIZE
EOF
LVCREATE=`which lvcreate`
if [ ! -f "$LVCREATE" ]; then
error "'lvcreate' command is missing, run 'apt-get install lvm2'"
exit 1
fi
LVROOTFS="/dev/$LVM/$NAME"
lvdisplay $LVROOTFS > /dev/null 2>&1
if [ $? -eq 0 ]; then
error "error: $LVROOTFS already exists in vggroup $LVM" >&2
exit 1
fi
$LVCREATE -n $NAME --size $SIZE $LVM 2>&1
if [ $? -ne 0 ]; then
error "error creating $LVROOTFS in vggroup $LVM, aborted" >&2
exit 1
fi
# volume format
/sbin/mkfs.$FS -L $NAME $LVROOTFS
if [ $? -ne 0 ]; then
error "error formatting $LVROOTFS with $FS filesystem, aborted" >&2
exit 1
fi
debug " - mounting $LVROOTFS in $ROOTFS"
mount $LVROOTFS $ROOTFS
fi
return 0
}
umount_lvm()
{
if [ "$BACKEND_METHOD" = 'lvm' ]; then
#umount
debug " - unmounting $LVROOTFS"
umount -f $LVROOTFS
fi
}
create_btrfs_volume() {
#TODO
if [ "$BACKEND_METHOD" = 'btrfs' ]; then
# btrfs is installed?
if [ ! -f `which btrfs` ]; then
error "'btrfs' command is missing, run 'apt-get install btrfs-tools'"
exit 1
fi
# is directory a BTRFS volume?
result=`btrfs filesystem df $DIR`
if [ $? -ne 0 ]; then
error "$DIR is not a BTRFS volume, use DIR to define a BTRFS mount point"
exit 1
fi
# test if sub-volume not exists
id=`btrfs subvolume list $DIR | grep $NAME | grep ID | awk '{print $2}'`
if [ ! -z "$id" ]; then
error "error: sub-volume $NAME already exists in $DIR with ID $id, aborted" >&2
exit 1
fi
debug " - creating BTRFS sub-volume $NAME in $DIR"
# crear un sub-volumen
btrfs subvolume create $DIR/$NAME > /dev/null 2>&1
if [ $? -ne 0 ]; then
error "error creating btrfs sub-volume in volume $DIR, aborted" >&2
exit 1
fi
fi
}
# crear la carpeta para el rootfs
make_rootfs() {
# create directory
if [ ! -f "$ROOTFS" ]; then
mkdir -p $ROOTFS
if [ $? -ne 0 ]; then
error "error creating $ROOTFS folder" >&2
exit 1
fi
debug "- created rootfs folder $ROOTFS -"
fi
}
touch_config() {
if [ -d "$CONFIG" ]; then
debug "- created config file in $CONFIG -"
touch $CONFIGFILE
fi
}
####================= Main execution ==========================####
# variables
NAME=''
SIZE=''
IP=''
IFACE=''
NET_OPTIONS=''
VLAN_ID=''
DIST=''
TEMPLATE=''
ROLE=''
HOSTNAME=''
LAN_INTERFACE='eth0'
CLEAN_CACHE=0
FORCE=0
usage() {
echo "Usage: $(basename $0) [-n|--name=<container_name>] [-t|--template=<template-name>]
[-r|--role=<role-name>] [-A|--arch=<arch of the container>][--packages=<comma-separated package list>]
[-B|--backend=<dir|lvm|btrfs>] [--dhcp|--ip=<ip>] [-D|--domain=DOMAIN] [--clean] [--debug] [-h|--help]"
return 1
}
help() {
usage
cat <<EOF
This script is a helper to create lxc system containers
The script will create the container configuration file following
the informations submitted interactively with 'lxct-create'
Options:
-n, --name specify the name of the lxc container
-t, --template create container from a template script (debian, ubuntu, canaima, etc)
-r, --role role-based script for running in container after creation
-c, --clean create a clean version of cache container
-B, --backend define backend for container storage (default: dir, LVM or btrfs)
-s, --size if LVM or btrfs, volume size
-f, --fs if LVM, set filesystem format (ext4, xfs, btrfs)
-l, --lvm set the LVM volume group name for container creation
-D, --domain define Domain Name
--net specify network option (veth, macvlan, vlan, empty)
-i, --iface if macvlan or vlan option, define interface name (ex: eth0)
-v, --vlan if vlan is enabled, define vlan-id
-A, --arch Hardware Architecture of Container (i386, i686, x86_64)
--packages Extra comma-separated list of packages
--debug Enable debugging information
--dhcp Container interface is in dhcp mode
--ip IP Address of container
--force Destroy LXC before creation
Help options:
--help give this help list
--usage Display brief usage message
--version print program version
EOF
get_version
exit 1
}
### main execution program ###
if [ "$(id -u)" != "0" ]; then
error "$(basename $0): must be run as root" >&2
exit 1
fi
# basic checkin
check_backend()
{
if [[ "$1" = "lvm" || "$1" = "dir" || "$1" = "btrfs" ]]; then
return 0
else
usage_err "option '$1' invalid backend"
fi
}
check_net()
{
if [[ "$1" = "veth" ]] || [[ "$1" = "empty" ]] || [[ "$1" = "macvlan" ]] || [[ "$1" = "vlan" ]] || [[ "$1" = "phys" ]]; then
# TODO: check network options
return 0
else
usage_err "option '$1' invalid network option"
fi
}
check_name()
{
if [[ "${#1}" -gt 20 ]] || [[ "${#1}" -lt 2 ]]; then
usage_err "container name '$1' is an invalid name"
fi
}
check_directory()
{
if [ ! -d "$1" ]; then
usage_err "Directory '$1' doesn't exist."
fi
}
check_template()
{
case "$1" in
"debian")
DIST='Debian'
TEMPLATE='debian'
SUITE=`get_suite`
;;
"ubuntu")
DIST='Ubuntu'
TEMPLATE='ubuntu'
SUITE='precise'
MIRROR='http://archive.ubuntu.com/ubuntu'
;;
"canaima")
DIST='Canaima'
TEMPLATE='canaima'
SUITE='kerepakupai'
;;
"gentoo")
DIST='Gentoo'
TEMPLATE='gentoo'
;;
"archlinux")
DIST='Archlinux'
TEMPLATE='archlinux'
;;
"centos")
DIST='CentOS'
TEMPLATE='centos'
SUITE='6'
;;
"fedora")
DIST='Fedora'
TEMPLATE='fedora'
SUITE='18'
;;
*)
usage_err "option '$1' invalid template option"
exit 1
esac
}
# processing arguments
ARGS=`getopt -n$0 -u -a -o r:n:t:A:B:d:D:l:i:vf:s:h --longoptions packages:,net::,dhcp,ip::,dist::,suite::,debug,verbose,clean,version,help,size::,fs::,vlan::,iface::,lvm::,domain::,dir::,directory::,backend::,arch::,template::,role::,name:: -- "$@"`
eval set -- "$ARGS"
while [ $# -gt 0 ]; do
case "$1" in
-n|--name)
check_name "$2"
NAME=$2
shift
;;
-d|--dir|--directory)
optarg_check $1 $2
check_directory $2
DIR=$2
shift
;;
-t|--template)
check_template "$2"
shift
;;
-r|--role)
optarg_check $1 "$2"
ROLENAME=$2
shift
;;
-D|--domain)
optarg_check $1 $2
DOMAIN=$2
shift
;;
-A|--arch)
optarg_check $1 "$2"
ARCH=$2
shift
;;
-B|--backend)
check_backend "$2"
BACKEND_METHOD=$2
shift
;;
-l|--lvm)
optarg_check $1 "$2"
LVM=$2
shift
;;
-i|--iface)
optarg_check $1 "$2"
IFACE=$2
shift
;;
-v|--vlan)
optarg_check $1 "$2"
VLAN_ID=$2
shift
;;
-f|--fs)
optarg_check $1 "$2"
FS=$2
shift
;;
-s|--size)
optarg_check $1 "$2"
SIZE=$2
shift
;;
--packages)
PACKAGES="$2"
shift
;;
--net)
check_net "$2"
NET_TYPE=$2
shift
;;
--dhcp)
DHCP='y'
IP=''
;;
--ip)
optarg_check $1 "$2"
DHCP='n'
IP=$2
shift
;;
--dist)
optarg_check $1 "$2"
DIST=$2
shift
;;
--suite)
optarg_check $1 "$2"
SUITE=$2
shift
;;
--secure)
# creating a more secure container
SECURE='true'
;;
--debug)
VERBOSE='true'
;;
--verbose)
VERBOSE='true'
;;
--clean)
CLEAN_CACHE=1
;;
--force)
FORCE=1
;;
--version)
get_version
exit 0;;
-h|--help)
help
exit 1
;;
--)
break;;
-?)
usage_err "unknown option '$1'"
exit 1
;;
*)
info "$1"
usage
exit 1
;;
esac
shift
done
#
# fill all "auto" variables and check configuration
#
if [ -e /usr/lib/lxc-tools/lxct_config.sh ]; then
. /usr/lib/lxc-tools/lxct_config.sh
else
. ./lib/lxct_config.sh
fi
### main execution program ###
main()
{
# si no pasamos ningun parametro
if [ $# = 0 ]; then
# descubrir el dominio
get_domain
# descubrir el nombre del equipo
get_hostname
fi
# define logging facility
logging_file $NAME
CONFIG="$DIR/$NAME"
ROOTFS="$CONFIG/rootfs"
CONFIGFILE="$CONFIG/config"
# network configuration
get_network_info
# summary of changes
show_summary
# TODO, ¿pedir confirmación para proceder luego del sumario?
if [ "$VERBOSE" == 'true' ]; then
read -p "Continue (y/n)?" WORK
if [ "$WORK" != "y" ]; then
exit 0
fi
fi
case "$BACKEND_METHOD" in
"btrfs")
#TODO: no options yet
create_btrfs_volume
make_rootfs
touch_config
;;
"dir")
make_rootfs
# create config-file
touch_config
;;
"lvm")
# mount LVM volume
make_rootfs
create_lvm_volume
touch_config
;;
"*")
# invalid option
error "invalid method $BACKEND_METHOD" >&2
exit 1;;
esac
# create config file
create_lxc_config
#
## creating vm from template
get_template
set_suite_repository
debug "creating container from template $TEMPLATE"
. $TEMPLATE
create_container
if [ "$?" -ne "0" ]; then
error "failed to create Container, aborted"
exit 1
fi
# running any role defined into chroot
execute_roles
# cleanup container
cleanup_container
message "Creation of Container finished."
umount_lvm
# create container
lxc-create -n $NAME -f $CONFIGFILE
install_summary
info "You can run your container with the 'lxc-start -n $NAME -d'"
}
# = end = #
main
exit 0