no virsh managedsave option?


20 posts in this topic Last Reply

Recommended Posts

I think maybe pause does virsh suspend and hibernate does virsh dompmsuspend.  But what would be much more useful for me is virsh managedsave to actually pause the vm in a way that will last through an unraid reboot.  Am I misunderstanding the situation?  Is there some way to add this functionality to the unraid gui instead of just running it from command line?

Edited by uek2wooF
s/without/instead of/
Link to post
Posted (edited)

Ok I found the related stuff in /usr/local/emhttp/plugins/dynamix.vm.manager/include/VMajax.php

 

This is for Pause:

 

case 'domain-pause':
        requireLibvirt();
        $arrResponse = $lv->domain_suspend($domName)
        ? ['success' => true, 'state' => $lv->domain_get_state($domName)]
        : ['error' => $lv->get_last_error()];
        break;

 

And this is for hibernate:

 

case 'domain-pmsuspend':
        requireLibvirt();
        // No support in libvirt-php to do a dompmsuspend, use virsh tool instead
        exec("virsh dompmsuspend ".escapeshellarg($uuid)." disk 2>&1", $arrOutput, $intReturnCode);
        $arrResponse = $intReturnCode==0
        ? ['success' => true, 'state' => $lv->domain_get_state($domName)]
        : ['error' => str_replace('error: ', '', implode('. ', $arrOutput))];
        break;

 

So it looks like I pretty much guessed right.  I am not sure I want to mess with libvirt-php so I think I will probably change Pause to something like this:

 

# nope.  no.  incorrect sorry

 

I don't know what all the error checking code there does exactly but I will just try it and see what happens.  I suppose to make it permanent through reboots I need to modify it with the User Scripts plugin each time.

 

Edited by uek2wooF
Link to post
Posted (edited)

Ugh.  Right after posting that I realized libvirt-php might have domain_managedsave, and yes it does.  So this will be even easier, just

 

s/domain_suspend/domain_save/

 

in VMajax.php

Edited by uek2wooF
save not managedsave there, it calls a wrapper for the libvirt stuff
Link to post

I recommend you create a feature request instead of hacking the code. When people make their own modifications it makes support impossible.

 

If/when a feature request gets honored, it has the additional benefit that all users have it available.

 

Link to post
Posted (edited)

 

I tested this and after a refresh of the VMs page, the new pause works well.  Very happy with this (on 6.8.3).   After I upgrade to 6.9 I will check to see if it still works.

 

# all wrong

 

Edited by uek2wooF
script was incomplete
Link to post
Posted (edited)

Good point.  It is saving to /var/lib/libvirt/qemu/save/VMname.save which is on / which I am not sure is even an actual disk.  I am looking into how to make it save somewhere else.

Edited by uek2wooF
Link to post

Another update, this is kind of embarrassing.  Anyway managedsave is not right.  I actually want virsh save and virsh restore.  Both take a file argument.  I don't see support for either one in libvirt.php though.  Apparently it is a bad idea to make that symlink for managedsave, not clear why but managedsave is "internal" to kvm.

 

The new plan was then to modify case 'domain-pmsuspend' (Pause in the menu) to use virsh save instead, and also modify 'domain-resume' to use virsh restore.  Since it is my box and i know the save dir will exist I will just have it save to /mnt/user/domain/save/$domName.save.  But the problem here is that after a virsh save, the vm looks "shut off", and not "paused", so the context menu to restore won't be there in the unraid gui.  I guess I could add some logic to look for a save file and provide the restore menu choice but this starts getting kind of complicated.

 

Link to post
Posted (edited)

ok this is really the end I hope.  bind mount is ok for managedsave apparently.  so I have a userscript with:

 

correct userscript in later post

 

Edited by uek2wooF
oops
Link to post
On 4/2/2021 at 6:47 PM, uek2wooF said:

ok this is really the end I hope.  bind mount is ok for managedsave apparently.  so I have a userscript with:

 




sed -i -e "s/domain_suspend/domain_save/" /usr/local/emhttp/plugins/dynamix.vm.manager/include/VMajax.php
mkdir -p /mnt/user/domains/save
mount --bind /mnt/user/domains/save /var/lib/libvirt/qemu/save

 

