ShutDown Vbox before Shutting down Linux [solved] :-)

Questions about virtualization software
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
lewtwo
Level 4
Level 4
Posts: 254
Joined: Wed Oct 29, 2014 8:33 pm
Location: Houston, Texas

ShutDown Vbox before Shutting down Linux [solved] :-)

Post by lewtwo »

I have Windows 7 running as a virtual machine in seamless mode via Oracle VirtualBox with raw disk access.
Tis working so well that I may abandon VMware WorkStation.
I have one little nitpicking problem left:
When I shutdown Linux Mint, I need it to shutdown the Vbox Windows 7 virtual machine first.
Is there somewhere I can stuff a command in Linux Mint's Shutdown process ?

Linux Mint 19 Tara 64-bit, Kernel Linux 4.15.0-39-generic x86_64, MATE 1.20.1
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
"Engineering is the art of planning and forethought."
http://www.keywild.com
User avatar
AZgl1800
Level 20
Level 20
Posts: 11173
Joined: Thu Dec 31, 2015 3:20 am
Location: Oklahoma where the wind comes Sweeping down the Plains
Contact:

Re: ShutDown Vbox before Shutting down Linux

Post by AZgl1800 »

I find it far safer to shutdown VB first, and then tell Linux to shutdown.

but, you could write a bash script to do the job, but I don't know how.
LM21.3 Cinnamon ASUS FX705GM | Donate to Mint https://www.patreon.com/linux_mint
Image
User avatar
lewtwo
Level 4
Level 4
Posts: 254
Joined: Wed Oct 29, 2014 8:33 pm
Location: Houston, Texas

Re: ShutDown Vbox before Shutting down Linux

Post by lewtwo »

I think that I can write the script to shutdown windows and the virtual box ....
I just need to know where to put it so that it gets executed when Mint starts to shut down.

You Tube video demo: https://youtu.be/Obqn7ZjFN0g
This is a short demo of a multi-boot system with Windows 7, Linux Mint 19 Tara and Ubuntu Mate 18.10. The unique feature is that the Windows 7 OS can be booted on native hardware or run as a virtual Machine under the Linux Mint OS. Ubuntu Mate is only used for doing image backups of the other two operating system disk partitions. It is still a work in progress.
Last edited by lewtwo on Sun Nov 18, 2018 3:23 am, edited 1 time in total.
"Engineering is the art of planning and forethought."
http://www.keywild.com
User avatar
AZgl1800
Level 20
Level 20
Posts: 11173
Joined: Thu Dec 31, 2015 3:20 am
Location: Oklahoma where the wind comes Sweeping down the Plains
Contact:

Re: ShutDown Vbox before Shutting down Linux

Post by AZgl1800 »

Use a keyboard custom shortcut to run your script.
Have the final line of your script execute a terminal command shutdown now

I have control-alt-o setup now to do a shutdown

Shutdown -O.png
LM21.3 Cinnamon ASUS FX705GM | Donate to Mint https://www.patreon.com/linux_mint
Image
User avatar
lewtwo
Level 4
Level 4
Posts: 254
Joined: Wed Oct 29, 2014 8:33 pm
Location: Houston, Texas

Re: ShutDown Vbox before Shutting down Linux

Post by lewtwo »

I am building the system for another user.
It needs to use the standard UI.

This is the command to shut it down (gracefully):
virtualbox controlvm Windows7 acpipowerbutton
"Engineering is the art of planning and forethought."
http://www.keywild.com
User avatar
AndyMH
Level 21
Level 21
Posts: 13728
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: ShutDown Vbox before Shutting down Linux

Post by AndyMH »

Use a keyboard custom shortcut to run your script
Or add a launcher to the desktop or panel or menu.
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
User avatar
lewtwo
Level 4
Level 4
Posts: 254
Joined: Wed Oct 29, 2014 8:33 pm
Location: Houston, Texas

Re: ShutDown Vbox before Shutting down Linux

Post by lewtwo »

The concept here is:
If the user tells Linux mint to shutdown without shutting down VBox first then do a proper shutdown anyway.

