Shutdown VMs based on storage

np86

Well-Known Member
Jun 11, 2016
38
3
48
37
Is it possible to shutdown VMs from CLI based on what storage they are on?

I tried to look through the documentation, but couldnt find the right command for this.
 
Not directly, but you can go through all configuration files, extract the ones on your storage and shutdown the VMs.
Would it be possible to throw a command, which goes through all the config files, and make me a list of vms, that are on one specific storage?
 
Hello,

somethin easy like:
Code:
cat /etc/pve/nodes/server/qemu-server/*.conf|grep "local2"

Replace all <between> with your values

cat /etc/pve/nodes/<your server name>/qemu-server/*.conf|grep "l<your storage name>"

or when you want to get all disks from every servers in a cluster

cat /etc/pve/nodes/*/qemu-server/*.conf|grep "l<your storage name>"

Best regards
Guenter
 
Hello,

somethin easy like:
Code:
cat /etc/pve/nodes/server/qemu-server/*.conf|grep "local2"

Replace all <between> with your values

cat /etc/pve/nodes/<your server name>/qemu-server/*.conf|grep "l<your storage name>"

or when you want to get all disks from every servers in a cluster

cat /etc/pve/nodes/*/qemu-server/*.conf|grep "l<your storage name>"

Best regards
Guenter
You don't need to cat first, just run grep directly on the files and extract the vmid to shut down.

Thanks. So this sort of work. How do I export this to a text file? From where I can initiate the qm stop command.
What I had in mind was, qm stop vm-on-storage.txt

root@pvetest:~# cat /etc/pve/nodes/pvetest/qemu-server/* | grep local-lvm
virtio0: local-lvm:vm-100-disk-1,size=2G
virtio0: local-lvm:vm-101-disk-1,size=2G

Btw is doesnt work without cat, I get permission denied
 
Okay, let's built if for you:

Code:
root@proxmox:/etc/pve/qemu-server# grep harddisk *conf | cut -d: -f1 | sed -s 's/\.conf//' | sort -u
10000
10001

This yields all VMs with given storage named 'harddisk'. You can then just do a little loop around to stop the VMs:

Code:
root@proxmox:/etc/pve/qemu-server# for vm in $(  grep harddisk *conf | cut -d: -f1 | sed -s 's/\.conf//' ); do qm shutdown $vm; done
 
  • Like
Reactions: GUENTER
Okay, let's built if for you:

Code:
root@proxmox:/etc/pve/qemu-server# grep harddisk *conf | cut -d: -f1 | sed -s 's/\.conf//' | sort -u
10000
10001

This yields all VMs with given storage named 'harddisk'. You can then just do a little loop around to stop the VMs:

Code:
root@proxmox:/etc/pve/qemu-server# for vm in $(  grep harddisk *conf | cut -d: -f1 | sed -s 's/\.conf//' ); do qm shutdown $vm; done
Thanks you very much.
Almost done here. I want to make this a one liner, and dont have to cd in the directory, since this will be run on multiple servers.

This is what I got now:
Code:
root@pvetest:~# grep local-lvm /etc/pve/nodes/*/qemu-server/*.conf | cut -d: -f1 | sed -s 's/\.conf//' | sort -u
/etc/pve/nodes/pvetest/qemu-server/100
/etc/pve/nodes/pvetest/qemu-server/101

How do i strip the "/etc/pve/nodes/*/qemu-server/" part?
 
Bash on liners, I like them!

But if I use your example I get:
Code:
root@server:/etc/pve/nodes# grep local2 *conf | cut -d: -f1 | sed -s 's/\.conf//' | sort -u
grep: *conf: No such file or directory

So when I use your for with mine advanced example this works:
Code:
root@server:/etc/pve/nodes# for vm in $( cat /etc/pve/nodes/server/qemu-server/*.conf | grep "local2" |awk -F":" '{ print $3 }'|cut -d"/" -f1 ); do qm status $vm; done
status: stopped

Best regards
Guenter
 
Bash on liners, I like them!

But if I use your example I get:
Code:
root@server:/etc/pve/nodes# grep local2 *conf | cut -d: -f1 | sed -s 's/\.conf//' | sort -u
grep: *conf: No such file or directory

So when I use your for with mine advanced example this works:
Code:
root@server:/etc/pve/nodes# for vm in $( cat /etc/pve/nodes/server/qemu-server/*.conf | grep "local2" |awk -F":" '{ print $3 }'|cut -d"/" -f1 ); do qm status $vm; done
status: stopped

Best regards
Guenter
your command give me this:
Code:
root@pvetest:~# for vm in $( cat /etc/pve/nodes/*/qemu-server/*.conf | grep "local-lvm" |awk -F":" '{ print $3 }'|cut -d"/" -f1 ); do qm status $vm; done
400 Parameter verification failed.
vmid: type check ('integer') failed - got 'vm-100-disk-1,size=2G'
qm status <vmid> [OPTIONS]
400 Parameter verification failed.
vmid: type check ('integer') failed - got 'vm-101-disk-1,size=2G'
qm status <vmid> [OPTIONS]

But if i change the awk parameter, is seems to work. Dont know why mine are so different from yours
Code:
root@pvetest:~# for vm in $( cat /etc/pve/nodes/*/qemu-server/*.conf | grep "local-lvm" |awk -F"-" '{ print $3 }'|cut -d"/" -f1 ); do qm status $vm; done
status: stopped
status: stopped
 
I have everything working now, but I forgot one important thing.
Is it possible to make the output to text file, only show running vms?
So only the vms that were started before, will be started when I am running this script to start the vms
 
Code:
qm list >> textfile.txt

Will save a textfile with a list of all machines and if you play around you will get

Code:
qm list|grep running >> textfile.txt

This are all Linux "basics"

Over and out
 
Code:
qm list >> textfile.txt

Will save a textfile with a list of all machines and if you play around you will get

Code:
qm list|grep running >> textfile.txt

This are all Linux "basics"

Over and out
Yes I was already playing with this method. Only problem is, it doesnt tell me what storage the vm is on, which is what I need for this script.
 
What if I qm suspend the vms on that storage, and then restart that storage server, and after use qm resume (since you cant resume machines that are already stopped)
Would this work, or would I get an error on my vm even though the vm is suspended?
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!