Created
May 8, 2015 08:20
-
-
Save calbrecht/adf364af178f17e61e93 to your computer and use it in GitHub Desktop.
create sles lxc container within mounted img from vmdk img.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
test 0 != $(id -u) && { echo "sudo ${0} ${@}"; sudo ${0} ${@} && exit 0 || exit 1; } | |
BOX_NAME=sles11sp3 | |
AUTOINST=https://raw.githubusercontent.com/jedi4ever/veewee/master/templates/SLES-11-SP3-DVD-x86_64-GM/autoinst.xml | |
BOX_HOME=${HOME}/.vagrant.d/boxes/${BOX_NAME}/0/virtualbox | |
BOX_VMDK=${BOX_HOME}/box-disk1.vmdk | |
LXC_DEFAULT_CONFIG=$(lxc-config lxc.default_config) | |
LXC_BASE_PATH=$(lxc-config lxc.lxcpath) | |
BOX_TMP=/tmp/${BOX_NAME} | |
BOX_MNT=${BOX_TMP}/mnt | |
BOX_RAW=${BOX_TMP}/box.img | |
test -d ${BOX_MNT} || mkdir -p ${BOX_MNT} | |
test -f ${BOX_RAW} || qemu-img convert -O raw ${BOX_VMDK} ${BOX_RAW} | |
OFFSET=$(fdisk -l ${BOX_RAW} | awk '/Linux$/{print ($3 * 512)}') | |
test -d ${BOX_MNT}/etc || mount -o loop,offset=${OFFSET} ${BOX_RAW} ${BOX_MNT} | |
cp /etc/resolv.conf ${BOX_MNT}/etc/ | |
chroot ${BOX_MNT} zypper install -y lxc | |
chroot ${BOX_MNT} lxc-create --name ${BOX_NAME} -B none --template sles | |
LXC_SRC_PATH=${BOX_MNT}/var/lib/lxc/${BOX_NAME} | |
LXC_SRC_ROOTFS=${LXC_PATH}/rootfs | |
lxc-stop --name ${BOX_NAME} | |
LXC_PATH=${LXC_BASE_PATH}/${BOX_NAME} | |
mkdir -p ${LXC_PATH} | |
rm -fr ${LXC_PATH}/* | |
tar --numeric-owner --create --directory ${LXC_SRC_PATH} --to-stdout rootfs \ | |
| tar --numeric-owner --get --verbose --directory ${LXC_PATH} | |
LXC_ROOTFS=${LXC_PATH}/rootfs | |
cp /etc/resolv.conf ${LXC_ROOTFS}/etc/ | |
curl ${AUTOINST} > ${LXC_ROOTFS}/autoinst.xml | |
tee ${LXC_ROOTFS}/autoinst.sh <<EOF | |
export TERM=linux | |
sed -i '/<bootloader/,/<\/bootloader/c <bootloader/>' /autoinst.xml | |
sed -i '/<partitioning/,/<\/partitioning/c <partitioning/>' /autoinst.xml | |
sed -i '/<networking/,/<\/networking/c <networking/>' /autoinst.xml | |
sed -i '/<software/,/<\/software/c <software/>' /autoinst.xml | |
/sbin/yast2 --ncurses ayast_setup setup verbose filename=/autoinst.xml | |
# remove zypper locks on removed packages to avoid later dependency problems | |
zypper --non-interactive rl \* | |
zypper install -y --no-recommends sudo | |
zypper install -y rubygem-puppet | |
date > /etc/vagrant_box_build_time | |
# install vagrant key | |
echo -e "\ninstall vagrant key ..." | |
mkdir -m 0700 /home/vagrant/.ssh | |
cd /home/vagrant/.ssh | |
wget --no-check-certificate -O authorized_keys https://raw.github.com/mitchellh/vagrant/master/keys/vagrant.pub | |
chmod 0600 /home/vagrant/.ssh/authorized_keys | |
chown -R vagrant.users /home/vagrant/.ssh | |
# update sudoers | |
echo -e "\nupdate sudoers ..." | |
echo -e "\n# added by veewee/postinstall.sh" >> /etc/sudoers | |
echo -e "vagrant ALL=(ALL) NOPASSWD: ALL\n" >> /etc/sudoers | |
# speed-up remote logins | |
echo -e "\nspeed-up remote logins ..." | |
echo -e "\n# added by veewee/postinstall.sh" >> /etc/ssh/sshd_config | |
echo -e "UseDNS no\n" >> /etc/ssh/sshd_config | |
exit 0 | |
EOF | |
chmod 755 ${LXC_ROOTFS}/autoinst.sh | |
tee ${LXC_PATH}/lxc-config <<EOF | |
lxc.tty = 4 | |
lxc.pts = 1024 | |
# When using LXC with apparmor, uncomment the next line to run unconfined: | |
#lxc.aa_profile = unconfined | |
lxc.cgroup.devices.deny = a | |
# /dev/null and zero | |
lxc.cgroup.devices.allow = c 1:3 rwm | |
lxc.cgroup.devices.allow = c 1:5 rwm | |
# consoles | |
lxc.cgroup.devices.allow = c 5:1 rwm | |
lxc.cgroup.devices.allow = c 5:0 rwm | |
lxc.cgroup.devices.allow = c 4:0 rwm | |
lxc.cgroup.devices.allow = c 4:1 rwm | |
# /dev/{,u}random | |
lxc.cgroup.devices.allow = c 1:9 rwm | |
lxc.cgroup.devices.allow = c 1:8 rwm | |
lxc.cgroup.devices.allow = c 136:* rwm | |
lxc.cgroup.devices.allow = c 5:2 rwm | |
# rtc | |
lxc.cgroup.devices.allow = c 254:0 rwm | |
#oracle wants shm, notice the option create=dir | |
#lxc.mount.entry = shm dev/shm tmpfs size=8192000k,nosuid,nodev,noexec,create=dir 0 0 | |
EOF | |
tee ${LXC_ROOTFS}/etc/fstab <<EOF | |
proc /proc proc defaults 0 0 | |
sysfs /sys sysfs noauto 0 0 | |
EOF | |
tee ${LXC_PATH}/config <${LXC_DEFAULT_CONFIG} | |
tee --append ${LXC_PATH}/config <<EOF | |
lxc.utsname = ${BOX_NAME} | |
lxc.rootfs = ${LXC_ROOTFS} | |
EOF | |
tee --append ${LXC_PATH}/config <${LXC_PATH}/lxc-config | |
lxc-start --name ${BOX_NAME} --daemon | |
lxc-attach --name ${BOX_NAME} -- /autoinst.sh | |
#lxc-attach --name ${BOX_NAME} -- /bin/bash | |
lxc-stop --name ${BOX_NAME} | |
tee ${LXC_PATH}/metadata.json <<EOF | |
{ | |
"provider": "lxc", | |
"version": "1.0.0", | |
"built-on": "$(date --utc)" | |
} | |
EOF | |
tee ${LXC_PATH}/lxc-template <<EOF | |
#!/usr/bin/env bash | |
set -eu | |
LXC_MAPPED_GID= | |
LXC_MAPPED_UID= | |
LXC_NAME= | |
LXC_PATH= | |
LXC_ROOTFS= | |
LXC_TARBALL= | |
LXC_CONFIG= | |
options=\$(getopt -o h -l tarball:,config:,help:,name:,path:,rootfs:,mapped-uid:,mapped-gid: -- "\$@") | |
test \$? -ne 0 && exit 1 | |
eval set -- "\$options" | |
while true | |
do | |
case "\$1" in | |
-h|--help) exit 0;; | |
--config) LXC_CONFIG=\$2; shift 2;; | |
--tarball) LXC_TARBALL=\$2; shift 2;; | |
--name) LXC_NAME=\$2; shift 2;; | |
--path) LXC_PATH=\$2; shift 2;; | |
--rootfs) LXC_ROOTFS=\$2; shift 2;; | |
--mapped-uid) LXC_MAPPED_UID=\$2; shift 2;; | |
--mapped-gid) LXC_MAPPED_GID=\$2; shift 2;; | |
*) break;; | |
esac | |
done | |
cat \${LXC_CONFIG} >> \${LXC_PATH}/config | |
tar --extract --numeric-owner --verbose --directory \${LXC_PATH} --file \${LXC_TARBALL} rootfs | |
exit 0 | |
EOF | |
LXC_BOX=${LXC_PATH}.box | |
rm -f ${LXC_BOX} | |
tar --numeric-owner --verbose --create --directory ${LXC_PATH} --file ${LXC_PATH}/rootfs.tar.gz --gz rootfs | |
tar --numeric-owner --verbose --create --directory ${LXC_PATH} --file ${LXC_BOX} rootfs.tar.gz | |
tar --numeric-owner --verbose --append --directory ${LXC_PATH} --file ${LXC_BOX} metadata.json | |
tar --numeric-owner --verbose --append --directory ${LXC_PATH} --file ${LXC_BOX} lxc-config | |
tar --numeric-owner --verbose --append --directory ${LXC_PATH} --file ${LXC_BOX} lxc-template | |
rm -fr ${LXC_PATH} | |
lxc-destroy --name ${BOX_NAME} | |
echo ${LXC_BOX} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment