mgmnt: Add initial files to repository master
authorKristian Fiskerstrand <kf@sumptuouscapital.com>
Sat, 28 May 2016 21:09:50 +0000 (23:09 +0200)
committerKristian Fiskerstrand <kf@sumptuouscapital.com>
Sat, 28 May 2016 21:09:50 +0000 (23:09 +0200)
mgmnt/config.sh [new file with mode: 0644]
mgmnt/virt-template.xml [new file with mode: 0644]
mgmnt/virt_create_lxc_container.sh [new file with mode: 0755]
mgmnt/virt_define_lxc.sh [new file with mode: 0755]

diff --git a/mgmnt/config.sh b/mgmnt/config.sh
new file mode 100644 (file)
index 0000000..d74b144
--- /dev/null
@@ -0,0 +1,9 @@
+#!/bin/bash
+
+LXC_HOME="/var/lib/lxc"
+LVM_VG="vg0"
+LVM_PREFIX="lxc_"
+LVM_DEFAULT_SIZE="5G"
+
+ROOT_SSH_PUBKEY="/home/kristianf/.ssh/id_rsa.pub"
+
diff --git a/mgmnt/virt-template.xml b/mgmnt/virt-template.xml
new file mode 100644 (file)
index 0000000..58b80bd
--- /dev/null
@@ -0,0 +1,24 @@
+<domain type='lxc'>
+  <name>REPLACE</name>
+  <memory>327680</memory>
+  <os>
+    <type>exe</type>
+    <init>/sbin/init</init>
+  </os>
+  <vcpu>1</vcpu>
+  <clock offset='utc'/>
+  <on_poweroff>destroy</on_poweroff>
+  <on_reboot>restart</on_reboot>
+  <on_crash>destroy</on_crash>
+  <devices>
+    <emulator>/usr/libexec/libvirt_lxc</emulator>
+    <filesystem type='mount'>
+      <source dir='/var/lib/lxc/REPLACE/rootfs'/>
+      <target dir='/'/>
+    </filesystem>
+    <interface type='network'>
+      <source network='default'/>
+    </interface>
+    <console type='pty'/>
+  </devices>
+</domain>
diff --git a/mgmnt/virt_create_lxc_container.sh b/mgmnt/virt_create_lxc_container.sh
new file mode 100755 (executable)
index 0000000..f8dbad9
--- /dev/null
@@ -0,0 +1,27 @@
+#!/bin/bash
+source /etc/libvirt/lxc-gentoo-scripts/mgmnt/config.sh
+
+if [[ $# -lt 2 ]]; then
+        echo "Missing argument: <name> <distro>"
+        exit 1;
+fi
+
+NAME=${1}
+DISTRO=${2}
+
+LXC_ROOTFS="${LXC_HOME}/${NAME}/rootfs"
+
+echo "Creating LVM"
+lvcreate -L ${LVM_DEFAULT_SIZE} -n "${LVM_PREFIX}${NAME}" /dev/${LVM_VG}
+mkfs.ext4 /dev/${LVM_VG}/"${LVM_PREFIX}${NAME}"
+echo "/dev/${LVM_VG}/${LVM_PREFIX}${NAME}       ${LXC_HOME}/${NAME}        ext4            noatime         0 0" >> /etc/fstab
+mkdir -p ${LXC_HOME}/${NAME}
+mount ${LXC_HOME}/${NAME}
+
+echo "Creating LXC container and define for libvirt"
+lxc-create -t ${DISTRO} -n ${NAME}
+/usr/local/bin/virt_define_lxc.sh ${NAME}
+
+echo "Setting up root pubkey"
+mkdir -p ${LXC_HOME}/${NAME}/rootfs/root/.ssh/
+cat ${ROOT_SSH_PUBKEY} >> ${LXC_HOME}/${NAME}/rootfs/root/.ssh/authorized_keys
diff --git a/mgmnt/virt_define_lxc.sh b/mgmnt/virt_define_lxc.sh
new file mode 100755 (executable)
index 0000000..9eda11f
--- /dev/null
@@ -0,0 +1,26 @@
+#!/bin/bash
+
+source /etc/libvirt/lxc-gentoo-scripts/mgmnt/config.sh
+
+if [[ $# -lt 1 ]]; then
+        echo "Missing argument: name"
+        exit 1;
+fi
+
+NAME=${1}
+
+LXC_ROOTFS="${LXC_HOME}/${NAME}/rootfs"
+
+if [[ ! -d "${LXC_ROOTFS}" ]]; then
+        echo "No matching rootfs in ${LXC_HOME}/${NAME}/rootfs"
+        exit 2;
+fi
+
+if [[ -f "${LXC_HOME}/${NAME}.xml" ]]; then
+        echo "Container ${NAME} already defined"
+        exit 3;
+fi;
+
+sed "s/REPLACE/${NAME}/g" "${LXC_HOME}/virt-template.xml" > ${LXC_HOME}/${NAME}.xml
+virsh -c lxc:// define ${LXC_HOME}/${NAME}.xml
+