PDA

View Full Version : How to change OpenVZ VPS OS template / rebuild reinstall VPS from command line



Fli
07-02-2015, 12:25 AM
I did not found any swith for this operation in vzctl manual page. (man vzctl)

So i did i changed OpenVZ VPS OS template / reinstalled VPS this way from SSH shell / command line:
A) Backup VPS config file, B) destroy VPS, C) create VPS with new template, D) restore config file

TUTORIAL, STEP BY STEP

I know OpenVZ VPS IP that i need to rebuild (use new/different OStemplate on it)

1) get that VPS details
vzlist -a | grep VPSIPADDRESS

2) i know VPS ID now (example: 2080)

3) stop that VPS
vzctl stop 2080

4) copy/backup that VPS config file
cp -p /etc/vz/conf/2080.conf /etc/vz/conf/2080.conf_bckp

5) permanently destroy VPS, delete all its data! (you will NOT be able to restore them!)
vzctl destroy 2080

6) create new VPS with same ID and with new OS template:
vzctl create 2080 --ostemplate centos-6-x86_64
(to discover right ostemplate name, do: ls /vz/template/cache | tr -d tar.gz)

7) replace new VPS config file with old, tuned backed up one:
mv /etc/vz/conf/2080.conf_bckp /etc/vz/conf/2080.conf

8) start VPS
vzctl start 2080

A shell/bash script can be created for this like:


echo ""
echo "This script will guide You thru OpenVZ VPS ostemplate change/rebuild/reinstall process.
Script will never do any data erase without providing warning and asking for confirmation."
echo ""
vzlist -a
echo ""
echo "Enter VPS ID to work with:"
read ctid
echo ""
ls /vz/template/cache
echo ""
echo "Enter OStemplate name (see above) that you want to apply/use on a VPS $ctid. do NOT add .tar.gz at the end. Proper example form: centos-6-x86_64"
read tname
echo ""
echo "You selected following VPS to be reinstalled or OStemplate changed:"
vzlist -a | grep $ctid
echo ""
echo "WARNING: reinstall/OStemplate change will cause all VPS data be deleted permanently !!!!!!!!!"
echo "Do you really want all data on above mentioned VPS deleted and OS template $tname applied on it? (y = yes (delete data, reinstall), n = no (exit))"
read action

if [ "$action" == "y" ];then
vzctl stop $ctid
cp -p /etc/vz/conf/$ctid.conf /etc/vz/conf/$ctid.conf_bckp
echo "Was an .conf file coppied or there was an error? If there was an error, do Ctrl+C keyb. shortcut to quit this script.
If no /"cp/" error, then hit any key to continue:"
read qwertz
vzctl destroy $ctid
vzctl create $ctid --ostemplate $tname
echo ""
echo "Now please hit /"y/" key and Enter key to use old VPS config file - to rewrite new config by old VPS config"
echo "mv /etc/vz/conf/$ctid.conf_bckp /etc/vz/conf/$ctid.conf"
vzctl start $ctid
echo ""
echo "VPS $ctid should be running now and have new operating system $tname."
vzlist -a | grep $ctid
echo ""
echo "You may want to update an OSTEMPLATE variable in VPS config file /etc/vz/conf/$ctid.conf
New value is: $tname"
elif [ "$action" != "y" ];then
echo "You did not used Y key, so the script exited."
exit
fi