1

It seems not possible on Ubuntu autoinstallations (14.04.3 and 15.04) to configure the right network interface via preseed.

Ubuntu sometimes uses eth0, and sometimes interfaces like p2p1, or p1p3.

I know how these names will be generated. But how can I detect these names during or before the autoinstall? Or how can I automatically rename the p2p1 interface name to the old eth0 name?

To set the static interface settings I use d-i preseed/late_command to overwrite the /etc/network/interfaces file. Right know I assume there is only one interface with the name eth0.

I tried to overwrite/delete the /etc/udev/rules.d/70-persistent-net.rules within the preseed/late_command. But this does not help in any way - it will be ignored.

How can I change the interface name to eth0? Or how can I detect the interface name during the autoinstallation, to write the correct name into the network/interfaces file?

Edit:

The replacement now works great (thanks to "muru"!)

The Interface will initial written from another server and served internal via http wget in the preseed/late_command:

wget http://{InternalServer}/{path}/interfaces -O /etc/network/interfaces;

in the same line i load an additional replaceinterface.sh script with the content:

#!/bin/bash
sDevice=`ip -o link | grep 'link/ether' | grep -oE "^[[:digit:]]:[[:space:]]([[:alnum:]]+)" | cut -d" " -f 2`
sed "s/eth0/$sDevice/g" $1

After loading ,i fire chmod +x, execute it with sh /tmp/replaceinterface.sh < /etc/network/interfaces > /etc/network/interfaces;

But then, after installation is done, the /etc/network/interfaces file is empty (?) ...

If i do the same steps in another console manualy, during installation, i see the correct output / correct modified file

But that seems to be another question for askubuntu.com ..

Edit²:

The Replacement works perfect now - thanks again to "muru" !

#!/bin/bash
sDevice=`ip -o link | grep 'link/ether' | grep -oE "^[[:digit:]]:[[:space:]]([[:alnum:]]+)" | cut -d" " -f 2`
sReplaced=`sed s/eth0/$sDevice/g $1`
echo "$sReplaced" > $1
karel
  • 110,292
  • 102
  • 269
  • 299
eXe
  • 111
  • 5
  • On identical hardware, Ubuntu *should* consistently use `pXpY` or `ethX`. I suppose your hardware changes? – muru Nov 26 '15 at 10:40
  • we use autoinstallation via preseed on several different hardwares. we do not track down the problem to the used motherboard - if we would, we would have to test every new motherboard, which would a pain - only becouse of ubuntu (right now). – eXe Nov 26 '15 at 10:43
  • Hmm. You could use the output of `ip -o link` (for example, from `ip -o link | awk -F: '/ether/{print $2; exit}'`). – muru Nov 26 '15 at 10:50
  • You cannot redirect to and from the same file for the same command - the `>` redirection will overwrite the file. Use `sed -i "s/eth0/$sDevice/g" /etc/network/interfaces` instead. – muru Nov 27 '15 at 09:09

1 Answers1

0

Network interfaces are not available even during 'late-command'. They are available on the first boot after having done an automated install.

This is why 'ip -o link' will be empty if run during a preseed late-command.

One way to do this is to create a simple systemd unit that runs after the network is ready, then have that adjust interface names and/or netplan configuration.

Example basic service that could be dropped into '/etc/systemd/system':

[Unit]
Description=Run Once
After=network-online.target
Requires=network-online.target

[Service]
Type=simple
EnvironmentFile=
ExecStart=/root/runonce.pl

[Install]
WantedBy=multi-user.target

In this example I setup a script to run things a single time, to make an easy method for running one or many scripts that you want to run once right after install. The corresponding runonce.pl:

#!/usr/bin/perl -w
use strict;

my $folder = "/root/runonce/scripts";
my $donef = "/root/runonce/done";
if( ! -e $donef ) {
    mkdir $donef;
}
my $outputf = "/root/runonce/output";
if( ! -e $outputf ) {
    mkdir $outputf;
}

opendir( my $dh, $folder );
my @files = readdir( $dh );
closedir( $dh );

for my $file ( @files ) {
    next if( $file =~ m/^\.+$/ );
    my $full = "$folder/$file";
    my $done = "$donef/$file";
    my $output = "$outputf/$file";
    my $output2 = "$outputf/$file-error";
    if( -e $done ) {
        unlink $done;
    }
    chmod "0700", $full;
    `$full > $output 2> $output2`;
    rename $full, $done;
}

In order to set everything up from preseed, contents for preseed file:

d-i preseed/late_command string \
    in-target wget -P /etc/systemd/system/ http://10.0.2.2:8001/kickstart/runonce.service; \
    in-target systemctl enable runonce; \
    in-target mkdir /root/runonce/scripts; \
    in-target wget -P /root/runonce/ http://10.0.2.2:8001/kickstart/runonce.pl; \
    in-target chmod +x /root/runonce/runonce.pl; \
    in-target wget -P /root/runonce/scripts/ http://10.0.2.2:8001/kickstart/fixnet.pl

You'll need to adjust this to have a location to fetch the files from. In this example it is querying the gateway IP address of Virtualbox, which in turn then hits a server running on my local desktop.

Example "fixnet.pl" script that is being run after initial install:

#!/usr/bin/perl -w
use strict;

my @netlines = `ip -o link`;

my $npFile = "/etc/netplan/vbox.yaml";
open( my $npFh, ">$npFile" ) or die "Cannot write to $npFile";

print $npFh "network:\n  version: 2\n  renderer: networkd\n  ethernets:\n";

for my $netline ( @netlines ) {
    if( $netline =~ m/(enp[0-9a-z]+)/ ) {
        my $interface = $1;
        print STDERR "Adding interface $interface to netplan\n";
        print $npFh "    $interface:\n";
        print $npFh "      dhcp4: yes\n";
    }
}

close $npFh;
`netplan apply`;
exit 0;