Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Commit

Permalink
Use short IDs when communicating with Bhyve
Browse files Browse the repository at this point in the history
BHyve has annoyingly small name length limits for virtual machines.
The name limits are not large enough to contain UUIDs in standard
encoding. This switches to using a shortened encoding of UUIDs
(URL-safe Base64) when communicating with bhyve, and adds a vm-id-of
command that can return IDs in long or short form.

Fix: #30
  • Loading branch information
io7m committed Jul 29, 2020
1 parent ca0e9fc commit ca2e95c
Show file tree
Hide file tree
Showing 32 changed files with 480 additions and 70 deletions.
14 changes: 12 additions & 2 deletions README-CHANGES.xml
Expand Up @@ -44,15 +44,25 @@
</c:change>
</c:changes>
</c:release>
<c:release date="2020-07-29T18:45:21+00:00" ticket-system="com.github.io7m.waxmill" version="0.0.5">
<c:release date="2020-07-29T00:00:00+00:00" ticket-system="com.github.io7m.waxmill" version="0.0.5">
<c:changes>
<c:change date="2020-07-29T18:45:21+00:00" summary="Refactor ZFS handling">
<c:change date="2020-07-29T00:00:00+00:00" summary="Refactor ZFS handling">
<c:tickets>
<c:ticket id="29"/>
</c:tickets>
</c:change>
</c:changes>
</c:release>
<c:release date="2020-07-29T20:51:26+00:00" ticket-system="com.github.io7m.waxmill" version="0.0.6">
<c:changes>
<c:change date="2020-07-29T00:00:00+00:00" summary="Use short IDs for compatibility with modern Bhyve">
<c:tickets>
<c:ticket id="30"/>
</c:tickets>
</c:change>
<c:change date="2020-07-29T20:51:26+00:00" summary="Add vm-id-of command"/>
</c:changes>
</c:release>
</c:releases>
<c:ticket-systems>
<c:ticket-system default="true" id="com.github.io7m.waxmill" url="https://www.github.com/io7m/waxmill/issues/"/>
Expand Down
2 changes: 1 addition & 1 deletion com.io7m.waxmill.boot/pom.xml
Expand Up @@ -9,7 +9,7 @@
<parent>
<artifactId>com.io7m.waxmill</artifactId>
<groupId>com.io7m.waxmill</groupId>
<version>0.0.5</version>
<version>0.0.6-SNAPSHOT</version>
</parent>

<artifactId>com.io7m.waxmill.boot</artifactId>
Expand Down
Expand Up @@ -50,6 +50,7 @@
import com.io7m.waxmill.machines.WXMGRUBKernelOpenBSD;
import com.io7m.waxmill.machines.WXMNetworkDeviceBackendType;
import com.io7m.waxmill.machines.WXMOpenOption;
import com.io7m.waxmill.machines.WXMShortIDs;
import com.io7m.waxmill.machines.WXMStorageBackendFile;
import com.io7m.waxmill.machines.WXMTTYBackendFile;
import com.io7m.waxmill.machines.WXMTTYBackendNMDM;
Expand All @@ -60,7 +61,6 @@
import com.io7m.waxmill.machines.WXMZFSFilesystems;
import com.io7m.waxmill.machines.WXMZFSVolumes;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
Expand Down Expand Up @@ -508,7 +508,7 @@ private WXMCommandExecution generateGRUBBhyveCommand()
.addArguments("--root=host")
.addArguments(String.format("--directory=%s", basePath))
.addArguments(String.format("--memory=%sM", memoryMB))
.addArguments(machineId)
.addArguments(WXMShortIDs.encode(this.machine.id()))
.build();
}

Expand Down Expand Up @@ -553,7 +553,7 @@ private WXMCommandExecution generateBhyveCommand(
}
}

commandBuilder.addArguments(this.machine.id().toString());
commandBuilder.addArguments(WXMShortIDs.encode(this.machine.id()));
return commandBuilder.build();
}

Expand Down Expand Up @@ -955,27 +955,23 @@ public WXMEvaluatedBootConfigurationType evaluate()
this.errorNoSuchConfiguration()
));

