The issue with creating machine groups and moving them.

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
WhitePhoenix
Posts: 1
Joined: 2. Dec 2024, 22:08

The issue with creating machine groups and moving them.

Post by WhitePhoenix »

Good day,

I have a large number of virtual machines, and during the automation process, hundreds of clones are created.

The problem is that navigating the program becomes nearly impossible. I would like to either move the clones into separate groups or create them directly in such groups, which can then be kept closed. This way, navigation would become much more convenient.

The issue is that I can’t figure out how to:
1. Create a new group.
2. Move an existing machine into a group.

Looking through the documentation, I found that we can specify a group when creating a new machine, but I couldn’t figure out how to do this as there are no examples provided.

If anyone has information or a code example for creating and moving virtual machines into groups, I would greatly appreciate it.
klaus
Oracle Corporation
Posts: 1562
Joined: 10. May 2007, 14:57

Re: The issue with creating machine groups and moving them.

Post by klaus »

Groups don't have to be created. They exist as soon as you put a VM into one. A group name can be hierarchical (containing slashes), and must start with a '/'. Oh, and one VM can be in multiple groups (comma separated). By default VMs go into the group called "/" (or as a shortcut you can provide the empty string). Just as a reminder.

VBoxManage createvm and VBoxManage clonevm both have a --groups option. Likewise VBoxManage modifyvm which is what you need to apply such groups for existing VMs. Pretty straightforward I think and is documented reasonably well - try running "VBoxManage help createvm" and check the details for the --groups option.
noteirak
Site Moderator
Posts: 5232
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7

Re: The issue with creating machine groups and moving them.

Post by noteirak »

Hello!
Please give the information asked in Posting Guidelines, except for the code bit, else can't really help you.
WhitePhoenix
Posts: 1
Joined: 2. Dec 2024, 22:08

Re: The issue with creating machine groups and moving them.

Post by WhitePhoenix »

Virtualbox version you are connecting to : 7.1.4
Virtualbox API version you are using : 7.1.4
Language used : Delphi / C++
OS under which your program should run: Windows server 2019 x64

I’ve been struggling for three days without any results.
All I managed to achieve was moving the machine to the root group, but I can’t move it to the group I need.
If you have code in any programming language, please provide an example.

Code: Select all

function AddVMToGroup(MachineNameOrId: string; GroupName: string; var ErrorStr: string): boolean;
var
  Machine: IMachine;
  Session: ISession;
  CurrentGroups, NewGroups: PSafeArray;
  Bound: SAFEARRAYBOUND;
  TempStr: WideString;
  pStr: BSTR;
  Index: LONG;
begin
  Result := False;
  ErrorStr := '';

  Log(etInfoMessage, 'AddVMToGroup', Format('Starting for VM: %s, Group: %s', [MachineNameOrId, GroupName]));

  try
    if not FoundVirtualsSession(MachineNameOrId, Machine, Session, ErrorStr) then
    begin
      ErrorStr := 'Failed to find VM: ' + ErrorStr;
      Log(etError, 'AddVMToGroup', ErrorStr);
      Exit;
    end;

    Machine.LockMachine(Session, LockType_Write);
    try
      Machine := Session.Machine;

      CurrentGroups := Machine.Groups;

      Bound.lLbound := 0;
      Bound.cElements := 1;
      NewGroups := SafeArrayCreateVector(VT_BSTR, 0, Bound.cElements);

      try
        if Copy(GroupName, 1, 1) <> '/' then
          TempStr := '/' + GroupName
        else
          TempStr := GroupName;

        Index := 0;
        pStr := SysAllocString(PWideChar(TempStr));
        try
          hr := SafeArrayPutElement(NewGroups, Index, pStr);
        finally
          SysFreeString(pStr);
        end;

        Machine.Groups := NewGroups;
        Machine.SaveSettings;
        Result := True;
        Log(etInfoMessage, 'AddVMToGroup', Format('Successfully added VM: %s to group: %s', [MachineNameOrId, GroupName]));
      finally
        SafeArrayDestroy(NewGroups);
      end;

    finally
      Machine.UnlockMachine(Session);
      Log(etInfoMessage, 'AddVMToGroup', 'Machine unlocked successfully.');
    end;

  except
    on E: Exception do
    begin
      ErrorStr := Format('Error: %s', [E.Message]);
      Log(etError, 'AddVMToGroup', ErrorStr);
    end;
  end;
end;
The specified machine is moved not to a group but to the root /.
Last edited by WhitePhoenix on 3. Dec 2024, 22:16, edited 1 time in total.
Post Reply