func()

in cmd/core_plugin/network/nm/nm_linux.go [178:244]


func (sn *serviceNetworkManager) writeEthernetConfig(nic *nic.Configuration, filePath string) error {
	galog.Debugf("Writing NetworkManager config file: %s", filePath)

	// Create the ini file.
	iface := nic.Interface.Name()

	config := nmConfig{
		Connection: nmConnectionSection{
			InterfaceName:       iface,
			ID:                  sn.connectionID(iface),
			ConnType:            "ethernet",
			Autoconnect:         true,
			AutoconnectPriority: defaultAutoconnectPriority,
		},
		Ipv4: nmIPSection{
			Method: "auto",
		},
		Ipv6: nmIPSection{
			Method: "auto",
		},
	}

	inicfg, err := ini.ReflectFrom(&config)
	if err != nil {
		return fmt.Errorf("error marshalling ini file: %w", err)
	}

	if nic.ExtraAddresses != nil {
		routeIndex := 1
		for _, ipAddress := range nic.ExtraAddresses.MergedSlice() {
			routeKey := fmt.Sprintf("route%d", routeIndex)
			optionsKey := fmt.Sprintf("route%d_options", routeIndex)

			section := "ipv6"
			if !ipAddress.IsIPv6() {
				section = "ipv4"
			}

			ipSection, err := inicfg.GetSection(section)
			if err != nil {
				return fmt.Errorf("error getting %s section: %w", section, err)
			}

			ipSection.Key(routeKey).SetValue(ipAddress.String())
			ipSection.Key(optionsKey).SetValue("type=local")
			routeIndex++
		}
	}

	// Save the config file.
	if err := inicfg.SaveTo(filePath); err != nil {
		return fmt.Errorf("error writing NetworkManager config file: %v", err)
	}

	// If the permission is not properly set nmcli will fail to load the file
	// correctly.
	if err := os.Chmod(filePath, nmConfigFileMode); err != nil {
		return fmt.Errorf("error updating permissions for %s connection config: %w", iface, err)
	}

	// Remove the previously managed ifcfg file if it exists.
	if err := os.RemoveAll(sn.ifcfgFilePath(iface)); err != nil {
		return fmt.Errorf("failed to remove previously managed ifcfg file(%s): %w", sn.ifcfgFilePath(iface), err)
	}

	return nil
}