Created
March 29, 2017 06:17
-
-
Save konstantin-kelemen/d3e7e58af0b4a9255e54d2bf1b5f1c07 to your computer and use it in GitHub Desktop.
some description
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
#!/bin/bash | |
VMID=$1 | |
OSVER=$2 | |
SKIPDOWNLOAD=$3 | |
if [ "$OSVER" == "cleanall" ]; then | |
echo "Cleaning" | |
rm -rf /home/work/rootvm/unpacked/vm$VMID | |
rm -rf /home/work/rootvm/vzdump-openvz-$VMID*.tar | |
rm -rf /home/work/transfer/vm$VMID.qcow2 | |
rm -rf /home/work/transfer/vm$VMID | |
exit | |
fi | |
if [ "$OSVER" == "cleanimage" ]; then | |
echo "Cleaning" | |
rm -rf /home/work/transfer/vm$VMID.qcow2 | |
rm -rf /home/work/transfer/vm$VMID | |
exit | |
fi | |
if [ "$SKIPDOWNLOAD" != "skip" ]; then | |
echo "Copying dump from the node" | |
sshpass -f nodepass scp [email protected]:/var/lib/vz/dump/dump/vzdump-openvz-$VMID*.lzo /home/work/rootvm/ | |
echo "Unpacking the lzo dump" | |
lzop -d /home/work/rootvm/vzdump-openvz-$VMID*.lzo | |
echo "Removing the lzo archive" | |
rm /home/work/rootvm/vzdump-openvz-$VMID*.lzo | |
echo "Making a directory to unpack the tar dump to" | |
mkdir /home/work/rootvm/unpacked/vm$VMID | |
echo "Unpacking the tar dump" | |
tar -C /home/work/rootvm/unpacked/vm$VMID -xf /home/work/rootvm/vzdump-openvz-$VMID*.tar | |
if [ -d "/home/work/rootvm/unpacked/vm$VMID/etc/apt" ]; then | |
echo "Managing apt repositories" | |
mv /home/work/rootvm/unpacked/vm$VMID/etc/apt /home/work/rootvm/unpacked/vm$VMID/etc/apt.original | |
fi | |
if [ -d "/home/work/rootvm/unpacked/vm$VMID/etc/yum.repos.d" ]; then | |
echo "Managing yum repositories" | |
mv /home/work/rootvm/unpacked/vm$VMID/etc/yum.repos.d /home/work/rootvm/unpacked/vm$VMID/etc/yum.repos.d.original | |
fi | |
else | |
echo "Download process skipped" | |
fi | |
echo "Copying the stock qcow2 image" | |
cp /home/work/$OSVER/$OSVER-resized.qcow2 /home/work/transfer/vm$VMID.qcow2 | |
echo "Making a directory to mount the stock image to" | |
mkdir /home/work/transfer/vm$VMID | |
echo "Mounting the image" | |
guestmount -a /home/work/transfer/vm$VMID.qcow2 -m /dev/sda1 --rw /home/work/transfer/vm$VMID | |
echo "Copying the packages list" | |
cp /home/work/$OSVER/packages.list /home/work/transfer/vm$VMID/ | |
echo "Merging the passwd, shadow, and groups files" | |
if [ "$SKIPDOWNLOAD" != "norsync" ]; then | |
RSYNCPASRAMS=$(/home/work/passwd-shadow-group-megre-script.pl /home/work/rootvm/unpacked/vm$VMID /home/work/transfer/vm$VMID | tail -n 1) | |
echo "Rsyncing the dump into the stock image" | |
rsync $RSYNCPASRAMS --exclude-from=/home/work/exclude.txt /home/work/rootvm/unpacked/vm$VMID/ /home/work/transfer/vm$VMID/ 2>/dev/null | |
if [ "$OSVER" == "debian7" ] || [ "$OSVER" == "debian8" ] || [ "$OSVER" == "ubuntu1404" ]; then | |
echo "Cleverly merging /etc/rcX.d directories" | |
for RUNLEVEL in {0..7}; do | |
if [ "$RUNLEVEL" == "7" ]; then | |
ACTUALRUNLEVEL=S | |
else | |
ACTUALRUNLEVEL=$RUNLEVEL | |
fi | |
for SERVICE in `ls -1 /home/work/rootvm/unpacked/vm$VMID/etc/rc$ACTUALRUNLEVEL.d/ | cut -c4-`; do | |
ls -1 /home/work/transfer/vm$VMID/etc/rc$ACTUALRUNLEVEL.d/ | grep $SERVICE > /dev/null | |
if [ $? -ne 0 ]; then | |
cp -a /home/work/rootvm/unpacked/vm$VMID/etc/rc$ACTUALRUNLEVEL.d/*$SERVICE /home/work/transfer/vm$VMID/etc/rc$ACTUALRUNLEVEL.d/ | |
fi | |
done | |
done | |
fi | |
if [ "$OSVER" == "centos6" ]; then | |
echo "Cleverly merging /etc/rcX.d directories" | |
for RUNLEVEL in {0..6}; do | |
ACTUALRUNLEVEL=$RUNLEVEL | |
for SERVICE in `ls -1 /home/work/rootvm/unpacked/vm$VMID/etc/rc$ACTUALRUNLEVEL.d/ | cut -c4-`; do | |
ls -1 /home/work/transfer/vm$VMID/etc/rc$ACTUALRUNLEVEL.d/ | grep $SERVICE > /dev/null | |
if [ $? -ne 0 ]; then | |
cp -a /home/work/rootvm/unpacked/vm$VMID/etc/rc$ACTUALRUNLEVEL.d/*$SERVICE /home/work/transfer/vm$VMID/etc/rc$ACTUALRUNLEVEL.d/ | |
fi | |
done | |
done | |
fi | |
if [ "$OSVER" == "debian7" ] || [ "$OSVER" == "debian8" ] || [ "$OSVER" == "ubuntu1404" ]; then | |
echo "Managing services" | |
cp /home/work/debian7/debian7.bash /home/work/transfer/vm$VMID/transfer-debian7.bash | |
chroot /home/work/transfer/vm$VMID /transfer-debian7.bash | |
rm /home/work/transfer/vm$VMID/transfer-debian7.bash | |
fi | |
if [ "$OSVER" == "centos6" ]; then | |
echo "Managing services" | |
cp /home/work/centos6/centos6.bash /home/work/transfer/vm$VMID/transfer-centos6.bash | |
chroot /home/work/transfer/vm$VMID /transfer-centos6.bash | |
rm /home/work/transfer/vm$VMID/transfer-centos6.bash | |
fi | |
if [ "$OSVER" == "centos7" ]; then | |
echo "Managing services" | |
cp /home/work/centos7/centos7.bash /home/work/transfer/vm$VMID/transfer-centos7.bash | |
chroot /home/work/transfer/vm$VMID /transfer-centos7.bash | |
rm /home/work/transfer/vm$VMID/transfer-centos7.bash | |
fi | |
echo "Unmounting the image" | |
guestunmount /home/work/transfer/vm$VMID | |
echo "Image unmounted. Upload it to the cloud an install packages listed in /packages.list" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment