Created
June 16, 2017 01:45
-
-
Save hayakawa/bb47998f447aafb1aad6f5c4941a9ae4 to your computer and use it in GitHub Desktop.
LXC instance setup(semi-automated)
This file contains hidden or 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
#!/bin/bash | |
LXCPATH="/usr/local/lxc/" | |
# Pre-check | |
if [ -f ./vms.list ]; | |
then | |
echo "[SUCCESS] ./vms.list is found" | |
else | |
echo "[ERROR] ./vms.list file is not found."; | |
exit 1; | |
fi | |
# Load some files | |
ADD_LXCS=( `cat ./vms.list | tr -d '\r' | tr '\n' ' '` ); | |
# Add some Groups | |
for i in "${ADD_LXCS[@]}"; do | |
# Get group id and group name. | |
HOSTNAME=`echo ${i} | cut -f 1 -d:`; | |
IPADDR=`echo ${i} | cut -f 2 -d:`; | |
VMGRP=`echo ${i} | cut -f 3 -d:`; | |
# Check group id and group name. | |
if [ -z ${HOSTNAME} -o -z ${IPADDR} -o -z ${VMGRP} ]; | |
then | |
echo "[ERROR] required param(s) not found...."; | |
exit 1; | |
fi | |
if [ ! -d ${LXCPATH}/var/lib/lxc/${HOSTNAME} ]; | |
then | |
echo "[FOUND] ${HOSTNAME} is not exists."; | |
echo "------[STEP 1] Creating instanse... Please Wait..."; | |
${LXCPATH}/bin/lxc-create -t corplab -n ${HOSTNAME} >/dev/null 2>&1 | |
echo "------[STEP 2] Setting groupname to instanse configuration..."; | |
sed --in-place=.old "s/REPLACE_ME/${VMGRP}/" ${LXCPATH}/var/lib/lxc/${HOSTNAME}/config | |
echo "------[STEP 3] Configure vm guest configuration..."; | |
cat ./ifcfg-eth0.tmpl | sed "s/{REPLACE_HOSTNAME}/${HOSTNAME}/" | sed "s/{REPLACE_IPADDR}/${IPADDR}/" > ${LXCPATH}/var/lib/lxc/${HOSTNAME}/rootfs/etc/sysconfig/network-scripts/ifcfg-eth0 | |
cat ./network.tmpl | sed "s/{REPLACE_HOSTNAME}/${HOSTNAME}/" > ${LXCPATH}/var/lib/lxc/${HOSTNAME}/rootfs/etc/sysconfig/network | |
echo "------[STEP 4] Configure vm guest password..."; | |
chroot ${LXCPATH}/var/lib/lxc/${HOSTNAME}/rootfs passwd | |
echo "------[STEP 5] Booting vm guest..."; | |
${LXCPATH}/bin/lxc-start -n ${HOSTNAME} | |
echo "------[END OF OPERATION]------" | |
else | |
echo "[SKIP] ${HOSTNAME} is already exists."; | |
fi | |
done | |
${LXCPATH}/bin/lxc-ls -f |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment