{"id":804,"date":"2019-05-20T01:42:37","date_gmt":"2019-05-20T07:42:37","guid":{"rendered":"https:\/\/mycomputerninja.com\/?p=804"},"modified":"2019-05-20T01:42:37","modified_gmt":"2019-05-20T07:42:37","slug":"how-to-start-a-virtualbox-vm-as-a-service-using-systemd","status":"publish","type":"post","link":"https:\/\/mycomputerninja.com\/?p=804","title":{"rendered":"How to start a Virtualbox VM as a service using systemd"},"content":{"rendered":"\n<p>First, you need to start the vm, headless, from the command line. To do this, drop into a terminal and run: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vboxmanage list vms<\/code><\/pre>\n\n\n\n<p>This should give you a list of VM&#8217;s available to your user. In my case, it gave me: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>jon@mycomputerninja:~ $ vboxmanage list vms\n\"your vm name\" {1f134d1e-ba34-432c-b6e4-fc01f1ad250f}<\/code><\/pre>\n\n\n\n<p>Then, you can run: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>vboxmanage startvm &lt;your vm name here> --type headless<\/code><\/pre>\n\n\n\n<p>Then, do: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ps aux | grep &lt;vmname><\/code><\/pre>\n\n\n\n<p>To see what command was actually run. This should show some output like: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>jon@mycomputerninja:\/etc\/systemd\/system $ ps aux| grep Zone\njon       4560 63.9 17.4 3214804 703504 ?      Sl   01:18   0:58 \/usr\/lib\/virtualbox\/VBoxHeadless --comment Zoneminder --startvm 1f134d1e-ba34-432c-b6e4-fc01f1ad250f --vrde config<\/code><\/pre>\n\n\n\n<p>Now, armed with the command that was actually run, highlighted above, you can build a systemd init file. An example init file is below. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>[Unit]\nDescription=Zoneminder VM startup\nWantedBy=multi-user.target\n\n[Service]\nType=simple\nExecStart=\/usr\/lib\/virtualbox\/VBoxHeadless --comment Zoneminder --startvm 1f134d1e-ba34-432c-b6e4-fc01f1ad250f --vrde config\nUser=jon\nRestart=always<\/code><\/pre>\n\n\n\n<p>That file should get dropped into \/etc\/systemd\/system\/ and named something like &lt;myservice>.service. In my case, since I was running a zoneminder service, I named it zoneminderVM.service. You also note I run it as my user, as it won&#8217;t work as the root user, because the vm context for root is different. I also run it in the multi-user target in wantedby so that the system is mostly up before this service is attempted to run. <\/p>\n\n\n\n<p>Next, you need to run the relevant systemd commands to run this new service. so do the following: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl daemon-reload\nsudo systemctl start &lt;your service file name>.service \nsudo systemctl status &lt;your service file name>.service <\/code><\/pre>\n\n\n\n<p>If all goes well, your VM should start up as a service under your username, and restart any time the machine restarts. <\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><!--adsense--><\/h2>\n","protected":false},"excerpt":{"rendered":"<p>First, you need to start the vm, headless, from the command line. To do this, drop into a terminal and run: This should give you a list of VM&#8217;s available to your user. In my case, it gave me: Then, you can run: Then, do: To see what command was actually run. This should show some output like: Now, armed with the command that was actually run, highlighted above, you can build a systemd init file. An example init file&#8230;<\/p>\n<p class=\"read-more\"><a class=\"btn btn-default\" href=\"https:\/\/mycomputerninja.com\/?p=804\"> Read More<span class=\"screen-reader-text\">  Read More<\/span><\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[499,500,498,494],"class_list":["post-804","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-service","tag-systemd","tag-virtualbox","tag-zoneminder"],"_links":{"self":[{"href":"https:\/\/mycomputerninja.com\/index.php?rest_route=\/wp\/v2\/posts\/804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mycomputerninja.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mycomputerninja.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mycomputerninja.com\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/mycomputerninja.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=804"}],"version-history":[{"count":2,"href":"https:\/\/mycomputerninja.com\/index.php?rest_route=\/wp\/v2\/posts\/804\/revisions"}],"predecessor-version":[{"id":806,"href":"https:\/\/mycomputerninja.com\/index.php?rest_route=\/wp\/v2\/posts\/804\/revisions\/806"}],"wp:attachment":[{"href":"https:\/\/mycomputerninja.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=804"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mycomputerninja.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=804"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mycomputerninja.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}