use udev for lvm & net
This commit is contained in:
parent
7325ee1c0c
commit
0f9679d91e
@ -1,9 +1,11 @@
|
||||
from golang:1.21.6-alpine3.19 as build
|
||||
from golang:1.23.2-alpine3.20 as build
|
||||
run apk add --no-cache gcc musl-dev linux-headers eudev-dev
|
||||
|
||||
workdir /src
|
||||
|
||||
copy . .
|
||||
|
||||
env CGO_ENABLED=1
|
||||
run \
|
||||
--mount=type=cache,id=gomod,target=/go/pkg/mod \
|
||||
--mount=type=cache,id=gobuild,target=/root/.cache/go-build \
|
||||
@ -11,7 +13,7 @@ run \
|
||||
&& go build -o /go/bin/init -trimpath .
|
||||
|
||||
# ------------------------------------------------------------------------
|
||||
from alpine:3.19.0 as initrd
|
||||
from alpine:3.20.3 as initrd
|
||||
|
||||
run apk add --no-cache xz
|
||||
|
||||
@ -30,6 +32,6 @@ run chroot /layer /init hello
|
||||
run find |cpio -H newc -o >/initrd
|
||||
|
||||
# ------------------------------------------------------------------------
|
||||
from alpine:3.19.0
|
||||
from alpine:3.20.3
|
||||
copy --from=initrd /initrd /
|
||||
entrypoint ["base64","/initrd"]
|
||||
|
22
go.mod
22
go.mod
@ -1,31 +1,33 @@
|
||||
module novit.tech/direktil/initrd
|
||||
|
||||
require (
|
||||
github.com/creack/pty v1.1.21
|
||||
github.com/creack/pty v1.1.24
|
||||
github.com/freddierice/go-losetup/v2 v2.0.1
|
||||
github.com/jochenvg/go-udev v0.0.0-20240801134859-b65ed646224b
|
||||
github.com/pkg/term v1.1.0
|
||||
github.com/rs/zerolog v1.31.0
|
||||
golang.org/x/crypto v0.18.0
|
||||
golang.org/x/sys v0.16.0
|
||||
golang.org/x/term v0.16.0
|
||||
github.com/rs/zerolog v1.33.0
|
||||
golang.org/x/crypto v0.28.0
|
||||
golang.org/x/sys v0.26.0
|
||||
golang.org/x/term v0.25.0
|
||||
golang.zx2c4.com/wireguard/wgctrl v0.0.0-20230429144221-925a1e7659e6
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
novit.tech/direktil/pkg v0.0.0-20240120172717-8498a102796f
|
||||
novit.tech/direktil/pkg v0.0.0-20240415130406-0d2e181a4ed6
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cavaliergopher/cpio v1.0.1 // indirect
|
||||
github.com/google/go-cmp v0.6.0 // indirect
|
||||
github.com/jkeiser/iter v0.0.0-20200628201005-c8aa0ae784d1 // indirect
|
||||
github.com/josharian/native v1.1.0 // indirect
|
||||
github.com/mattn/go-colorable v0.1.13 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mdlayher/genetlink v1.3.2 // indirect
|
||||
github.com/mdlayher/netlink v1.7.2 // indirect
|
||||
github.com/mdlayher/socket v0.5.0 // indirect
|
||||
golang.org/x/net v0.20.0 // indirect
|
||||
golang.org/x/sync v0.6.0 // indirect
|
||||
github.com/mdlayher/socket v0.5.1 // indirect
|
||||
golang.org/x/net v0.30.0 // indirect
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
golang.zx2c4.com/wireguard v0.0.0-20231211153847-12269c276173 // indirect
|
||||
)
|
||||
|
||||
go 1.21
|
||||
go 1.23
|
||||
|
36
go.sum
36
go.sum
@ -1,13 +1,17 @@
|
||||
github.com/cavaliergopher/cpio v1.0.1 h1:KQFSeKmZhv0cr+kawA3a0xTQCU4QxXF1vhU7P7av2KM=
|
||||
github.com/cavaliergopher/cpio v1.0.1/go.mod h1:pBdaqQjnvXxdS/6CvNDwIANIFSP0xRKI16PX4xejRQc=
|
||||
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
|
||||
github.com/creack/pty v1.1.21 h1:1/QdRyBaHHJP61QkWMXlOIBfsgdDeeKfK8SYVUWJKf0=
|
||||
github.com/creack/pty v1.1.21/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
|
||||
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
|
||||
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
|
||||
github.com/freddierice/go-losetup/v2 v2.0.1 h1:wPDx/Elu9nDV8y/CvIbEDz5Xi5Zo80y4h7MKbi3XaAI=
|
||||
github.com/freddierice/go-losetup/v2 v2.0.1/go.mod h1:TEyBrvlOelsPEhfWD5rutNXDmUszBXuFnwT1kIQF4J8=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/jkeiser/iter v0.0.0-20200628201005-c8aa0ae784d1 h1:smvLGU3obGU5kny71BtE/ibR0wIXRUiRFDmSn0Nxz1E=
|
||||
github.com/jkeiser/iter v0.0.0-20200628201005-c8aa0ae784d1/go.mod h1:fP/NdyhRVOv09PLRbVXrSqHhrfQypdZwgE2L4h2U5C8=
|
||||
github.com/jochenvg/go-udev v0.0.0-20240801134859-b65ed646224b h1:Pzf7tldbCVqwl3NnOnTamEWdh/rL41fsoYCn2HdHgRA=
|
||||
github.com/jochenvg/go-udev v0.0.0-20240801134859-b65ed646224b/go.mod h1:IBDUGq30U56w969YNPomhMbRje1GrhUsCh7tHdwgLXA=
|
||||
github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtLA=
|
||||
github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w=
|
||||
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
|
||||
@ -20,22 +24,24 @@ github.com/mdlayher/genetlink v1.3.2 h1:KdrNKe+CTu+IbZnm/GVUMXSqBBLqcGpRDa0xkQy5
|
||||
github.com/mdlayher/genetlink v1.3.2/go.mod h1:tcC3pkCrPUGIKKsCsp0B3AdaaKuHtaxoJRz3cc+528o=
|
||||
github.com/mdlayher/netlink v1.7.2 h1:/UtM3ofJap7Vl4QWCPDGXY8d3GIY2UGSDbK+QWmY8/g=
|
||||
github.com/mdlayher/netlink v1.7.2/go.mod h1:xraEF7uJbxLhc5fpHL4cPe221LI2bdttWlU+ZGLfQSw=
|
||||
github.com/mdlayher/socket v0.5.0 h1:ilICZmJcQz70vrWVes1MFera4jGiWNocSkykwwoy3XI=
|
||||
github.com/mdlayher/socket v0.5.0/go.mod h1:WkcBFfvyG8QENs5+hfQPl1X6Jpd2yeLIYgrGFmJiJxI=
|
||||
github.com/mdlayher/socket v0.5.1 h1:VZaqt6RkGkt2OE9l3GcC6nZkqD3xKeQLyfleW/uBcos=
|
||||
github.com/mdlayher/socket v0.5.1/go.mod h1:TjPLHI1UgwEv5J1B5q0zTZq12A/6H7nKmtTanQE37IQ=
|
||||
github.com/mikioh/ipaddr v0.0.0-20190404000644-d465c8ab6721 h1:RlZweED6sbSArvlE924+mUcZuXKLBHA35U7LN621Bws=
|
||||
github.com/mikioh/ipaddr v0.0.0-20190404000644-d465c8ab6721/go.mod h1:Ickgr2WtCLZ2MDGd4Gr0geeCH5HybhRJbonOgQpvSxc=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pkg/term v1.1.0 h1:xIAAdCMh3QIAy+5FrE8Ad8XoDhEU4ufwbaSozViP9kk=
|
||||
github.com/pkg/term v1.1.0/go.mod h1:E25nymQcrSllhX42Ok8MRm1+hyBdHY0dCeiKZ9jpNGw=
|
||||
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
|
||||
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
|
||||
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
|
||||
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
|
||||
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
|
||||
github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.18.0 h1:PGVlW0xEltQnzFZ55hkuX5+KLyrMYhHld1YHO4AKcdc=
|
||||
golang.org/x/crypto v0.18.0/go.mod h1:R0j02AL6hcrfOiy9T4ZYp/rcWeMxM3L6QYxlOuEG1mg=
|
||||
golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw=
|
||||
golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
@ -43,13 +49,13 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
|
||||
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
|
||||
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
|
||||
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
|
||||
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20200909081042-eff7692f9009/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
@ -61,14 +67,16 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
|
||||
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
|
||||
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
|
||||
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
|
||||
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
|
||||
golang.org/x/term v0.25.0 h1:WtHI/ltw4NvSUig5KARz9h521QvRC8RmF/cuYqifU24=
|
||||
golang.org/x/term v0.25.0/go.mod h1:RPyXicDX+6vLxogjjRxjgD2TKtmAO6NZBsBRfrOLu7M=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
@ -90,5 +98,5 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
novit.tech/direktil/pkg v0.0.0-20240120172717-8498a102796f h1:7y11nLhChrrsLQwRaW7wn/9x+Xn2gEVtzj75VkOpJ+o=
|
||||
novit.tech/direktil/pkg v0.0.0-20240120172717-8498a102796f/go.mod h1:zjezU6tELE880oYHs/WAauGBupKIEQQ7KqWTB69RW10=
|
||||
novit.tech/direktil/pkg v0.0.0-20240415130406-0d2e181a4ed6 h1:D0TN5GyZ4d88ILpgVZgcZ62027lW8/LLnQSpQyN2yOw=
|
||||
novit.tech/direktil/pkg v0.0.0-20240415130406-0d2e181a4ed6/go.mod h1:zjezU6tELE880oYHs/WAauGBupKIEQQ7KqWTB69RW10=
|
||||
|
56
lvm.go
56
lvm.go
@ -8,7 +8,9 @@ import (
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
||||
udev "github.com/jochenvg/go-udev"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
config "novit.tech/direktil/pkg/bootstrapconfig"
|
||||
@ -90,25 +92,47 @@ func setupVG(vg config.LvmVG) {
|
||||
log.Info().Msg("LVM VG does not exists, creating")
|
||||
}
|
||||
|
||||
devNames := make([]string, 0)
|
||||
err = filepath.Walk("/dev", func(n string, fi fs.FileInfo, err error) error {
|
||||
if fi.Mode().Type() == os.ModeDevice {
|
||||
devNames = append(devNames, n)
|
||||
devNames := make([]NameAliases, 0)
|
||||
{
|
||||
devRefs := map[uint64]*NameAliases{}
|
||||
|
||||
enum := new(udev.Udev).NewEnumerate()
|
||||
enum.AddMatchSubsystem("block")
|
||||
|
||||
devs, err := enum.Devices()
|
||||
if err != nil {
|
||||
fatal("udev enumeration failed")
|
||||
}
|
||||
|
||||
for _, dev := range devs {
|
||||
num := dev.Devnum()
|
||||
|
||||
n := dev.PropertyValue("DEVNAME")
|
||||
idx := len(devNames)
|
||||
devNames = append(devNames, nameAlias(n))
|
||||
|
||||
ref := uint64(num.Major())<<8 | uint64(num.Minor())
|
||||
devRefs[ref] = &devNames[idx]
|
||||
}
|
||||
|
||||
err = filepath.Walk("/dev", func(n string, fi fs.FileInfo, err error) error {
|
||||
if fi.Mode().Type() == os.ModeDevice {
|
||||
stat := fi.Sys().(*syscall.Stat_t)
|
||||
ref := stat.Rdev
|
||||
if na := devRefs[ref]; na != nil {
|
||||
na.AddAlias(n)
|
||||
}
|
||||
}
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
fatalf("failed to walk /dev: %v", err)
|
||||
}
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
fatalf("failed to walk /dev: %v", err)
|
||||
}
|
||||
|
||||
devNames = filter(devNames, func(v string) bool {
|
||||
for _, pv := range pvs.PVs() {
|
||||
if v == pv.Name {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
})
|
||||
for _, dev := range devNames {
|
||||
log.Info().Str("name", dev.Name).Any("aliases", dev.Aliases).Msg("found block device")
|
||||
}
|
||||
|
||||
m := regexpSelectN(vg.PVs.N, vg.PVs.Regexps, devNames)
|
||||
if len(m) == 0 {
|
||||
|
47
network.go
47
network.go
@ -1,10 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
||||
"os/exec"
|
||||
"strings"
|
||||
|
||||
udev "github.com/jochenvg/go-udev"
|
||||
"github.com/rs/zerolog/log"
|
||||
|
||||
config "novit.tech/direktil/pkg/bootstrapconfig"
|
||||
@ -16,14 +16,31 @@ func setupNetworks(cfg *config.Config) {
|
||||
return
|
||||
}
|
||||
|
||||
ifNames := make([]string, 0)
|
||||
type Iface struct {
|
||||
Name string
|
||||
AliasOf string
|
||||
}
|
||||
|
||||
ifaces := make([]NameAliases, 0)
|
||||
{
|
||||
ifaces, err := net.Interfaces()
|
||||
enum := new(udev.Udev).NewEnumerate()
|
||||
enum.AddMatchSubsystem("net")
|
||||
|
||||
devs, err := enum.Devices()
|
||||
if err != nil {
|
||||
fatal("failed")
|
||||
fatal("udev enumeration failed")
|
||||
}
|
||||
for _, iface := range ifaces {
|
||||
ifNames = append(ifNames, iface.Name)
|
||||
|
||||
for _, dev := range devs {
|
||||
iface := nameAliases(dev.Sysname(),
|
||||
dev.PropertyValue("INTERFACE"),
|
||||
dev.PropertyValue("ID_NET_NAME"),
|
||||
dev.PropertyValue("ID_NET_NAME_PATH"),
|
||||
dev.PropertyValue("ID_NET_NAME_MAC"),
|
||||
dev.PropertyValue("ID_NET_NAME_SLOT"),
|
||||
)
|
||||
log.Info().Str("name", iface.Name).Any("aliases", iface.Aliases).Msg("found network device")
|
||||
ifaces = append(ifaces, iface)
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,17 +50,9 @@ func setupNetworks(cfg *config.Config) {
|
||||
log := log.With().Str("network", network.Name).Logger()
|
||||
log.Info().Msg("setting up network")
|
||||
|
||||
// compute available names
|
||||
if len(assigned) != 0 {
|
||||
newNames := make([]string, 0, len(ifNames))
|
||||
for _, n := range ifNames {
|
||||
if assigned[n] {
|
||||
continue
|
||||
}
|
||||
newNames = append(newNames, n)
|
||||
}
|
||||
ifNames = newNames
|
||||
}
|
||||
unassigned := filter(ifaces, func(iface NameAliases) bool {
|
||||
return !assigned[iface.Name]
|
||||
})
|
||||
|
||||
// assign envvars
|
||||
envvars := make([]string, 0, 1+len(network.Interfaces))
|
||||
@ -53,13 +62,13 @@ func setupNetworks(cfg *config.Config) {
|
||||
envvar := new(strings.Builder)
|
||||
envvar.WriteString(match.Var)
|
||||
envvar.WriteByte('=')
|
||||
for i, m := range regexpSelectN(match.N, match.Regexps, unassigned) {
|
||||
assigned[m] = true
|
||||
|
||||
for i, m := range regexpSelectN(match.N, match.Regexps, ifNames) {
|
||||
if i != 0 {
|
||||
envvar.WriteByte(' ')
|
||||
}
|
||||
envvar.WriteString(m)
|
||||
assigned[m] = true
|
||||
}
|
||||
|
||||
envvars = append(envvars, envvar.String())
|
||||
|
45
regexp.go
45
regexp.go
@ -6,13 +6,36 @@ import (
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
func regexpSelectN(n int, regexps []string, names []string) (matches []string) {
|
||||
if n <= 0 {
|
||||
matches = make([]string, 0)
|
||||
} else {
|
||||
matches = make([]string, 0, n)
|
||||
type NameAliases struct {
|
||||
Name string
|
||||
Aliases []string
|
||||
}
|
||||
|
||||
func nameAlias(name string) NameAliases {
|
||||
return NameAliases{Name: name, Aliases: []string{name}}
|
||||
}
|
||||
func nameAliases(name string, aliases ...string) NameAliases {
|
||||
na := NameAliases{Name: name, Aliases: make([]string, 0, len(aliases)+1)}
|
||||
na.Aliases = append(na.Aliases, name)
|
||||
for _, alias := range aliases {
|
||||
na.AddAlias(alias)
|
||||
}
|
||||
|
||||
return na
|
||||
}
|
||||
func (na *NameAliases) AddAlias(alias string) {
|
||||
if alias == "" {
|
||||
return
|
||||
}
|
||||
if contains(na.Aliases, func(s string) bool { return s == alias }) {
|
||||
return
|
||||
}
|
||||
na.Aliases = append(na.Aliases, alias)
|
||||
}
|
||||
|
||||
func regexpSelectN(n int, regexps []string, nameAliases []NameAliases) (matches []string) {
|
||||
matches = make([]string, 0)
|
||||
|
||||
res := make([]*regexp.Regexp, 0, len(regexps))
|
||||
for _, reStr := range regexps {
|
||||
re, err := regexp.Compile(reStr)
|
||||
@ -23,15 +46,17 @@ func regexpSelectN(n int, regexps []string, names []string) (matches []string) {
|
||||
res = append(res, re)
|
||||
}
|
||||
|
||||
namesLoop:
|
||||
for _, name := range names {
|
||||
nameAliasesLoop:
|
||||
for _, item := range nameAliases {
|
||||
if len(matches) == n {
|
||||
break
|
||||
}
|
||||
for _, re := range res {
|
||||
if re.MatchString(name) {
|
||||
matches = append(matches, name)
|
||||
continue namesLoop
|
||||
for _, alias := range item.Aliases {
|
||||
if re.MatchString(alias) {
|
||||
matches = append(matches, item.Name)
|
||||
continue nameAliasesLoop
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user