func()

in google_guest_agent/network/manager/wicked_linux.go [261:313]


func (n *wicked) removeInterface(ctx context.Context, iface string) error {
	configFilePath := n.ifcfgFilePath(iface)

	// Check if the file exists.
	info, err := os.Stat(configFilePath)
	if err != nil {
		if os.IsNotExist(err) {
			return nil
		}
		return fmt.Errorf("failed to stat wicked ifcfg file: %+v", err)
	}

	commentLen := len(googleComment)

	// We definetly don't manage this file, skip it.
	if info.Size() < int64(commentLen) {
		return nil
	}

	configFile, err := os.Open(configFilePath)
	if err != nil {
		return fmt.Errorf("failed to open wicked ifcfg file: %+v", err)
	}
	defer configFile.Close()

	buffer := make([]byte, commentLen)
	readSize, err := configFile.Read(buffer)
	if err != nil {
		return fmt.Errorf("failed to read google comment from wicked ifcfg file: %+v", err)
	}

	if readSize != commentLen {
		return fmt.Errorf("failed to read comment section, read %d bytes, expected to read %d",
			readSize, commentLen)
	}

	// This file is clearly not managed by us.
	if string(buffer) != googleComment {
		return nil
	}

	// Delete the ifcfg file.
	if err = os.Remove(configFilePath); err != nil {
		return fmt.Errorf("error deleting config file for %s: %v", iface, err)
	}

	// Reload for this interface.
	if err = run.Quiet(ctx, n.wickedCommand, "ifreload", iface); err != nil {
		return fmt.Errorf("error reloading config for %s: %v", iface, err)
	}

	return nil
}