This is really interesting. So with this User Script, assuming it's run on each Array Start, it modifies the WebUI and is able to effectively hibernate the VM to /mnt/user/domains/save and restore from the same location. In other words, a VM can be paused, the server rebooted, after which the VM can still be resumed. 

Am I understanding correctly?

Edited by lnxd
Link to post
On 4/7/2021 at 3:31 AM, lnxd said:

This is really interesting. So with this User Script, assuming it's run on each Array Start, it modifies the WebUI and is able to effectively hibernate the VM to /mnt/user/domains/save and restore from the same location. In other words, a VM can be paused, the server rebooted, after which the VM can still be resumed. 

Am I understanding correctly?

 

That was the plan and it seems to work.  It should be the default save since the others aren't really useful for much.

 

Sorry for the delayed response, I don't get email notifications for some reason.

Link to post

And just to be clear, the opposite of this kind of pause is "start".  And yes the gui pause and start will work for this after running the userscript.  And yes run the userscript at array start.  The save file is removed after unpausing ("start"ing).

Link to post
42 minutes ago, uek2wooF said:

That was the plan and it seems to work.  It should be the default save since the others aren't really useful for much.

 

Sorry for the delayed response, I don't get email notifications for some reason.

Honestly this is pretty cool and I'm going to use it myself, thank you 😁

Just as a warning to anyone who decides to use this it's definitely worth taking note of above:

On 4/2/2021 at 5:15 AM, bonienl said:

When people make their own modifications it makes support impossible.

 

This script also assumes your Default VM storage path is /mnt/user/domains/, if it's not, it will create a share called domains unless you update the references to this path.


Also note that VM Manager needs to be started before you run the script if you are using the default Libvirt storage location which is a disk image that gets mounted when you start VM Manager. Otherwise when it gets started /mnt/user/domains/save will be bound to a location that no longer exists, and you'll possibly either get errors when pausing a VM or the local location won't have any data saved in it. So if you accidentally run the script with VM Manager stopped, you need to need to run the script again after starting it, or specifically the line:

mount --bind /mnt/user/domains/save /var/lib/libvirt/qemu/save


Also to revert to the original option I'm pretty sure you can just restart; or make sure no VMs are paused and run:

sed -i -e "s/domain_save/domain_suspend/" /usr/local/emhttp/plugins/dynamix.vm.manager/include/VMajax.php

You can then safely delete /mnt/user/domains/save which will likely be empty. I'm not going to include the code for deletion here in case someone makes a typo and deletes the contents of their array. 

 

Also there is a possibility that this will get broken / possibly become slightly dangerous in a new release, to be clear this is tested as working on versions 6.8.3 - 6.9.2. If anyone is really interested in making the change permanent; it appears relatively un-breaking and straight-forward to implement so like @bonienl said, best to make a feature request. This mount option has the benefit of not wasting space in libvirt.img, so if someone makes a feature request definitely reference @uek2wooF's script, because it's awesome.
 

Edited by lnxd
Link to post

I never tested after reboot on 6.8.2 but on 6.9.2 the bind mount isn't there after reboot.  The sed seems to be happening though.  I have to run the userscript again once I have access to the unraid console.  (The other commands are idempotent but the bind mount isn't.)

Link to post

The userscript is running too early.  I wonder if I can put a sleep in there without breaking anything.  I guess I will try.

 

mount: /var/lib/libvirt/qemu/save: mount point does not exist.

 

Link to post

I think the only way to use this is to disable autostart on VMs you want to use it with, and manually run the userscript before pausing the VMs.  Then reboot, manually run the userscript again, and then start your paused VMs manually.

Link to post
15 hours ago, uek2wooF said:

disable autostart on VMs

Unraid's autostart is limited anyway, no conditionals or timing. Much better to manage yourself with scripts. I've posted elsewhere on this forum how I manage it.

Link to post

Save dir doesn't exist until you save, so there was nowhere to bind mount to at boot.  This is the correct userscript.

 

sed -i -e "s/domain_suspend/domain_save/" /usr/local/emhttp/plugins/dynamix.vm.manager/include/VMajax.php
mkdir -p /mnt/user/domains/save /var/lib/libvirt/qemu/save
mount --bind /mnt/user/domains/save /var/lib/libvirt/qemu/save

 

Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.