Exemple #1
0
        /// <summary>
        /// Dynamic bridge configuration using DHCP
        /// </summary>
        /// <param name="bridgeName"></param>
        /// <param name="networkInterfaces"></param>
        /// <param name="type"></param>
        public void ConfigureBridge(string bridgeName, IEnumerable <string> networkInterfaces, NetworkdConfigurationType type)
        {
            var bridgeFile  = $"{NetworkdFolder}/{bridgeName}.netdev";
            var bridgeLines = new List <string> {
                "[NetDev]",
                $"Name={bridgeName}",
                "Kind=bridge",
                ""
            };

            if (File.Exists(bridgeFile))
            {
                File.Copy(bridgeFile, $"{bridgeFile}.bck");
            }
            File.WriteAllLines(bridgeFile, bridgeLines);
            Restart();

            foreach (var networkInterface in networkInterfaces)
            {
                var file  = $"{NetworkdFolder}/{networkInterface}.network";
                var lines = new List <string> {
                    "[Match]",
                    $"Name={networkInterface}",
                    "",
                    "[Network]",
                    $"Bridge={bridgeName}",
                    ""
                };
                if (File.Exists(file))
                {
                    File.Copy(file, $"{file}.bck");
                }
                File.WriteAllLines(file, lines);
            }

            var bridgeNetworkFile  = $"{NetworkdFolder}/{bridgeName}.network";
            var bridgeNetworkLines = new List <string> {
                "[Match]",
                $"Name={bridgeName}",
                "",
                "[Network]",
                $"DHCP={type.ToString().ToLower()}",
                ""
            };

            if (File.Exists(bridgeNetworkFile))
            {
                File.Copy(bridgeNetworkFile, $"{bridgeNetworkFile}.bck");
            }
            File.WriteAllLines(bridgeNetworkFile, bridgeNetworkLines);
            Restart();
        }
Exemple #2
0
        /// <summary>
        /// Dynamic configuration using DHCP
        ///
        /// [Match]
        /// Name=enp1s0
        ///
        /// [Network]
        /// DHCP=ipv4
        ///
        /// </summary>
        /// <param name="networkInterface"></param>
        /// <param name="type"></param>
        public void Configure(string networkInterface, NetworkdConfigurationType type)
        {
            var file  = $"{NetworkdFolder}/{networkInterface}.network";
            var lines = new List <string> {
                "[Match]",
                $"Name={networkInterface}",
                "",
                "[Network]",
                $"DHCP={type.ToString().ToLower()}",
                ""
            };

            if (File.Exists(file))
            {
                File.Copy(file, $"{file}.bck");
            }
            File.WriteAllLines(file, lines);
            Restart();
        }