Working as a systems administrator, there are many times where you want to automate things so that you don’t have to do them manually. A lot of times I am working with OpenVZ nodes, I run into situations where I have to run a command in all vm’s or reboot all vm’s (don’t reboot all in production node). Here is a simple linux command that will find all openvz vm’s and reboot / restart or do anything with it.

Reboot All Containers

for VE in $(vzlist -Ha -o veid); do vzctl restart $VE; done

Start All Containers

for VE in $(vzlist -Ha -o veid); do vzctl start $VE; done

 

Migrate All Containers

 

for VE in $(vzlist -Ha -o veid); do vzmigrate --remove-area no --keep-dst [NODE_IP] $VE; done
Just replace the [NODE_IP] with the ip of the node that you want to migrate them to. Make sure you have keys on the node that you are migrating to so that it doesn’t repititively ask you for password.
You can pretty much do anything with all the VM’s by following the following format:
for VE in $(vzlist -Ha -o veid); do [COMMAND]; done

Just replace the [COMMAND] with the command you want to run for each VM. Use $VE to get the VEID of the container.

Similar Posts: