1

I created 2 VMs using VirtualBox and I can start them fine as my own user using the following command:

vboxmanage startvm myVM -type headless

but when I try the same command with sudo, or in a systemd service, it returns that it can't find the VM.

I found out this

It fails because you are using sudo. VirtualBox is designed to be run by any user (in the vboxusers group), and sudo runs the command as the root user whose VirtualBox configuration is empty.

But I just don't know how to make it so when run as root (with sudo) to have virtualbox settings present.

Any idea?

  • The end objective here is to auto start the VMs and gracefully terminate them on shutdown. – Martin B May 19 '18 at 14:47
  • Related possible duplicate: https://askubuntu.com/questions/128413/setting-the-path-so-it-applies-to-all-users-including-root-sudo – Elder Geek May 19 '18 at 15:19
  • Does the root user have a Virtualbox configuration? – Elder Geek May 19 '18 at 15:20
  • It's a fresh ubuntu install, so I doubt the root user have a virtualbox configuration. How would I do that? – Martin B May 19 '18 at 15:22
  • The same way you configured it for your user account, but as root. https://www.virtualbox.org/manual/ch01.html might be useful to you. You can clone your existing VM's if so desired. – Elder Geek May 19 '18 at 15:24
  • Thanks, will read that. I really only apt installed virtualbox and created my VMs from there.. I did not manually add any configuration to my user. – Martin B May 19 '18 at 15:26
  • Beside issuing sudo command, I don't even know how to connect to my root user. su command fail with my password – Martin B May 19 '18 at 15:26
  • sudo -i will give you an all powerful root access login. Be careful as it's possible to do a great deal of damage. You should backup first. – Elder Geek May 19 '18 at 15:29
  • If your end result should be systemd managing those VMs, have you tried to put your username in the systmed units as executing user? There are also systemd user units, which are run under your user. Maybe this will help you, too. I once did something similar using systemd user mode and vagrant. – Lienhart Woitok May 19 '18 at 16:34

1 Answers1

0

To start a VM on login and have it stop on logout, you can use systemd user units.

Create a file `~/.config/systemd/user/myvm.service with the following content:

[Unit]
Description=My VM

[Service]
ExecStart=vboxmanage startvm myVM -type headless
ExecStop=vboxmanage controlvm myVM acpipowerbutton 
# Actually, I'm not sure about the stop command, but I'm confident you will figure that one out
RemainAfterExit=true

[Install]
WantedBy=default.target

Afterwards, run systemctl --user enable myvm.service. You can now start your VM with systemctl --user start myvm.service and stop it with systemctl --user stop myvm.service. Logging in and out should start and stop your VM.

If you need this on boot instead of log in, you can try to make this a system systemd unit and specify the executing user in the [Service] section with User= and Group=.

  • Thanks! I had tried setting my username in the services' unit, without luck.

    Adding the RemainAfterExit saved the day! Thanks a LOT!

    – Martin B May 23 '18 at 02:10