There is a option of start up programs. Why not an equivalent option for shutdown ... it is much more critical than start up.
"Engineering is the art of planning and forethought."
http://www.keywild.com
bassplayer

Re: ShutDown Vbox before Shutting down Linux

Post by bassplayer »

lewtwo wrote: Sun Nov 18, 2018 7:46 am The concept here is:
If the user tells Linux mint to shutdown without shutting down VBox first then do a proper shutdown anyway.

There is a option of start up programs. Why not an equivalent option for shutdown ... it is much more critical than start up.
I'm used an init sys v system and not this systemd related stuff, but you could try putting a script in /etc/rc0.d and /etc/rc6.d for shutting down the vm's. VirtualBox has a script in both directories already called K01virtualbox. Just makes sure it has a name that is before that one, like K01ShutdownVMs or something that gets executed before K01virtualbox.
User avatar
AndyMH
Level 21
Level 21
Posts: 13728
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: ShutDown Vbox before Shutting down Linux

Post by AndyMH »

There is a option of start up programs. Why not an equivalent option for shutdown ... it is much more critical than start up.
Yes you can, but it means messing around with systemd, and from memory, I don't think you can control the order that processes shut down, so for example, your partitions may get unmounted before your script executes to shut down Vbox. For this reason, when I had a similar requirement, I wrote a script that did what I wanted and then shut the system down.
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
User avatar
lewtwo
Level 4
Level 4
Posts: 254
Joined: Wed Oct 29, 2014 8:33 pm
Location: Houston, Texas

Re: ShutDown Vbox before Shutting down Linux

Post by lewtwo »

bassplayer wrote: Sun Nov 18, 2018 8:16 am I'm used an init sys v system and not this systemd related stuff, but you could try putting a script in /etc/rc0.d and /etc/rc6.d for shutting down the vm's. VirtualBox has a script in both directories already called K01virtualbox. Just makes sure it has a name that is before that one, like K01ShutdownVMs or something that gets executed before K01virtualbox.
That was what I was looking for. By the way this systems does not have anything for virtualbox in rc0.d or rc6.d.
Anyway that is the easy part.

As I am running the Windows 7 VM with raw disk access I first need to go mess with "sudoers ".
Both the VM startup and VM shutdown require root authority.
"Engineering is the art of planning and forethought."
http://www.keywild.com
bassplayer

Re: ShutDown Vbox before Shutting down Linux

Post by bassplayer »

lewtwo wrote: Sun Nov 18, 2018 9:42 am
bassplayer wrote: Sun Nov 18, 2018 8:16 am I'm used an init sys v system and not this systemd related stuff, but you could try putting a script in /etc/rc0.d and /etc/rc6.d for shutting down the vm's. VirtualBox has a script in both directories already called K01virtualbox. Just makes sure it has a name that is before that one, like K01ShutdownVMs or something that gets executed before K01virtualbox.
That was what I was looking for. By the way this systems does not have anything for virtualbox in rc0.d or rc6.d.
Anyway that is the easy part.

As I am running the Windows 7 VM with raw disk access I first need to go mess with "sudoers ".
Both the VM startup and VM shutdown require root authority.
This is what I have in /etc/rc0.d

Code: Select all

...
lrwxrwxrwx 1 root root 17 Sep 27 13:40 K01openvpn -> ../init.d/openvpn
lrwxrwxrwx 1 root root 18 Sep 27 13:40 K01plymouth -> ../init.d/plymouth
lrwxrwxrwx 1 root root 17 Sep 27 13:40 K01rsyslog -> ../init.d/rsyslog
lrwxrwxrwx 1 root root 15 Sep 27 13:40 K01saned -> ../init.d/saned
lrwxrwxrwx 1 root root 27 Sep 27 13:40 K01speech-dispatcher -> ../init.d/speech-dispatcher
lrwxrwxrwx 1 root root 15 Sep 27 13:40 K01uuidd -> ../init.d/uuidd
lrwxrwxrwx 1 root root 20 Sep 27 14:43 K01virtualbox -> ../init.d/virtualbox
bassplayer@Gonzo:/etc/rc0.d$ 
Do you have the file /etc/init.d/virtualbox ?? As it seems like they are all links to the /etc/init.d folder.
User avatar
Pepi
Level 6
Level 6
Posts: 1305
Joined: Wed Nov 18, 2009 7:47 pm

