Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,12 @@ sudo launchctl bootout system /Library/LaunchDaemons/io.github.lima-vm.socket_vm
sudo rm /Library/LaunchDaemons/io.github.lima-vm.socket_vmnet.bridged.${BRIDGED}.plist
```

### No DHCP mode

By passing `--vmnet-mode=host` without `--vmnet-gateway` to `socket_vmnet`, the internal DHCP will be disabled.
This is useful for situations where the users want to create a network with statically assigned IP addresses or where another VMs is running its own DHCP server.
This mode does not come with a launchd service.

## FAQs

### Why does `socket_vmnet` require root?
Expand Down Expand Up @@ -430,6 +436,15 @@ sudo /usr/libexec/ApplicationFirewall/socketfilterfw --add /usr/libexec/bootpd
/usr/libexec/ApplicationFirewall/socketfilterfw --unblock /usr/libexec/bootpd
```

### How to setup a vmnet host network without DHCP

You may need to disable the vmnet framework's DHCP to:
- Create a host network where all VMs have static IPs.
- Run a custom DHCP server on one VM to assign IPs to others on the same network.

To disable the internal DHCP, use `--vmnet-mode=host` without `--vmnet-gateway`. This allows you to use static IP addresses for your VMs.
You can optionally provide a `--vmnet-network-uuid`.

## Links

- https://developer.apple.com/documentation/vmnet
Expand Down
17 changes: 16 additions & 1 deletion cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ static void print_usage(const char *argv0) {
"specified\n");
printf("--vmnet-interface-id=UUID vmnet interface ID (default: "
"random)\n");
printf("--vmnet-network-identifier=UUID vmnet network identifier (UUID string, \"random\", or \"\")\n"
" When vmnet mode is \"host\" and --vmnet-gateway is not set, the internal DHCP will be disabled.\n"
" (default: \"random\")\n");
printf("--vmnet-nat66-prefix=PREFIX:: The IPv6 prefix to use with "
"shared mode.\n");
printf(" The prefix must be a ULA i.e. "
Expand All @@ -72,6 +75,7 @@ enum {
CLI_OPT_VMNET_MASK,
CLI_OPT_VMNET_INTERFACE_ID,
CLI_OPT_VMNET_NAT66_PREFIX,
CLI_OPT_VMNET_NETWORK_IDENTIFIER,
};

struct cli_options *cli_options_parse(int argc, char *argv[]) {
Expand All @@ -90,6 +94,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
{"vmnet-mask", required_argument, NULL, CLI_OPT_VMNET_MASK },
{"vmnet-interface-id", required_argument, NULL, CLI_OPT_VMNET_INTERFACE_ID},
{"vmnet-nat66-prefix", required_argument, NULL, CLI_OPT_VMNET_NAT66_PREFIX},
{"vmnet-network-identifier", required_argument, NULL, CLI_OPT_VMNET_NETWORK_IDENTIFIER},
{"pidfile", required_argument, NULL, 'p' },
{"help", no_argument, NULL, 'h' },
{"version", no_argument, NULL, 'v' },
Expand Down Expand Up @@ -134,6 +139,16 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
case CLI_OPT_VMNET_NAT66_PREFIX:
res->vmnet_nat66_prefix = strdup(optarg);
break;
case CLI_OPT_VMNET_NETWORK_IDENTIFIER:
if (strcmp(optarg, "random") == 0) {
uuid_generate_random(res->vmnet_network_identifier);
} else if (strcmp(optarg, "") == 0) {
uuid_clear(res->vmnet_network_identifier);
} else if (uuid_parse(optarg, res->vmnet_network_identifier) < 0) {
ERRORF("Failed to parse UUID \"%s\"", optarg);
goto error;
}
break;
case 'p':
res->pidfile = strdup(optarg);
break;
Expand Down Expand Up @@ -191,7 +206,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
goto error;
}
if (res->vmnet_gateway == NULL) {
if (res->vmnet_mode != VMNET_BRIDGED_MODE) {
if (res->vmnet_mode != VMNET_BRIDGED_MODE && res->vmnet_mode != VMNET_HOST_MODE) {
WARN("--vmnet-gateway=IP should be explicitly specified to "
"avoid conflicting with other applications");
}
Expand Down
2 changes: 2 additions & 0 deletions cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct cli_options {
char *vmnet_mask;
// --vmnet-interface-id, corresponds to vmnet_interface_id_key
uuid_t vmnet_interface_id;
// --vmnet-network-identifier, corresponds to vmnet_network_identifier_key
uuid_t vmnet_network_identifier;
// --vmnet-nat66-prefix, corresponds to vmnet_nat66_prefix_key
char *vmnet_nat66_prefix;
// -p, --pidfile; writes pidfile using permissions of socket_vmnet
Expand Down
19 changes: 15 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,21 @@ static interface_ref start(struct state *state, struct cli_options *cliopt) {
INFOF("Using network interface \"%s\"", cliopt->vmnet_interface);
xpc_dictionary_set_string(dict, vmnet_shared_interface_name_key, cliopt->vmnet_interface);
}
if (cliopt->vmnet_gateway != NULL) {
xpc_dictionary_set_string(dict, vmnet_start_address_key, cliopt->vmnet_gateway);
xpc_dictionary_set_string(dict, vmnet_end_address_key, cliopt->vmnet_dhcp_end);
xpc_dictionary_set_string(dict, vmnet_subnet_mask_key, cliopt->vmnet_mask);

// no dhcp
if (cliopt->vmnet_mode == VMNET_HOST_MODE && cliopt->vmnet_gateway == NULL) {
if (!uuid_is_null(cliopt->vmnet_network_identifier)) {
xpc_dictionary_set_uuid(dict, vmnet_network_identifier_key, cliopt->vmnet_network_identifier);
uuid_string_t uuid_str;
uuid_unparse(cliopt->vmnet_network_identifier, uuid_str);
INFOF("Using network identifier \"%s\" and no vmnet gateway -> NO DHCP will be enabled on this vmnet", uuid_str);
}
} else {
if (cliopt->vmnet_gateway != NULL) {
xpc_dictionary_set_string(dict, vmnet_start_address_key, cliopt->vmnet_gateway);
xpc_dictionary_set_string(dict, vmnet_end_address_key, cliopt->vmnet_dhcp_end);
xpc_dictionary_set_string(dict, vmnet_subnet_mask_key, cliopt->vmnet_mask);
}
}

xpc_dictionary_set_uuid(dict, vmnet_interface_id_key, cliopt->vmnet_interface_id);
Expand Down