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

Questions about virtualization software
Forum rules
Before you post please read how to get help
Post Reply
User avatar
lewtwo
Level 3
Level 3
Posts: 143
Joined: Wed Oct 29, 2014 8:33 pm

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

Post by lewtwo » Sun Nov 18, 2018 12:55 am

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 lewtwo on Sun Nov 18, 2018 8:17 pm, edited 1 time in total.
"Engineering is the art of planning and forethought."
http://www.keywild.com

User avatar
AZgl1500
Level 9
Level 9
Posts: 2885
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 AZgl1500 » Sun Nov 18, 2018 3:13 am

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.

User avatar
lewtwo
Level 3
Level 3
Posts: 143
Joined: Wed Oct 29, 2014 8:33 pm

Re: ShutDown Vbox before Shutting down Linux

Post by lewtwo » Sun Nov 18, 2018 3:19 am

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
AZgl1500
Level 9
Level 9
Posts: 2885
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 AZgl1500 » Sun Nov 18, 2018 3:23 am

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

User avatar
lewtwo
Level 3
Level 3
Posts: 143
Joined: Wed Oct 29, 2014 8:33 pm

Re: ShutDown Vbox before Shutting down Linux

Post by lewtwo » Sun Nov 18, 2018 3:26 am

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 5
Level 5
Posts: 869
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: ShutDown Vbox before Shutting down Linux

Post by AndyMH » Sun Nov 18, 2018 5:54 am

Use a keyboard custom shortcut to run your script
Or add a launcher to the desktop or panel or menu.
Homebrew i5-8400+GTX1080 Cinnamon 19, Thinkpad T430 i7-3632 Cinnamon 19, Thinkpad T420 Cinnamon 18.3, Thinkpad T410 Cinnamon 17.3, Thinkpad T60 19.0 Mate

User avatar
lewtwo
Level 3
Level 3
Posts: 143
Joined: Wed Oct 29, 2014 8:33 pm

Re: ShutDown Vbox before Shutting down Linux

Post by lewtwo » 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.
"Engineering is the art of planning and forethought."
http://www.keywild.com

bassplayer
Level 1
Level 1
Posts: 38
Joined: Thu Aug 10, 2017 8:48 am

Re: ShutDown Vbox before Shutting down Linux

Post by bassplayer » Sun Nov 18, 2018 8:16 am

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 5
Level 5
Posts: 869
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: ShutDown Vbox before Shutting down Linux

Post by AndyMH » Sun Nov 18, 2018 9:18 am

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.
Homebrew i5-8400+GTX1080 Cinnamon 19, Thinkpad T430 i7-3632 Cinnamon 19, Thinkpad T420 Cinnamon 18.3, Thinkpad T410 Cinnamon 17.3, Thinkpad T60 19.0 Mate

User avatar
lewtwo
Level 3
Level 3
Posts: 143
Joined: Wed Oct 29, 2014 8:33 pm

Re: ShutDown Vbox before Shutting down Linux

Post by lewtwo » 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.
"Engineering is the art of planning and forethought."
http://www.keywild.com

bassplayer
Level 1
Level 1
Posts: 38
Joined: Thu Aug 10, 2017 8:48 am

Re: ShutDown Vbox before Shutting down Linux

Post by bassplayer » Sun Nov 18, 2018 11:38 am

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 5
Level 5
Posts: 741
Joined: Wed Nov 18, 2009 7:47 pm

Re: ShutDown Vbox before Shutting down Linux

Post by Pepi » 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:

User avatar
lewtwo
Level 3
Level 3
Posts: 143
Joined: Wed Oct 29, 2014 8:33 pm

Re: ShutDown Vbox before Shutting down Linux

Post by lewtwo » Sun Nov 18, 2018 1:03 pm

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 3
Level 3
Posts: 143
Joined: Wed Oct 29, 2014 8:33 pm

Re: ShutDown Vbox before Shutting down Linux

Post by lewtwo » 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.
"Engineering is the art of planning and forethought."
http://www.keywild.com

User avatar
lewtwo
Level 3
Level 3
Posts: 143
Joined: Wed Oct 29, 2014 8:33 pm

Re: ShutDown Vbox before Shutting down Linux

Post by lewtwo » Sun Nov 18, 2018 8:15 pm

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 5
Level 5
Posts: 741
Joined: Wed Nov 18, 2009 7:47 pm

Re: ShutDown Vbox before Shutting down Linux

Post by Pepi » Mon Nov 19, 2018 9:29 am

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:

Post Reply

Return to “Virtualization”