[SOLVED] guestcontrol + Perl + shared folder == fail

Discussions about using Windows guests in VirtualBox.
Post Reply
Jurgen1
Posts: 10
Joined: 2. Apr 2014, 10:47

[SOLVED] guestcontrol + Perl + shared folder == fail

Post by Jurgen1 »

Hello forum,

this is a convoluted one so bear with me. I have a perl script that is located on a Windows VirtualBox guest. I want to call this script from the Linux host and have it read a shared folder from the host. The reading of the folder fails.

On the host I call this script and it gives me the following output:
host:~/$ ./script.pl /nfs/nasi/temp
[2014-04-02 10:50:55] Uploading file records to localhost
[2014-04-02 10:50:55] Running VirtualBox for Kaspersky
fatal: opendir(E:\nasi\temp) failed: No such file or directory
[2014-04-02 10:50:56] Uploading malware samples data to localhost
host:$
The script converts the argument /nfs/nasi/temp to E:\nasi\temp and calls the script using the following command:
/usr/bin/VBoxManage guestcontrol <guest> execute --image "C:\strawberry\perl\bin\perl.exe" --username <user> --password <pass> --wait-stdout --wait-stderr 
--wait-exit -- "C:\antivirus\kaspersky.pl" "E:\nasi\temp"
When I run this same script using the same option from the guest directly however I get the following:
C:\antivirus>C:\strawberry\perl\bin\perl.exe C:\antivirus\kaspersky.pl E:\nasi\temp
[2014-04-02 10:54:19] Running Kaspersky Antivirus
[2014-04-02 10:54:20] Parsing Kaspersky report
[2014-04-02 10:54:20] Uploading Kaspersky results to 10.0.0.1
C:\antivirus>
But wait, it gets weirder. When instead of providing the shared directory E:\ I instead point it to C:\ it has no problem reading the directory and just happily keeps going. So the error only shows up when I run the command from the host through VirtualBox and point it to the share.

Here is the relevant code:

Code: Select all

sub createSamplesMap {
	opendir( my $dh, $ARGV[0] ) or die "fatal: opendir($ARGV[0]) failed: $!\n";
	my @files = readdir( $dh );
	foreach my $file ( @files ) {
		if (! -d $file ) {
			...
		}
	}
	closedir($dh);
}
I tried different ways of reading the filenames from the directory but they didn't work. Here's what I tried.

Code: Select all

my @files = <$ARGV[0]\\*>;
my @files = glob( $ARGV[0] . '\\*' );
I don't know whether to blame perl or virtualbox. Anyone have any ideas on what the problem might be?

Windows 7, Ubuntu 12.04.04, VirtualBox 4.2.16r86992, linux perl v5.14.2, Windows Strawberry perl v5.18.2

thnx
Jurgen

crosspost: There is a post at stackoverflow but the forum won't let me add urls: /questions/22807376/virtualbox-perl-shared-folder-fail
Last edited by Jurgen1 on 3. Apr 2014, 22:02, edited 2 times in total.
Jurgen1
Posts: 10
Joined: 2. Apr 2014, 10:47

Re: guestcontrol + Perl + shared folder == fail

Post by Jurgen1 »

Am starting to lean towards VirtualBox being the culprit. This command also fails:

user@host:~/$ /usr/bin/VBoxManage guestcontrol <vm> execute --image "C:\windows\system32\cmd.exe" --username <user> --password <pass> --wait-stdout --wait-stderr --wait-exit -- "/c" "dir E:\nasi\temp"
The system cannot find the path specified.
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: guestcontrol + Perl + shared folder == fail

Post by socratis »

Maybe that the environment of the user does not include the mapping of the shared drive E:\ ?
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
Jurgen1
Posts: 10
Joined: 2. Apr 2014, 10:47

Re: guestcontrol + Perl + shared folder == fail

Post by Jurgen1 »

How do I test that? When I login as any user I can see the share and the files are there. I tried listing shares and creating shares through guestcontrol (and local on the guest) but that didn't work out. I couldn't even see the server with "net view". I'm guessing the virtualbox share doesn't work with that.
Jurgen1
Posts: 10
Joined: 2. Apr 2014, 10:47

Re: guestcontrol + Perl + shared folder == fail

Post by Jurgen1 »

I've found the problem. As mentioned by socratis there was a problem with the environment variables set when running the perl script. After much googling I also found a blog post at http://kissmyarch.blogspot.nl/2012/01/o ... guest.html where kissmyarch describes how he solved the problem.

You can set environment variables using the --environment option in VBoxManage guestcontrol and according to kissmyarch you need to set USERPROFILE to get it to work. This did not work for me.

So instead I used the following code from the script to figure out what environment variables were set:

Code: Select all

    foreach $key (sort keys(%ENV)) {
      print "$key = $ENV{$key}\n";
    }
and ran that both on the guest and from guestcontrol to compare the environments. My command now looks like this:

Code: Select all

/usr/bin/VBoxManage guestcontrol <vm> execute \
    --image "C:\strawberry\perl\bin\perl.exe" \
    --username <user> --password <pass> \
    --environment "USERPROFILE=C:\Users\<user>" \
    --environment "APPDATA=C:\Users\<user>\AppData\Roaming" \
    --environment "LOCALAPPDATA=C:\Users\<user>\AppData\Local" \
    --environment "HOMEDRIVE=C:" \
    --environment "HOMEPATH=\Users\<user>" \
    --environment "LOGONSERVER=\\\<server>" \
    --environment "SESSIONNAME=Console" \
    --environment "TEMP=C:\Users\<user>\AppData\Local\Temp" \
    --environment "TMP=C:\Users\<user>\AppData\Local\Temp" \
    --environment "USERDOMAIN=<domain>" \
    --environment "USERNAME=<user>" \
    --wait-stdout --wait-stderr --wait-exit \
    -- "C:\antivirus\kaspersky.pl" "E:\nasi\temp"
Somewhere in that big pile of environment variables is one that is important.

Thanks too all that helped.
Last edited by Jurgen1 on 3. Apr 2014, 22:00, edited 1 time in total.
socratis
Site Moderator
Posts: 27330
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: guestcontrol + Perl + shared folder == fail

Post by socratis »

Great! Would you mind changing the original post's title to "[SOLVED] ..." so that the rest of the community can benefit as well from your findings? Added bonus if you could experiment a little bit more and see which specific variable (or combination of) makes your script sing...
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
Jurgen1
Posts: 10
Joined: 2. Apr 2014, 10:47

Re: [SOLVED] guestcontrol + Perl + shared folder == fail

Post by Jurgen1 »

fixed the title, the code will have to wait until I get to work on monday.
Jurgen1
Posts: 10
Joined: 2. Apr 2014, 10:47

Re: [SOLVED] guestcontrol + Perl + shared folder == fail

Post by Jurgen1 »

Sorry took a little longer.

USERPROFILE is the only environment variable that is absolutely necessary. Without it it will reliably fail. With it it will mostly work. But on my machine at least I still occasionally get an error. I don't want to hunt around anymore so I'm just leaving all the environment variables there just in case.
Post Reply