VirtualBox Sdk Java - 从主机 OS 在来宾 OS 中执行命令

VirtualBox Sdk Java - Executing a command inside guest OS from host OS

我正在尝试从主机 OS(Ubuntu 14.04).

在来宾 OS(Ubuntu 服务器) 中创建一个用户

这是我的代码

IGuestProcess process = null;

ISession session = manager.getSessionObject(); //VirtualBoxManager

machine.lockMachine(session, LockType.Shared); //IMachine

IConsole console = session.getConsole();

IGuest guest = console.getGuest();

IGuestSession guestSession = guest.createSession("registerdUserWithRights", "hisPasswd", "", "");

Long time = 100000L;

GuestSessionWaitResult result = guestSession.waitFor(time, time);       

if(result == GuestSessionWaitResult.Start)
    process = guestSession.processCreate("adduser --disabled-password --gecos \"\" username", null, null, null, 0L);

ProcessWaitResult waitResult = process.waitFor(1L, time);

if(waitResult == ProcessWaitResult.Start)
    System.out.println("started");

然而,这在 ProcessWaitResult waitResult = process.waitFor(1L, time); 处失败了。我不断收到

VBox error: VirtualBox error: rc=0x80bb0005 The specified file was not found on guest (0x80bb0005)

我哪里错了?

我解决了。诀窍是发出命令的整个路径。我使用 which <command> 得到了它。此外,我还必须在单独的数组列表中传递命令的参数。

IGuestProcess process = null;

ISession session = manager.getSessionObject(); //VirtualBoxManager

machine.lockMachine(session, LockType.Shared); //IMachine

IConsole console = session.getConsole();

IGuest guest = console.getGuest();

IGuestSession guestSession = guest.createSession("registerdUserWithRights", "hisPasswd", "", "");

Long time = 100000L;

GuestSessionWaitResult result = guestSession.waitFor(time, time);       

List<String> argumentsForProcess = Arrays.asList("--disabled-password", "--gecos", "'" + name + "'", username);

if(result == GuestSessionWaitResult.Start)
    createUser = guestSession.processCreate("/usr/sbin/adduser", argumentsForProcess, null, null, 0L);


ProcessWaitResult waitResult = process.waitFor(1L, time);

if(waitResult == ProcessWaitResult.Start)
    System.out.println("started");
guestSession.close();
session.unlockMachine();

我将可执行路径设置为空,并将可执行路径放入arguments[0]

  List<String> argumentsForProcess = Arrays.asList( "/usr/bin/java", "-jar", "/home/webserver/vm60-docker/webserver/service-1.0.0.jar");

  process = guestSession.processCreate(null, argumentsForProcess, null, null, 0L);