]> git.proxmox.com Git - pve-common.git/blobdiff - data/PVE/INotify.pm
try to keep ordering in /etc/network/interfaces
[pve-common.git] / data / PVE / INotify.pm
index abc8aeb6777ebaa97be3239c40dd00425e3a5594..12dc11b6ac0ed75d12514b871844fc905703bc73 100644 (file)
@@ -692,6 +692,9 @@ sub read_etc_network_interfaces {
        close($fd2);
     }
 
+    # we try to keep order inside the file
+    my $priority = 2; # 1 is reserved for lo 
+
     # always add the vmbr0 bridge device
     $ifaces->{vmbr0}->{exists} = 1;
 
@@ -720,46 +723,54 @@ sub read_etc_network_interfaces {
        } elsif ($line =~ m/^iface\s+(\S+)\s+inet\s+(\S+)\s*$/) {
            my $i = $1;
            $ifaces->{$i}->{method} = $2;
+           $ifaces->{$i}->{priority} = $priority++;
 
            my $d = $ifaces->{$i};
-           while (defined ($line = <$fh>) && ($line =~ m/^\s+((\S+)\s+(.+))$/)) {
-               my $option = $1;
-               my ($id, $value) = ($2, $3);
-               if (($id eq 'address') || ($id eq 'netmask') || ($id eq 'broadcast')) {
-                   $d->{$id} = $value;
-               } elsif ($id eq 'gateway') {
-                   $d->{$id} = $value;
-                   $gateway = 1;
-               } elsif ($id eq 'slaves' || $id eq 'bridge_ports') {
-                   my $devs = {};
-                   foreach my $p (split (/\s+/, $value)) {
-                       next if $p eq 'none';
-                       $devs->{$p} = 1;
-                   }
-                   my $str = join (' ', sort keys %{$devs});
-                   $d->{$id} = $str || '';
-               } elsif ($id eq 'bridge_stp') {
-                   if ($value =~ m/^\s*(on|yes)\s*$/i) {
-                       $d->{$id} = 'on';
-                   } else {
-                       $d->{$id} = 'off';
-                   }
-               } elsif ($id eq 'bridge_fd') {
-                   $d->{$id} = $value;
-               } elsif ($id eq 'bond_miimon') {
-                   $d->{$id} = $value;
-               } elsif ($id eq 'bond_mode') {
-                   # always use names
-                   foreach my $bm (keys %$bond_modes) {
-                       my $id = $bond_modes->{$bm};
-                       if ($id eq $value) {
-                           $value = $bm;
-                           last;
+           while (defined ($line = <$fh>)) {
+               if ($line =~ m/^#(.*)\s*$/) {
+                   $d->{comment} = '' if !$d->{comment};
+                   $d->{comment} .= PVE::Tools::decode_text($1) . "\n";
+               } elsif ($line =~ m/^\s+((\S+)\s+(.+))$/) {
+                   my $option = $1;
+                   my ($id, $value) = ($2, $3);
+                   if (($id eq 'address') || ($id eq 'netmask') || ($id eq 'broadcast')) {
+                       $d->{$id} = $value;
+                   } elsif ($id eq 'gateway') {
+                       $d->{$id} = $value;
+                       $gateway = 1;
+                   } elsif ($id eq 'slaves' || $id eq 'bridge_ports') {
+                       my $devs = {};
+                       foreach my $p (split (/\s+/, $value)) {
+                           next if $p eq 'none';
+                           $devs->{$p} = 1;
                        }
+                       my $str = join (' ', sort keys %{$devs});
+                       $d->{$id} = $str || '';
+                   } elsif ($id eq 'bridge_stp') {
+                       if ($value =~ m/^\s*(on|yes)\s*$/i) {
+                           $d->{$id} = 'on';
+                       } else {
+                           $d->{$id} = 'off';
+                       }
+                   } elsif ($id eq 'bridge_fd') {
+                       $d->{$id} = $value;
+                   } elsif ($id eq 'bond_miimon') {
+                       $d->{$id} = $value;
+                   } elsif ($id eq 'bond_mode') {
+                       # always use names
+                       foreach my $bm (keys %$bond_modes) {
+                           my $id = $bond_modes->{$bm};
+                           if ($id eq $value) {
+                               $value = $bm;
+                               last;
+                           }
+                       }
+                       $d->{$id} = $value;
+                   } else {
+                       push @{$d->{options}}, $option;
                    }
-                   $d->{$id} = $value;
                } else {
-                   push @{$d->{options}}, $option;
+                   last;
                }
            }
        }
@@ -771,6 +782,7 @@ sub read_etc_network_interfaces {
     }
 
     if (!$ifaces->{lo}) {
+       $ifaces->{lo}->{priority} = 1;
        $ifaces->{lo}->{method} = 'loopback';
        $ifaces->{lo}->{type} = 'loopback';
        $ifaces->{lo}->{autostart} = 1;
@@ -864,6 +876,12 @@ sub __interface_to_string {
        $raw .= "\t$option\n";
     }
 
+    # add comments 
+    my $comment = $d->{comment} || '';
+    foreach my $cl (split(/\n/, $comment)) {
+       $raw .= '#' .  PVE::Tools::encode_text($cl) . "\n";
+    }
+
     $raw .= "\n";
 
     return $raw;
@@ -876,16 +894,24 @@ sub write_etc_network_interfaces {
 
     my $printed = {};
 
-    foreach my $t (('lo', 'eth', '')) {
-       foreach my $iface (sort keys %$ifaces) {
-           my $d = $ifaces->{$iface};
+    foreach my $iface (sort {
+       my $ref1 = $ifaces->{$a};
+       my $ref2 = $ifaces->{$b};
+       my $p1 = $ref1->{priority} || 100000;
+       my $p2 = $ref2->{priority} || 100000;
 
-           next if $printed->{$iface};
-           next if $iface !~ m/^$t/;
+       return $p1 <=> $p2 if $p1 != $p2;
 
-           $printed->{$iface} = 1;
-           $raw .= __interface_to_string($iface, $d);
-       }
+
+       return $a cmp $b;
+                      } keys %$ifaces) {
+
+       my $d = $ifaces->{$iface};
+
+       next if $printed->{$iface};
+
+       $printed->{$iface} = 1;
+       $raw .= __interface_to_string($iface, $d);
     }
     
     PVE::Tools::safe_print($filename, $fh, $raw);