Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Shutting down a VirtualBox VM automatically at log off

  1. #1
    Join Date
    Dec 2008
    Location
    Moscow, Russia
    Posts
    11

    Default Shutting down a VirtualBox VM automatically at log off

    I use openSUSE 13.1 with KDE. If I log off when a VirtualBox VM is running, a message box pops up asking whether I want to save the machine state, send the shutdown signal or power off the machine. I do not want to press keys manually, instead of it I want to execute the command

    VBoxManage controlvm <vm> savestate


    The problem is I cannot find the right place to put the command. If I put it in a shutdown script in ~/.kde4/shutdown or even in an init script, the same message box pops up. I also tried to put the command in /usr/bin/startkde with the same result. How can I run the command before VirtualBox pops up the message box?

  2. #2

    Default Re: Shutting down a VirtualBox VM automatically at log off

    First create your script and then just symlink them to the correct kde directory.

    Code:
    "$HOME/.kde4/Autostart/"
    "$HOME/.kde4/shutdown/"
    This is also thesame when using

    Code:
    Systemsettings -> Startup and shutdown -> Autostart -> Scriptfile -> Add script
    You will also have an option to run your script at start or shutdown in KDE.
    Of course the script needs to be executable...
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  3. #3
    Join Date
    Dec 2008
    Location
    Moscow, Russia
    Posts
    11

    Default Re: Shutting down a VirtualBox VM automatically at log off

    Quote Originally Posted by jetchisel View Post
    First create your script and then just symlink them to the correct kde directory.
    I've already done it as could be seen from my first post. It results in a pop up window when I try to log off. By the way if I press the button in the pop up window I have described in my first post, then the VM is shut down but logging off is canceled. It is a bug in VirtualBox but my question is not about the bug, but how I can shut down the VM automatically.

  4. #4

    Default Re: Shutting down a VirtualBox VM automatically at log off

    Quote Originally Posted by mike934 View Post
    I've already done it as could be seen from my first post. It results in a pop up window when I try to log off. By the way if I press the button in the pop up window I have described in my first post, then the VM is shut down but logging off is canceled. It is a bug in VirtualBox but my question is not about the bug, but how I can shut down the VM automatically.
    Ok, let me give you some holding hands

    The startup script.
    Code:
    #!/usr/bin/env bash
    
    exec >> ~/VBoxAutoStart.log 2>&1
    
    printf '%s\n' -----"$(date +%d-%b-%Y-[%H:%M])"-----
    
    vms=(foo bar)
    
    for i in "${!vms[@]}"; do
      VBoxManage startvm "${vms[i]}" --type gui
    done
    The savestate script.
    Code:
    #!/usr/bin/env bash
    
    exec >> ~/VBoxAutoStart.log 2>&1
    
    printf '%s\n' -----"$(date +%d-%b-%Y-[%H:%M])"-----
    
    vms=(foo bar)
    
    for i in "${!vms[@]}"; do
      printf '%s\n' "Saving machine ${vms[i]}"...
      VBoxManage controlvm "${vms[i]}" savestate
    done
    It is in ~/bin and you can name it to you own hearts content. Make it excutable. Like what i have posted before using the proper kde directories using system-settings.
    Code:
    Systemsettings -> Startup and shutdown -> Autostart -> Scriptfile -> Add script
    You can always add/remove some vms inside the array in between the ( and ) separated by a space. My vms names are foo and bar in this case. Check the logs if you want too
    This is tested on 12.3 and 13.1 kde with VBox version 4.3+, so dunno what is wrong with your setup but It works like a charm on this side .
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  5. #5

    Default Re: Shutting down a VirtualBox VM automatically at log off

    One more thing you can replace the gui with other options such as headless and sdl too...
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  6. #6
    Join Date
    Dec 2008
    Location
    Moscow, Russia
    Posts
    11

    Default Re: Shutting down a VirtualBox VM automatically at log off

    Quote Originally Posted by jetchisel View Post
    Ok, let me give you some holding hands
    Thank you.
    Quote Originally Posted by jetchisel View Post
    The savestate script.
    Code:
    #!/usr/bin/env bash
    
    exec >> ~/VBoxAutoStart.log 2>&1
    
    printf '%s\n' -----"$(date +%d-%b-%Y-[%H:%M])"-----
    
    vms=(foo bar)
    
    for i in "${!vms[@]}"; do
      printf '%s\n' "Saving machine ${vms[i]}"...
      VBoxManage controlvm "${vms[i]}" savestate
    done
    I changed your VM names to mine, placed the script in ~/bin and made a symlink to it from ~/.kde4/shutdown. My VM was running. I chose "Log out" from the host system menu and the same window I have seen previously popped up: http://paste.opensuse.org/35695682 After I pressed OK button in the pop-up window, the VM state was saved, the VM was shut down and I saw a notification from KDE Workspace:

    Logout canceled by '/usr/lib/virtualbox/VirtualBox'

    ~/VBoxAutoStart.log was not created because the mentioned above script had no chance to run.
    Quote Originally Posted by jetchisel View Post
    This is tested on 12.3 and 13.1 kde with VBox version 4.3+, so dunno what is wrong with your setup but It works like a charm on this side .
    This is tested on openSUSE 13.1, linux 3.11.10-11-desktop x86_64, KDE 4.11.5 and virtualbox 4.3.10-114.7.

    So my question is still unanswered: How can I run the script before VirtualBox pops up the message box?

  7. #7

    Default Re: Shutting down a VirtualBox VM automatically at log off

    Quote Originally Posted by mike934 View Post
    Thank you.

    I changed your VM names to mine, placed the script in ~/bin and made a symlink to it from ~/.kde4/shutdown. My VM was running. I chose "Log out" from the host system menu and the same window I have seen previously popped up: http://paste.opensuse.org/35695682 After I pressed OK button in the pop-up window, the VM state was saved, the VM was shut down and I saw a notification from KDE Workspace:

    Logout canceled by '/usr/lib/virtualbox/VirtualBox'

    ~/VBoxAutoStart.log was not created because the mentioned above script had no chance to run.

    This is tested on openSUSE 13.1, linux 3.11.10-11-desktop x86_64, KDE 4.11.5 and virtualbox 4.3.10-114.7.

    So my question is still unanswered: How can I run the script before VirtualBox pops up the message box?
    I have tried to reproduce your issues but so far i can't. Does not seem to appear on this side. By the way if it matters i have the Oracle version of VBox and not from the suse repo, been a while since i have used that package. Also does it has the same issue on any vm not just windows? I have win7 also as a guest and so far no issues like yours.
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  8. #8

    Default Re: Shutting down a VirtualBox VM automatically at log off

    Now to really answer the topic. Shutting down a VirtualBox VM automatically at log off regardless how they are started i.e. at boot, before log-in or manually started.

    The script.

    Code:
    #!/usr/bin/env bash
    
    # ******************************************************************************************** #
    #                                                                                              #
    #  Default Log everything to ~/VBoxAutoSave.log.(Change the value of LogFile to your liking.)  #
    #                                                                                              #
    # ******************************************************************************************** #
    
    LogFile=~/VBoxAutoSave.log
    
    if [[ $LogFile ]]; then
      exec > >(tee -a "$LogFile") 2>&1
    fi
    
    # ******************************************************************************************** #
    #                                                                                              #
    #    The date in between the dashes. (Change the value of date and dash to your liking :-).    #
    #                                                                                              #
    # ******************************************************************************************** #
    
    Dash='---------------------------'
    Date=#$(printf "%(%d-%b-%Y-[%H:%M])T\n" -1)
    
     printf '\n%s\n' "${Dash}${Date}${Dash}"
      
    # ******************************************************************************************** #
    #                                                                                              #
    #  VBoxManage Functions. for more info on "command -p", see "help command" using your shell.   #
    #                                                                                              #
    # ******************************************************************************************** #
    
    ListRunningVms() {
      command -p VBoxManage list runningvms
    }
    
    SaveRunningVms() {
      printf '%s\n' "Saving machine '$RunningVmName' state..."
      command -p VBoxManage controlvm "$RunningUuid" savestate
    }
    
    # ******************************************************************************************** #
    #                                                                                              #
    #    Function to sanitize/extract the vmname and uuid using P.E. inside the stop function.     # 
    #                                                                                              #
    # ******************************************************************************************** #
    
    ExtractVmNameUuid() {
      RunningUuid=${line##*"{"}
      RunningUuid=${RunningUuid%"}"*}
      RunningVmName=${line#*'"'}
      RunningVmName=${RunningVmName%'"'*}
    }
    
    # ******************************************************************************************** #
    #                                                                                              #
    #       Check for runningvms, If there is none found exit immediately without an error.        #
    #                                                                                              #
    # ******************************************************************************************** #
    
    mapfile -u5 -t RunningVms 5< <(ListRunningVms)
    
    (( ${#RunningVms[@]} )) || { 
      echo 'No virtual machine runnning!'
      exit 0  
    }
    
    # ******************************************************************************************** #
    #                                                                                              #
    #   Finally Loop through all the runningvms and save the state. Set the exit status as well.   #
    #                                                                                              #
    # ******************************************************************************************** #
    
    while read -u6 -r line; do 
      ExtractVmNameUuid 
      SaveRunningVms
    done 6< <(ListRunningVms)
    
    exit 0
    
    # ============================================================================================ #
    #                                                                                              #
    #                                   >>> END OF SCRIPT <<<                                      #
    #                                                                                              #
    # ============================================================================================ #
    Just do the same thing with that previous savestate script and symlink to autosave.

    I have it in gist also you can run the following code below as a normal user. You can browse that link before you run curl on it though.
    Code:
    curl -s https://gist.githubusercontent.com/Jetchisel/c339976af08ea4f14b14/raw/f25e3e6cdcb471e2c6ae3f39153dcbc0f5d05dfb/VBoxAutoSave > ~/bin/VBoxAutoSave
    
    chmod +x ~/bin/VBoxAutoSave
    
    ln -sv ~/bin/VBoxAutoSave ~/.kde4/shutdown
    Sorry about your log-out issues if i can't help with that but you can try the Oracle package just to rule out the packaging/package issues though.
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  9. #9
    Join Date
    Dec 2008
    Location
    Moscow, Russia
    Posts
    11

    Default Re: Shutting down a VirtualBox VM automatically at log off

    Quote Originally Posted by jetchisel View Post
    I have tried to reproduce your issues but so far i can't. Does not seem to appear on this side. By the way if it matters i have the Oracle version of VBox and not from the suse repo, been a while since i have used that package.
    I use http://download.opensuse.org/reposit...openSUSE_13.1/ repository. Today VBox was updated there and it has now the same version as on the virtualbox.org, but this has not eliminated the problem.
    Quote Originally Posted by jetchisel View Post
    Also does it has the same issue on any vm not just windows? I have win7 also as a guest and so far no issues like yours.
    I installed openSUSE 12.3 x86 in a new VM and included its name as the first in the VM list of your script.Now when I try to log out from the host I see a similar pop up window, but for this VM.

    I also installed a clean hard drive into my PC, installed to it openSUSE 13.1 x86_64 with the default software (that is I changed nothing during installation), installed all updates, downloaded the virtualbox rpm from
    https://www.virtualbox.org/wiki/Linux_Downloads
    and installed it, built the kernel module and copied my VMs and your script to the disk. I checked that the script was registered. I started two VMs: linux and windows. After I tried to log out from the host, I saw the familiar pop up window.

    And I tested it on a different host with openSUSE 12.3 x86, kernel 3.7.10-1.32-desktop, KDE 4.10.5 and VirtualBox 4.2.6-3.6.11. Alas, the result is the same.

  10. #10

    Default Re: Shutting down a VirtualBox VM automatically at log off

    Perhaps someone can give some feedback about this bug? Feedback folks!!! , I for one do not have this issue. I only see that window when i try to close the main vbox vm by clicking the x button
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •