VirtualBox

Opened 15 years ago

Closed 12 years ago

#3549 closed enhancement (fixed)

VBoxHeadless is not headless on Windows => Fixed in SVN

Reported by: Rehan Khan Owned by:
Component: other Version: VirtualBox 2.1.4
Keywords: vboxheadless service Cc:
Guest type: Linux Host type: other

Description

Hi

I have been trying to run vboxheadless as a windows service (see http://forums.virtualbox.org/viewtopic.php?f=6&t=15450). I have got it working alright but it does not run truely headless as it needs a console for stdout. Starting virtualbox without the ability to open a console (disabling 'allow service to interact with the desktop') makes it error out with a large (possibly meaningless) minus number.

I'm not sure exactly how it works but I read that on windows createprocess() can redirect it's stdout and stderr to files. For example http://support.microsoft.com/kb/190351.

Another option to give flexible log output might be to use logger.dll which can be found here -> http://www.nick.rozanski.org.uk/logger. logger.dll can log to a file, the event log or to stdout/stderr. It seems to also have a linux version but i'm not sure if this helps.

Is it possible to add an option to redirect the stdout/stderr to a file or the event log?

Change History (16)

comment:1 by Frank Mehnert, 15 years ago

Open a headless session with VBoxManage startvm VM_NAME -type vrdp.

comment:2 by Rehan Khan, 15 years ago

The problem with doing it that way is that vboxheadless is spawned by the vboxsvc process (as in is a child of vboxsvc). Both these processes are detached from the service wrapper (running under a svchost process I guess as a COM server) that started them so the service fails to start (not really, as vboxheadless is running, but windows service manager thinks it has failed).

In addition, even starting it using vboxmanage opens a console window as the machine sends some data to stdout (copyright notice and port number). It would be great if vboxheadless was able to be started either without it sending anything to stdout or being able to redirect stdout to a file or nul.

comment:3 by Rehan Khan, 15 years ago

Sorry, the vboxsvc and vboxheadless processes run under a svchost process. The service wrapper and vboxmanage run as child processes of services.exe.

comment:4 by Rehan Khan, 15 years ago

Just to clarify the original request. It is not the output of the virtual machine at question here. It is the output of the vboxheadless executable itself that is the problem.

Thanks

comment:5 by Jessica Hamilton, 14 years ago

I agree. I don't use vbox as a service; just start it up in headless mode when I want the VM running.

Is it possible you could add an option to fork the VM? Say you still print the copyright and port number (if VRDP is enabled; I personally don't use) to stdout as you currently do, then the parent process returns to the console?

Like vboxheadless --startvm "My VM" --vrdp off --fork. It's silly to have to invent VBScripts to do this =/

comment:6 by rossmeissl, 14 years ago

This is a hack, but you can use TrayIt to put the console window in the tray.

comment:7 by Justin J. Hayes, 14 years ago

I'm also wondering if anyone has looked at addressing this recently? I'm using Virtual Box 3.2.8 and the same issue still occurs. I've tried running it inside GNU Screen under Cygwin, but I'm having trouble getting that to work too.

Any ideas?

comment:8 by timob, 14 years ago

I've found a workaround that seems to work. Use the "at" command to schedule a task. So for me this was:

c:> at 19:59 "c:\Program Files\Oracle\VirtualBox\VBoxHeadless.exe" -s mars

It does not show up in GUI as running, but "vboxmanage list vms", lists the vm running.

in reply to:  description comment:9 by KB, 13 years ago

Another workaround, paste in vbs file:

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run """C:\Program Files\Oracle\VirtualBox\VBoxHeadless.exe"" --
  startvm Xubuntu64 --vrdp=off", 0

Note the delicious use of doublequotes...

comment:10 by trakos, 13 years ago

Easy workaround: just change the subsystem byte to 0002, it won't output anything then, but it also won't open cmd.

You can use PE header patcher (http://www.fiberdownload.com/Transfer/PE-Header-Patcher/22856) - copy VBoxHeadless.exe to VBoxReallyheadless.exe, open it with PE header patcher, change the subsystem from windows console to windows GUI and click 'save'. Then use the VBoxReallyheadless.exe instead of VBoxHeadless.exe.

You will have to use the original version of file to see the output again.

PS: Personally, I did it the hard way, without PE header patcher - just using hex editing (change the 324th - i.e. 144 in hex - byte from 0003 to 0002) ;)

comment:11 by Rom Walton, 13 years ago

I work on the BOINC project (http://boinc.berkeley.edu) and we have begun work on integrating VirtualBox into the BOINC Desktop Grid Environment. We ran into this issue with multiple command line applications on Windows. Basically most applications are command line applications are compiled to be a part of the console subsystem on Windows, which automatically launches a conhost.exe process to display a console window regardless if you use it or not.

To work around this issue you just need to tell the linker that you are linking a windows application which uses the windows subsystem instead of the console subsystem.

I'm not quite familiar with the Vbox build system but adding the following flags to g++ during the link operation should do what 'Trakos' suggested with the hex editor modification.

if OS_WIN32_MINGW
vboxheadless_LDFLAGS +=-Wl,--subsystem,windows -mwindows 
endif
if OS_WIN64_MINGW
vboxheadless_LDFLAGS +=-Wl,--subsystem,windows -mwindows 
endif

Now depending on your build environment you may need to add an additional function called WinMain(), if you do need to specify WinMain() here is some quick and dirty code you can use to redirect to main(). Just add it after the main() function definition.

#ifdef RT_OS_WINDOWS

// take a string containing some space separated words.
// return an array of pointers to the null-terminated words.
// Modifies the string arg.
// Returns argc

#define NOT_IN_TOKEN                0
#define IN_SINGLE_QUOTED_TOKEN      1
#define IN_DOUBLE_QUOTED_TOKEN      2
#define IN_UNQUOTED_TOKEN           3

int parse_command_line(char* p, char** argv) {
    int state = NOT_IN_TOKEN;
    int argc=0;

    while (*p) {
        switch(state) {
        case NOT_IN_TOKEN:
            if (isspace(*p)) {
            } else if (*p == '\'') {
                p++;
                argv[argc++] = p;
                state = IN_SINGLE_QUOTED_TOKEN;
                break;
            } else if (*p == '\"') {
                p++;
                argv[argc++] = p;
                state = IN_DOUBLE_QUOTED_TOKEN;
                break;
            } else {
                argv[argc++] = p;
                state = IN_UNQUOTED_TOKEN;
            }
            break;
        case IN_SINGLE_QUOTED_TOKEN:
            if (*p == '\'') {
                *p = 0;
                state = NOT_IN_TOKEN;
            }
            break;
        case IN_DOUBLE_QUOTED_TOKEN:
            if (*p == '\"') {
                *p = 0;
                state = NOT_IN_TOKEN;
            }
            break;
        case IN_UNQUOTED_TOKEN:
            if (isspace(*p)) {
                *p = 0;
                state = NOT_IN_TOKEN;
            }
            break;
        }
        p++;
    }
    argv[argc] = 0;
    return argc;
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR Args, int WinMode) {
    LPSTR command_line;
    char* argv[100];
    int argc;

    command_line = GetCommandLine();
    argc = parse_command_line(command_line, argv);
    return main(argc, argv);
}
#endif

comment:12 by Frank Mehnert, 13 years ago

Resolution: duplicate
Status: newclosed
Summary: vboxheadless is not headless on windowsVBoxHeadless is not headless on Windows => Fixed in SVN

romw, thanks for the suggestion but actually your solution is not what we want to implement. The VBoxHeadless application should actually DO write to the current console if started from cmd.exe but should not open a separate window if started from VBoxSVC. The (hopefully) correct solution is implemented in r38618.

This fix will be included in the next maintenance release.

comment:13 by Frank Mehnert, 13 years ago

Resolution: duplicate
Status: closedreopened

Grr, closed by accident...

comment:14 by Frank Mehnert, 13 years ago

You can download a test build from here.

comment:15 by Rom Walton, 13 years ago

That worked. vboxheadless.exe no longer opens a console window when launched from vboxsvc.exe.

comment:16 by Frank Mehnert, 12 years ago

Resolution: fixed
Status: reopenedclosed

VBox 4.1.4 contains the fix.

Note: See TracTickets for help on using tickets.

© 2023 Oracle
ContactPrivacy policyTerms of Use