]> git.proxmox.com Git - proxmox-backup.git/commitdiff
src/config/network.rs: only save attriubutes used by configuration method
authorDietmar Maurer <dietmar@proxmox.com>
Wed, 22 Apr 2020 10:42:09 +0000 (12:42 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Wed, 22 Apr 2020 10:42:09 +0000 (12:42 +0200)
src/config/network.rs

index bb5ef30ea6d7ed9f0b4eb14c17c2f69e607f718c..82be5de2741fe36f8094d0f59c485221afead287 100644 (file)
@@ -97,13 +97,16 @@ impl Interface {
         }
     }
 
-    fn write_iface_attributes_v4(&self, w: &mut dyn Write) -> Result<(), Error> {
-        if let Some(address) = &self.cidr_v4 {
-            writeln!(w, "    address {}", address)?;
-        }
-        if let Some(gateway) = &self.gateway_v4 {
-            writeln!(w, "    gateway {}", gateway)?;
+    fn write_iface_attributes_v4(&self, w: &mut dyn Write, method: NetworkConfigMethod) -> Result<(), Error> {
+        if method == NetworkConfigMethod::Static {
+            if let Some(address) = &self.cidr_v4 {
+                writeln!(w, "    address {}", address)?;
+            }
+            if let Some(gateway) = &self.gateway_v4 {
+                writeln!(w, "    gateway {}", gateway)?;
+            }
         }
+
         for option in &self.options_v4 {
             writeln!(w, "    {}", option)?;
         }
@@ -111,13 +114,16 @@ impl Interface {
         Ok(())
     }
 
-    fn write_iface_attributes_v6(&self, w: &mut dyn Write) -> Result<(), Error> {
-        if let Some(address) = &self.cidr_v6 {
-            writeln!(w, "    address {}", address)?;
-        }
-        if let Some(gateway) = &self.gateway_v6 {
-            writeln!(w, "    gateway {}", gateway)?;
+    fn write_iface_attributes_v6(&self, w: &mut dyn Write, method: NetworkConfigMethod) -> Result<(), Error> {
+        if method == NetworkConfigMethod::Static {
+            if let Some(address) = &self.cidr_v6 {
+                writeln!(w, "    address {}", address)?;
+            }
+            if let Some(gateway) = &self.gateway_v6 {
+                writeln!(w, "    gateway {}", gateway)?;
+            }
         }
+
         for option in &self.options_v6 {
             writeln!(w, "    {}", option)?;
         }
@@ -136,25 +142,28 @@ impl Interface {
             }
         }
 
+        if self.method_v4.is_none() && self.method_v6.is_none() { return Ok(()); }
+
         if self.autostart {
             writeln!(w, "auto {}", self.name)?;
         }
 
         if self.method_v4 == self.method_v6 {
-            let method = self.method_v4.unwrap_or(NetworkConfigMethod::Static);
-            writeln!(w, "iface {} {}", self.name, method_to_str(method))?;
-            self.write_iface_attributes_v4(w)?;
-            self.write_iface_attributes_v6(w)?;
-            writeln!(w)?;
+            if let Some(method) = self.method_v4 {
+                writeln!(w, "iface {} {}", self.name, method_to_str(method))?;
+                self.write_iface_attributes_v4(w, method)?;
+                self.write_iface_attributes_v6(w, method)?;
+                writeln!(w)?;
+            }
         } else {
             if let Some(method) = self.method_v4 {
                 writeln!(w, "iface {} inet {}", self.name, method_to_str(method))?;
-                self.write_iface_attributes_v4(w)?;
+                self.write_iface_attributes_v4(w, method)?;
                 writeln!(w)?;
             }
             if let Some(method) = self.method_v6 {
                 writeln!(w, "iface {} inet6 {}", self.name, method_to_str(method))?;
-                self.write_iface_attributes_v6(w)?;
+                self.write_iface_attributes_v6(w, method)?;
                 writeln!(w)?;
             }
         }