Re: ShutDown Vbox before Shutting down Linux

Post by Pepi »

Always had a habit of shutting down all running programs before shutting down the OS. Probably from my Windoz years :mrgreen:
User avatar
lewtwo
Level 4
Level 4
Posts: 254
Joined: Wed Oct 29, 2014 8:33 pm
Location: Houston, Texas

Re: ShutDown Vbox before Shutting down Linux

Post by lewtwo »

bassplayer wrote: Sun Nov 18, 2018 11:38 am Do you have the file /etc/init.d/virtualbox ?? As it seems like they are all links to the /etc/init.d folder.
That would be a negatory.
"Engineering is the art of planning and forethought."
http://www.keywild.com
User avatar
lewtwo
Level 4
Level 4
Posts: 254
Joined: Wed Oct 29, 2014 8:33 pm
Location: Houston, Texas

Re: ShutDown Vbox before Shutting down Linux

Post by lewtwo »

Pepi wrote: Sun Nov 18, 2018 11:46 am Always had a habit of shutting down all running programs before shutting down the OS. Probably from my Windoz years :mrgreen:
At one point when I was in IT we had a check list posted on the wall of the server room.
It listed the shutdown order of all applications, servers, network switches, routers, UPSs, etc.
That was back when that required physically switching the power off some of those.
There was another for start up.

Also when I was in IT I learned to expect the operator to never do as they were instructed. :cry:
Hope for the best but plan for the worst.
"Engineering is the art of planning and forethought."
http://www.keywild.com
User avatar
lewtwo
Level 4
Level 4
Posts: 254
Joined: Wed Oct 29, 2014 8:33 pm
Location: Houston, Texas

Re: ShutDown Vbox before Shutting down Linux

Post by lewtwo »

The script turned out to be a tad bit more complicated.
So for posterity's sake:

Code: Select all

#!/bin/bash
# file ~/bin/Wind7ShutDown

# create a 'pause' function, used in debugging
function pause () {
   read -p "$*"
}

#
# This takes advantage of a line added to the file /etc/sudoers
# username ALL = NOPASSWD:  /usr/bin/VirtualBox, /usr/bin/virtualbox,  /usr/bin/vboxmanage
# This also assumes only ONE running VM
#
# sudo vboxmanage list runningvms
# "Windows7" {82dc1d4c-e680-4dd6-a2cd-d345bdf2d91d}
#
VM=$(sudo -u root vboxmanage list runningvms)

# test if we have a string that is not empty
if [[ $VM = *[!\ ]* ]]; then
  # extract the part of the string after '{'
  VM=${VM#*'{'}
  # Strip off the last characte '}'
  VM=${VM:0:$((${#VM} - 1))}
  # shut down the virtual machine via the UUID
  sudo -u root vboxmanage controlvm $VM acpipowerbutton
  # wait for it ...
  sleep 3s
  VM=$(sudo -u root vboxmanage list runningvms)
  while [[ $VM = *[!\ ]* ]];  do
    # echo $VM
    sleep 3s
    VM=$(sudo -u root vboxmanage list runningvms)
  done
fi
"Engineering is the art of planning and forethought."
http://www.keywild.com
User avatar
Pepi
Level 6
Level 6
Posts: 1305
Joined: Wed Nov 18, 2009 7:47 pm

Re: ShutDown Vbox before Shutting down Linux

Post by Pepi »

lewtwo wrote: Sun Nov 18, 2018 1:11 pm
Pepi wrote: Sun Nov 18, 2018 11:46 am Always had a habit of shutting down all running programs before shutting down the OS. Probably from my Windoz years :mrgreen:
At one point when I was in IT we had a check list posted on the wall of the server room.
It listed the shutdown order of all applications, servers, network switches, routers, UPSs, etc.
That was back when that required physically switching the power off some of those.
There was another for start up.

Also when I was in IT I learned to expect the operator to never do as they were instructed. :cry:
Hope for the best but plan for the worst.
All I can say is ... AMEN :mrgreen:
Locked

Return to “Virtual Machines”