github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/dhclient/iface.go (about) 1 // Copyright 2019 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package dhclient 6 7 import ( 8 "fmt" 9 "log" 10 "os" 11 "path/filepath" 12 "regexp" 13 14 "github.com/vishvananda/netlink" 15 ) 16 17 // FilterBondedInterfaces takes a slice of links, checks to see if any of 18 // them are already bonded to a bond, and returns the ones that aren't bonded. 19 func FilterBondedInterfaces(ifs []netlink.Link, verbose bool) []netlink.Link { 20 t := []netlink.Link{} 21 for _, iface := range ifs { 22 n := iface.Attrs().Name 23 if _, err := os.Stat(filepath.Join("/sys/class/net", n, "master")); err != nil { 24 // if the master symlink does not exist, it probably means link isn't bonded to a bond. 25 t = append(t, iface) 26 } else if verbose { 27 log.Printf("skipping bonded interface %v", n) 28 } 29 } 30 return t 31 } 32 33 // Interfaces takes an RE and returns a 34 // []netlink.Link that matches it, or an error. It is an error 35 // for the returned list to be empty. 36 func Interfaces(ifName string) ([]netlink.Link, error) { 37 ifRE, err := regexp.CompilePOSIX(ifName) 38 if err != nil { 39 return nil, err 40 } 41 42 ifnames, err := netlink.LinkList() 43 if err != nil { 44 return nil, fmt.Errorf("can not get list of link names: %v", err) 45 } 46 47 var filteredIfs []netlink.Link 48 for _, iface := range ifnames { 49 if ifRE.MatchString(iface.Attrs().Name) { 50 filteredIfs = append(filteredIfs, iface) 51 } 52 } 53 54 if len(filteredIfs) == 0 { 55 return nil, fmt.Errorf("no interfaces match %s", ifName) 56 } 57 return filteredIfs, nil 58 }