2014-05-08 16:59:35 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# This is a helper script for creating a basic LXC container with some convenient packages
|
|
|
|
# ./create.sh [container_name]
|
|
|
|
|
2015-04-04 17:44:07 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
set -u
|
|
|
|
|
|
|
|
NAME=${1:-orchestra}
|
|
|
|
CONTAINER="/var/lib/lxc/$NAME/rootfs"
|
|
|
|
PASSWORD=$NAME
|
2015-04-29 10:38:41 +00:00
|
|
|
export SUITE="jessie"
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
[ $(whoami) != 'root' ] && {
|
|
|
|
echo -e "\nErr. This script should run as root\n" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
|
|
|
lxc-create -h &> /dev/null || {
|
|
|
|
echo -e "\nErr. It seems like LXC is not installed, run apt-get install lxc\n" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2015-04-29 13:17:21 +00:00
|
|
|
lxc-ls | grep -E "(^|\s)$NAME($|\s)" && {
|
|
|
|
echo -e "\nErr. Container with name $NAME already exists."
|
|
|
|
echo -e " You can destroy it by: sudo lxc-destroy -n $NAME\n" >&2
|
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
lxc-create -n $NAME -t debian
|
|
|
|
|
2015-04-29 13:17:21 +00:00
|
|
|
trap "umount $CONTAINER/{dev,sys}; exit 1;" INT TERM EXIT
|
2014-05-08 16:59:35 +00:00
|
|
|
mount --bind /dev $CONTAINER/dev
|
|
|
|
mount -t sysfs none $CONTAINER/sys
|
2015-04-29 13:17:21 +00:00
|
|
|
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
sed -i "s/\tlocalhost$/\tlocalhost $NAME/" $CONTAINER/etc/hosts
|
|
|
|
sed -i "s/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/" $CONTAINER/etc/locale.gen
|
|
|
|
chroot $CONTAINER locale-gen
|
|
|
|
|
2015-09-29 08:45:47 +00:00
|
|
|
echo -e "#!/bin/sh\nexit 101\n" > $CONTAINER/usr/sbin/policy-rc.d
|
|
|
|
chmod 755 $CONTAINER/usr/sbin/policy-rc.d
|
|
|
|
|
2015-04-29 13:17:21 +00:00
|
|
|
chroot $CONTAINER apt-get update
|
2014-05-08 16:59:35 +00:00
|
|
|
chroot $CONTAINER apt-get install -y --force-yes \
|
2015-04-04 17:44:07 +00:00
|
|
|
nano git screen sudo iputils-ping python3 python3-pip wget curl dnsutils rsyslog
|
2014-05-08 16:59:35 +00:00
|
|
|
|
2015-09-29 08:45:47 +00:00
|
|
|
rm $CONTAINER/usr/sbin/policy-rc.d
|
2014-05-08 16:59:35 +00:00
|
|
|
chroot $CONTAINER apt-get clean
|
|
|
|
|
|
|
|
|
|
|
|
sleep 0.1
|
2015-05-05 20:11:03 +00:00
|
|
|
umount $CONTAINER/{dev,sys} || {
|
|
|
|
echo "Killing processes inside the container ..."
|
|
|
|
lsof | grep $CONTAINER | awk {'print $2'} | uniq | xargs kill
|
|
|
|
umount $CONTAINER/{dev,sys}
|
|
|
|
}
|
2014-05-08 16:59:35 +00:00
|
|
|
trap - INT TERM EXIT
|