try {
final var deviceMap =
WXMDeviceMap.create(
this.messages,
this.clientConfiguration,
configuration,
this.machine
);
final var deviceMap =
WXMDeviceMap.create(
this.messages,
this.clientConfiguration,
configuration,
this.machine
);

switch (configuration.kind()) {
case GRUB_BHYVE:
return this.evaluateGRUBConfiguration(
(WXMBootConfigurationGRUBBhyve) configuration, deviceMap
);
case UEFI:
return this.evaluateUEFIConfiguration(
(WXMBootConfigurationUEFI) configuration, deviceMap
);
}
} catch (final IOException e) {
throw new WXMException(e);
switch (configuration.kind()) {
case GRUB_BHYVE:
return this.evaluateGRUBConfiguration(
(WXMBootConfigurationGRUBBhyve) configuration, deviceMap
);
case UEFI:
return this.evaluateUEFIConfiguration(
(WXMBootConfigurationUEFI) configuration, deviceMap
);
}

throw new UnreachableCodeException();
Expand Down Expand Up @@ -1034,7 +1030,6 @@ private WXMEvaluatedBootConfigurationUEFI evaluateUEFIConfiguration(
private WXMEvaluatedBootConfigurationGRUBBhyve evaluateGRUBConfiguration(
final WXMBootConfigurationGRUBBhyve configuration,
final WXMDeviceMap deviceMap)
throws IOException
{
final var kernel =
configuration.kernelInstructions();
Expand Down
2 changes: 1 addition & 1 deletion com.io7m.waxmill.client.api/pom.xml
Expand Up @@ -9,7 +9,7 @@
<parent>
<artifactId>com.io7m.waxmill</artifactId>
<groupId>com.io7m.waxmill</groupId>
<version>0.0.5</version>
<version>0.0.6-SNAPSHOT</version>
</parent>

<artifactId>com.io7m.waxmill.client.api</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion com.io7m.waxmill.client.vanilla/pom.xml
Expand Up @@ -9,7 +9,7 @@
<parent>
<artifactId>com.io7m.waxmill</artifactId>
<groupId>com.io7m.waxmill</groupId>
<version>0.0.5</version>
<version>0.0.6-SNAPSHOT</version>
</parent>

<artifactId>com.io7m.waxmill.client.vanilla</artifactId>
Expand Down
Expand Up @@ -27,6 +27,7 @@
import com.io7m.waxmill.machines.WXMDeviceLPC;
import com.io7m.waxmill.machines.WXMDeviceType;
import com.io7m.waxmill.machines.WXMDryRun;
import com.io7m.waxmill.machines.WXMShortIDs;
import com.io7m.waxmill.machines.WXMTTYBackendFile;
import com.io7m.waxmill.machines.WXMTTYBackends;
import com.io7m.waxmill.machines.WXMVirtualMachine;
Expand Down Expand Up @@ -158,10 +159,13 @@ public void vmKill(
final WXMDryRun dryRun)
throws WXMException
{
final var shortId =
WXMShortIDs.encode(machine.id());

final var processDescription =
WXMProcessDescription.builder()
.setExecutable(this.configuration.bhyveCtlExecutable())
.addArguments(String.format("--vm=%s", machine.id()))
.addArguments(String.format("--vm=%s", shortId))
.addArguments("--destroy")
.build();

Expand Down
2 changes: 1 addition & 1 deletion com.io7m.waxmill.cmdline/pom.xml
Expand Up @@ -9,7 +9,7 @@
<parent>
<artifactId>com.io7m.waxmill</artifactId>
<groupId>com.io7m.waxmill</groupId>
<version>0.0.5</version>
<version>0.0.6-SNAPSHOT</version>
</parent>

<artifactId>com.io7m.waxmill.cmdline</artifactId>
Expand Down
Expand Up @@ -36,6 +36,7 @@
import com.io7m.waxmill.cmdline.internal.WXMCommandVMDeleteBootConfigurations;
import com.io7m.waxmill.cmdline.internal.WXMCommandVMDeleteDevice;
import com.io7m.waxmill.cmdline.internal.WXMCommandVMExport;
import com.io7m.waxmill.cmdline.internal.WXMCommandVMIDOf;
import com.io7m.waxmill.cmdline.internal.WXMCommandVMImport;
import com.io7m.waxmill.cmdline.internal.WXMCommandVMKill;
import com.io7m.waxmill.cmdline.internal.WXMCommandVMList;
Expand Down Expand Up @@ -88,6 +89,7 @@ public Main(
WXMCommandVMDeleteBootConfigurations::new,
WXMCommandVMDeleteDevice::new,
WXMCommandVMExport::new,
WXMCommandVMIDOf::new,
WXMCommandVMImport::new,
WXMCommandVMKill::new,
WXMCommandVMList::new,
Expand Down
@@ -0,0 +1,105 @@
/*
* Copyright © 2020 Mark Raynsford <code@io7m.com> http://io7m.com
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

package com.io7m.waxmill.cmdline.internal;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.io7m.claypot.core.CLPCommandContextType;
import com.io7m.waxmill.machines.WXMMachineName;
import com.io7m.waxmill.machines.WXMShortIDs;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.Path;
import java.util.Objects;

import static com.io7m.claypot.core.CLPCommandType.Status.FAILURE;
import static com.io7m.claypot.core.CLPCommandType.Status.SUCCESS;

@Parameters(commandDescription = "Find the ID of a virtual machine")
public final class WXMCommandVMIDOf extends WXMAbstractCommandWithConfiguration
{
private static final Logger LOG =
LoggerFactory.getLogger(WXMCommandVMIDOf.class);

@Parameter(
names = "--name",
description = "The name of the virtual machine",
converter = WXMMachineNameConverter.class,
required = true
)
private WXMMachineName name;

@Parameter(
names = "--short",
description = "Print the ID as a short ID",
arity = 1,
required = false
)
private boolean shortId;

@Override
public String extendedHelp()
{
return this.messages().format("vmIDOfHelp");
}

/**
* Construct a command.
*
* @param inContext The command context
*/

public WXMCommandVMIDOf(
final CLPCommandContextType inContext)
{
super(LOG, inContext);
}

@Override
public String name()
{
return "vm-id-of";
}

@Override
protected Status executeActualWithConfiguration(
final Path configurationPath)
throws Exception
{
try (var client = WXMServices.clients().open(configurationPath)) {
final var machines = client.vmList();
for (final var machine : machines.machines().values()) {
if (Objects.equals(machine.name(), this.name)) {
final var machineId = machine.id();
if (this.shortId) {
System.out.println(WXMShortIDs.encode(machineId));
} else {
System.out.println(machineId);
}
return SUCCESS;
}
}
}

LOG.error(
"{}",
this.messages().format("errorNoMachinesWithName", this.name.value())
);
return FAILURE;
}
}
Expand Up @@ -273,5 +273,11 @@ a virtual machine that is connected to a VNC server.

<entry key="vmKillHelp"><![CDATA[
The vm-kill command kills any running instance of a given virtual machine.
]]></entry>

<entry key="vmIDOfHelp"><![CDATA[
The vm-id-of command finds the first virtual machine that has a given name
and prints the ID of the machine. If the --short option is used, the command
will print the ID in a shortened form consumable by bhyve.
]]></entry>
</properties>
2 changes: 1 addition & 1 deletion com.io7m.waxmill.database.api/pom.xml
Expand Up @@ -9,7 +9,7 @@
<parent>
<artifactId>com.io7m.waxmill</artifactId>
<groupId>com.io7m.waxmill</groupId>
<version>0.0.5</version>
<version>0.0.6-SNAPSHOT</version>
</parent>

<artifactId>com.io7m.waxmill.database.api</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion com.io7m.waxmill.database.vanilla/pom.xml
Expand Up @@ -9,7 +9,7 @@
<parent>
<artifactId>com.io7m.waxmill</artifactId>
<groupId>com.io7m.waxmill</groupId>
<version>0.0.5</version>
<version>0.0.6-SNAPSHOT</version>
</parent>

<artifactId>com.io7m.waxmill.database.vanilla</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion com.io7m.waxmill.documentation/pom.xml
Expand Up @@ -9,7 +9,7 @@
<parent>
<artifactId>com.io7m.waxmill</artifactId>
<groupId>com.io7m.waxmill</groupId>
<version>0.0.5</version>
<version>0.0.6-SNAPSHOT</version>
</parent>

<artifactId>com.io7m.waxmill.documentation</artifactId>
Expand Down
@@ -0,0 +1,5 @@
$ waxmill vm-id-of --name example
538a90e4-d50d-4511-8643-ae418279bac4

$ waxmill vm-id-of --name example --short
U4qQ5NUNRRGGQ65Bgnm6xA

0 comments on commit ca2e95c

Please sign in to comment.