]> git.proxmox.com Git - pve-common.git/blobdiff - data/PVE/INotify.pm
bump version to 1.0-39
[pve-common.git] / data / PVE / INotify.pm
index 0ee899764b9d9adf10ea8afecdffe399464643ac..77e2fd7aff1c70afd2d254125de33fabec6ca70f 100644 (file)
@@ -616,7 +616,7 @@ sub read_active_workers {
 
     my $res = []; 
     while (defined (my $line = <$fh>)) {
-       if ($line =~ m/^(\S+)\s(0|1)(\s([0-9A-Za-z]{8})(\s(\S.*))?)?$/) {
+       if ($line =~ m/^(\S+)\s(0|1)(\s([0-9A-Za-z]{8})(\s(\s*\S.*))?)?$/) {
            my $upid = $1;
            my $saved = $2;
            my $endtime = $4;
@@ -685,13 +685,16 @@ sub read_etc_network_interfaces {
 
     if (my $fd2 = IO::File->new("/proc/net/dev", "r")) {
        while (defined ($line = <$fd2>)) {
-           if ($line =~ m/^\s*(eth[0-9]):.*/) {
+           if ($line =~ m/^\s*(eth\d+):.*/) {
                $ifaces->{$1}->{exists} = 1;
            }
        }
        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,56 @@ 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*#(.*)\s*$/) {
+                   # NOTE: we use 'comments' instead of 'comment' to 
+                   # avoid automatic utf8 conversion
+                   $d->{comments} = '' if !$d->{comments};
+                   $d->{comments} .= "$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 +784,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;
@@ -796,7 +810,7 @@ sub read_etc_network_interfaces {
                $ifaces->{$1}->{exists} = 0;
                $d->{exists} = 0;
            }
-       } elsif ($iface =~ m/^eth[0-9]$/) {
+       } elsif ($iface =~ m/^eth\d+$/) {
            $d->{type} = 'eth';
        } elsif ($iface =~ m/^lo$/) {
            $d->{type} = 'loopback';
@@ -864,6 +878,12 @@ sub __interface_to_string {
        $raw .= "\t$option\n";
     }
 
+    # add comments
+    my $comments = $d->{comments} || '';
+    foreach my $cl (split(/\n/, $comments)) {
+       $raw .= "#$cl\n";
+    }
+
     $raw .= "\n";
 
     return $raw;
@@ -876,16 +896,58 @@ sub write_etc_network_interfaces {
 
     my $printed = {};
 
-    foreach my $t (('lo', 'eth', '')) {
-       foreach my $iface (sort keys %$ifaces) {
-           my $d = $ifaces->{$iface};
+    my $if_type_hash = {
+       loopback => 10,
+       eth => 20,
+       bond => 30,
+       bridge => 40,
+    };
 
-           next if $printed->{$iface};
-           next if $iface !~ m/^$t/;
+    my $lookup_type_prio = sub {
+       my $iface = shift;
 
-           $printed->{$iface} = 1;
-           $raw .= __interface_to_string($iface, $d);
+       my $alias = 0;
+       if ($iface =~ m/^(\S+):\d+$/) {
+           $iface = $1;
+           $alias = 1;
        }
+
+       my $pri;
+       if ($iface eq 'lo') {
+           $pri = $if_type_hash->{loopback};
+       } elsif ($iface =~ m/^eth\d+$/) {
+           $pri = $if_type_hash->{eth} + $alias;
+       } elsif ($iface =~ m/^bond\d+$/) {
+           $pri = $if_type_hash->{bond} + $alias;
+       } elsif ($iface =~ m/^vmbr\d+$/) {
+           $pri = $if_type_hash->{bridge} + $alias;
+       }
+
+       return $pri || ($if_type_hash->{unknown} + $alias);
+    };
+
+    foreach my $iface (sort {
+       my $ref1 = $ifaces->{$a};
+       my $ref2 = $ifaces->{$b};
+       my $p1 = &$lookup_type_prio($a);
+       my $p2 = &$lookup_type_prio($b);
+
+       return $p1 <=> $p2 if $p1 != $p2;
+
+       $p1 = $ref1->{priority} || 100000;
+       $p2 = $ref2->{priority} || 100000;
+
+       return $p1 <=> $p2 if $p1 != $p2;
+
+       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);