Unverified Commit a3264bdd authored by Gergő Gutyina's avatar Gergő Gutyina Committed by GitHub
Browse files

nixos-container: add --use-host-network option (#447155)

parents 75cab0eb 95455952
Loading
Loading
Loading
Loading
+42 −26
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ Usage: nixos-container list
         [--port <port>]
         [--host-address <string>]
         [--local-address <string>]
         [--use-host-network]
       nixos-container destroy <container-name>
       nixos-container restart <container-name>
       nixos-container start <container-name>
@@ -74,6 +75,7 @@ my $signal;
my $configFile;
my $hostAddress;
my $localAddress;
my $useHostNetwork = 0;
my $flake;
my $flakeAttr = "container";

@@ -106,6 +108,7 @@ GetOptions(
    "config-file=s" => \$configFile,
    "host-address=s" => \$hostAddress,
    "local-address=s" => \$localAddress,
    "use-host-network" => \$useHostNetwork,
    "flake=s" => \$flake,
    # Nix passthru options.
    "log-format=s" => \&copyNixFlags1,
@@ -127,6 +130,10 @@ if (defined $hostAddress and !defined $localAddress or defined $localAddress and
    die "With --host-address set, --local-address is required as well!";
}

if ($useHostNetwork && (defined $hostAddress || defined $localAddress)) {
    die "--use-host-network cannot be used with --host-address or --local-address!";
}

my $action = $ARGV[0] or die "$0: no action specified\n";

if (defined $configFile and defined $extraConfig) {
@@ -231,6 +238,12 @@ if ($action eq "create") {
    # be restricted too.
    die "$0: container name ‘$containerName’ is too long\n" if length $containerName > 11;

    my @conf;

    if ($useHostNetwork) {
        push @conf, "PRIVATE_NETWORK=0\n";
        print STDERR "using host network\n";
    } else {
        # Get an unused IP address.
        my %usedIPs;
        foreach my $confFile2 (glob "$configurationDirectory/*.conf") {
@@ -257,10 +270,12 @@ if ($action eq "create") {
            die "$0: out of IP addresses\n" unless defined $ipPrefix;
        }

    my @conf;
        push @conf, "PRIVATE_NETWORK=1\n";
        push @conf, "HOST_ADDRESS=$hostAddress\n";
        push @conf, "LOCAL_ADDRESS=$localAddress\n";
        print STDERR "host IP is $hostAddress, container IP is $localAddress\n";
    }

    push @conf, "HOST_BRIDGE=$bridge\n";
    push @conf, "HOST_PORT=$port\n";
    push @conf, "AUTO_START=$autoStart\n";
@@ -269,8 +284,6 @@ if ($action eq "create") {

    close($lock);

    print STDERR "host IP is $hostAddress, container IP is $localAddress\n";

    # The per-container directory is restricted to prevent users on
    # the host from messing with guest users who happen to have the
    # same uid.
@@ -517,6 +530,9 @@ elsif ($action eq "run") {

elsif ($action eq "show-ip") {
    my $s = read_file($confFile) or die;
    if ($s =~ /^PRIVATE_NETWORK=0$/m) {
        die "$0: container uses host network, no separate IP address\n";
    }
    $s =~ /^LOCAL_ADDRESS=([0-9\.]+)(\/[0-9]+)?$/m
        or $s =~ /^LOCAL_ADDRESS6=([0-9a-f:]+)(\/[0-9]+)?$/m
        or die "$0: cannot get IP address\n";