several improvements

- drop vfs
 - less hardcoded stuff, use the runtime status
 - Remove things that syscalls dont understand (defaults, auto)

Signed-off-by: Itxaka <itxaka@spectrocloud.com>
This commit is contained in:
Itxaka
2023-02-06 16:20:18 +01:00
parent fcdc2f28b6
commit f98954367f
7 changed files with 125 additions and 39 deletions

2
go.mod
View File

@@ -12,8 +12,8 @@ require (
github.com/onsi/ginkgo/v2 v2.8.0
github.com/onsi/gomega v1.26.0
github.com/rs/zerolog v1.29.0
github.com/sanity-io/litter v1.5.5
github.com/spectrocloud-labs/herd v0.3.0
github.com/twpayne/go-vfs v1.7.2
github.com/urfave/cli/v2 v2.24.3
)

8
go.sum
View File

@@ -245,6 +245,7 @@ github.com/d2g/dhcp4 v0.0.0-20170904100407-a1d1b6c41b1c/go.mod h1:Ct2BUK8SB0YC1S
github.com/d2g/dhcp4client v1.0.0/go.mod h1:j0hNfjhrt2SxUOw55nL0ATM/z4Yt3t2Kd1mW34z5W5s=
github.com/d2g/dhcp4server v0.0.0-20181031114812-7d4a0a7f59a5/go.mod h1:Eo87+Kg/IX2hfWJfwxMzLyuSZyxSoAug2nGa1G2QAi8=
github.com/d2g/hardwareaddr v0.0.0-20190221164911-e7d9fbe030e4/go.mod h1:bMl4RjIciD2oAxI7DmWRx6gbeqrkoLqv3MV0vzNad+I=
github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -598,6 +599,7 @@ github.com/pkg/errors v0.8.1-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pquerna/cachecontrol v0.0.0-20171018203845-0dec1b30a021/go.mod h1:prYjPmNq4d1NPVmpShWobRqXY3q7Vp+80DqgxxUrUIA=
@@ -654,6 +656,8 @@ github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQD
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/safchain/ethtool v0.0.0-20190326074333-42ed695e3de8/go.mod h1:Z0q5wiBQGYcxhMZ6gUqHn6pYNLypFAvaL3UvgZLR0U4=
github.com/sanity-io/litter v1.5.5 h1:iE+sBxPBzoK6uaEP5Lt3fHNgpKcHXc/A2HGETy0uJQo=
github.com/sanity-io/litter v1.5.5/go.mod h1:9gzJgR2i4ZpjZHsKvUXIRQVk7P+yM3e+jAF7bU2UI5U=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sclevine/spec v1.2.0/go.mod h1:W4J29eT/Kzv7/b9IWLB055Z+qvVC9vt0Arko24q7p+U=
github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvWlF4P2Ca7zGrPiEpWo=
@@ -696,6 +700,7 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v0.0.0-20180303142811-b89eecf5ca5d/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
@@ -712,8 +717,6 @@ github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG
github.com/tchap/go-patricia v2.2.6+incompatible/go.mod h1:bmLyhP68RS6kStMGxByiQ23RP/odRBOTVjwp2cDyi6I=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/twpayne/go-vfs v1.7.2 h1:ZNYMAXcu2Av8c109USrSGYm8dIIIV0xPlG19I2088Kw=
github.com/twpayne/go-vfs v1.7.2/go.mod h1:1eni2ntkiiAHZG27xfLOO4CYvMR4Kw8V7rYiLeeolsQ=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
@@ -926,7 +929,6 @@ golang.org/x/sys v0.0.0-20201117170446-d9b008d0a637/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201201145000-ef89a241ccb3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201202213521-69691e467435/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201223074533-0d417f636930/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210324051608-47abb6519492/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View File

@@ -9,7 +9,6 @@ import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spectrocloud-labs/herd"
"github.com/twpayne/go-vfs"
"github.com/urfave/cli/v2"
)
@@ -33,9 +32,13 @@ Sends a generic event payload with the configuration found in the scanned direct
Action: func(c *cli.Context) (err error) {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger()
g := herd.DAG()
s := &mount.State{Logger: log.Logger, Rootdir: "/"}
fs := vfs.OSFS
s := &mount.State{
Logger: log.Logger,
Rootdir: utils.GetRootDir(),
MountRoot: true,
TargetLabel: utils.BootStateToLabel(),
TargetImage: utils.BootStateToImage(),
}
err = s.Register(g)
if err != nil {
@@ -49,7 +52,7 @@ Sends a generic event payload with the configuration found in the scanned direct
return err
}
cdBoot, err := utils.BootedFromCD(fs)
cdBoot, err := utils.BootedFromCD()
if err != nil {
s.Logger.Err(err)
return err

View File

@@ -2,10 +2,11 @@ package utils
import (
"github.com/kairos-io/kairos/sdk/state"
"github.com/twpayne/go-vfs"
"os"
"strings"
)
func BootedFromCD(fs vfs.FS) (bool, error) {
func BootedFromCD() (bool, error) {
runtime, err := state.NewRuntime()
if err != nil {
return false, err
@@ -13,3 +14,48 @@ func BootedFromCD(fs vfs.FS) (bool, error) {
return runtime.BootState == state.LiveCD, nil
}
func BootStateToLabel() string {
runtime, err := state.NewRuntime()
if err != nil {
return ""
}
switch runtime.BootState {
case "active_boot":
return "COS_ACTIVE"
case "passive_boot":
return "COS_PASSIVE"
case "recovery_boot":
return "COS_RECOVERY"
default:
return ""
}
}
func BootStateToImage() string {
runtime, err := state.NewRuntime()
if err != nil {
return ""
}
switch runtime.BootState {
case "active_boot":
return "/cOS/active.img"
case "passive_boot":
return "/cOS/passive.img"
case "recovery_boot":
return "/recovery.img"
default:
return ""
}
}
func GetRootDir() string {
cmdline, _ := os.ReadFile("/proc/cmdline")
switch {
case strings.Contains(string(cmdline), "IMMUCORE_NOPIVOT"):
return "/"
default:
// Default is sysroot for normal no-pivot boot
return "/sysroot"
}
}

View File

@@ -2,6 +2,8 @@ package mount
import (
"fmt"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"os"
"os/exec"
"path/filepath"
@@ -36,6 +38,7 @@ func appendSlash(path string) string {
// https://github.com/kairos-io/packages/blob/94aa3bef3d1330cb6c6905ae164f5004b6a58b8c/packages/system/dracut/immutable-rootfs/30cos-immutable-rootfs/cos-mount-layout.sh#L129
func baseOverlay(overlay profile.Overlay) (mountOperation, error) {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger()
if err := os.MkdirAll(overlay.Base, 0700); err != nil {
return mountOperation{}, err
}
@@ -49,26 +52,26 @@ func baseOverlay(overlay profile.Overlay) (mountOperation, error) {
t := dat[0]
switch t {
case "tmpfs":
tmpMount := mount.Mount{Type: "tmpfs", Source: "tmpfs", Options: []string{"defaults", fmt.Sprintf("size=%s", dat[1])}}
tmpMount := mount.Mount{Type: "tmpfs", Source: "tmpfs", Options: []string{fmt.Sprintf("size=%s", dat[1])}}
err := mount.All([]mount.Mount{tmpMount}, overlay.Base)
fstab := mountToStab(tmpMount)
fstab.File = overlay.BackingBase
tmpFstab := mountToStab(tmpMount)
tmpFstab.File = overlay.BackingBase
return mountOperation{
MountOption: tmpMount,
FstabEntry: *fstab,
FstabEntry: *tmpFstab,
Target: overlay.Base,
}, err
case "block":
blockMount := mount.Mount{Type: "auto", Source: dat[1]}
err := mount.All([]mount.Mount{blockMount}, overlay.Base)
fstab := mountToStab(blockMount)
fstab.File = overlay.BackingBase
fstab.MntOps["default"] = ""
tmpFstab := mountToStab(blockMount)
tmpFstab.File = overlay.BackingBase
tmpFstab.MntOps["default"] = ""
return mountOperation{
MountOption: blockMount,
FstabEntry: *fstab,
FstabEntry: *tmpFstab,
Target: overlay.Base,
}, err
default:
@@ -115,12 +118,12 @@ func mountBind(mountpoint, root, stateTarget string) (mountOperation, error) {
},
}
fstab := mountToStab(tmpMount)
fstab.File = fmt.Sprintf("/%s", mountpoint)
fstab.Spec = strings.ReplaceAll(fstab.Spec, root, "")
tmpFstab := mountToStab(tmpMount)
tmpFstab.File = fmt.Sprintf("/%s", mountpoint)
tmpFstab.Spec = strings.ReplaceAll(tmpFstab.Spec, root, "")
return mountOperation{
MountOption: tmpMount,
FstabEntry: *fstab,
FstabEntry: *tmpFstab,
Target: rootMount,
PrepareCallback: func() error {
if err := createIfNotExists(rootMount); err != nil {
@@ -144,6 +147,8 @@ func syncState(src, dst string) error {
// https://github.com/kairos-io/packages/blob/94aa3bef3d1330cb6c6905ae164f5004b6a58b8c/packages/system/dracut/immutable-rootfs/30cos-immutable-rootfs/cos-mount-layout.sh#L145
func mountWithBaseOverlay(mountpoint, root, base string) (mountOperation, error) {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger()
log.Debug().Str("mountpoint", mountpoint).Str("root", root).Str("base", base).Msg("mount with base overlay")
mountpoint = strings.TrimLeft(mountpoint, "/") // normalize, remove / upfront as we are going to re-use it in subdirs
rootMount := filepath.Join(root, mountpoint)
bindMountPath := strings.ReplaceAll(mountpoint, "/", "-")
@@ -164,14 +169,14 @@ func mountWithBaseOverlay(mountpoint, root, base string) (mountOperation, error)
},
}
fstab := mountToStab(tmpMount)
fstab.File = rootMount
tmpFstab := mountToStab(tmpMount)
tmpFstab.File = rootMount
// TODO: update fstab with x-systemd info
// https://github.com/kairos-io/packages/blob/94aa3bef3d1330cb6c6905ae164f5004b6a58b8c/packages/system/dracut/immutable-rootfs/30cos-immutable-rootfs/cos-mount-layout.sh#L170
return mountOperation{
MountOption: tmpMount,
FstabEntry: *fstab,
FstabEntry: *tmpFstab,
Target: rootMount,
PrepareCallback: func() error {
// Make sure workdir and/or upper exists

View File

@@ -3,7 +3,9 @@ package mount
import (
"context"
"fmt"
"github.com/kairos-io/kairos/sdk/state"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"os"
"path/filepath"
"strings"
@@ -15,6 +17,7 @@ import (
"github.com/joho/godotenv"
"github.com/kairos-io/immucore/pkg/profile"
"github.com/kairos-io/kairos/pkg/utils"
"github.com/sanity-io/litter"
"github.com/spectrocloud-labs/herd"
)
@@ -57,11 +60,13 @@ func (s *State) path(p ...string) string {
}
func (s *State) WriteFstab(fstabFile string) func(context.Context) error {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger()
return func(ctx context.Context) error {
for _, fst := range s.fstabs {
select {
case <-ctx.Done():
default:
log.Logger.Debug().Str("fstabline", litter.Sdump(fst)).Str("fstabfile", fstabFile).Msg("Writing fstab line")
f, err := os.OpenFile(fstabFile,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
@@ -73,6 +78,7 @@ func (s *State) WriteFstab(fstabFile string) func(context.Context) error {
if _, err := f.WriteString(toWrite); err != nil {
return err
}
log.Logger.Debug().Str("fstabline", litter.Sdump(fst)).Str("fstabfile", fstabFile).Msg("Done fstab line")
}
}
return nil
@@ -90,12 +96,21 @@ func (s *State) RunStageOp(stage string) func(context.Context) error {
}
func (s *State) MountOP(what, where, t string, options []string, timeout time.Duration) func(context.Context) error {
s.Logger.Debug().Str("what", what).Str("where", where).Str("type", t)
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger()
return func(c context.Context) error {
cc := time.After(timeout)
for {
select {
default:
if _, err := os.Stat(where); os.IsNotExist(err) {
log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Msg("Mount point does not exist, creating")
err = os.MkdirAll(where, os.ModeDir|os.ModePerm)
if err != nil {
log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Err(err).Msg("")
continue
}
}
time.Sleep(1 * time.Second)
mountPoint := mount.Mount{
Type: t,
@@ -112,16 +127,21 @@ func (s *State) MountOP(what, where, t string, options []string, timeout time.Du
err := op.run()
if err != nil {
log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Err(err).Msg("")
continue
}
s.fstabs = append(s.fstabs, tmpFstab)
log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Msg("Mounted")
return nil
case <-c.Done():
return fmt.Errorf("context canceled")
e := fmt.Errorf("context canceled")
log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Err(e).Msg("")
return e
case <-cc:
return fmt.Errorf("timeout exhausted")
e := fmt.Errorf("timeout exhausted")
log.Logger.Debug().Str("what", what).Str("where", where).Str("type", t).Str("options", litter.Sdump(options)).Err(e).Msg("")
return e
}
}
}
@@ -162,6 +182,11 @@ func readEnv(file string) (map[string]string, error) {
func (s *State) Register(g *herd.Graph) error {
var err error
runtime, err := state.NewRuntime()
if err != nil {
return err
}
// TODO: add hooks, fstab (might have missed some), systemd compat
// TODO: We should also set tmpfs here (not -related)
@@ -177,7 +202,10 @@ func (s *State) Register(g *herd.Graph) error {
herd.WithDeps(opMountState),
herd.WithCallback(
func(ctx context.Context) error {
_, err := utils.SH(fmt.Sprintf("losetup --show -f /run/initramfs/cos-state%s", s.TargetImage))
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger()
cmd := fmt.Sprintf("losetup --show -f /run/initramfs/cos-state%s", s.TargetImage)
log.Logger.Debug().Str("targetImage", s.TargetImage).Str("fullcmd", cmd).Msg("Mounting image")
_, err := utils.SH(cmd)
return err
},
))
@@ -186,13 +214,14 @@ func (s *State) Register(g *herd.Graph) error {
}
// mount the state partition so to find the loopback device
// Itxaka: what if its recovery?
s.Logger.Debug().Str("what", opMountState).Msg("Add operation")
err = g.Add(opMountState,
herd.WithCallback(
s.MountOP(
"/dev/disk/by-label/COS_STATE",
runtime.State.Name,
s.path("/run/initramfs/cos-state"),
"auto",
runtime.State.Type,
[]string{
"ro", // or rw
}, 60*time.Second),
@@ -210,7 +239,7 @@ func (s *State) Register(g *herd.Graph) error {
s.MountOP(
fmt.Sprintf("/dev/disk/by-label/%s", s.TargetLabel),
s.Rootdir,
"auto",
"ext2", // are images always ext2?
[]string{
"ro", // or rw
"suid",
@@ -247,12 +276,12 @@ func (s *State) Register(g *herd.Graph) error {
err = g.Add(opLoadConfig,
herd.WithDeps(opRootfsHook),
herd.WithCallback(func(ctx context.Context) error {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Caller().Logger()
env, err := readEnv("/run/cos-layout.env")
if err != nil {
return err
}
log.Logger.Debug().Str("envfile", litter.Sdump(env)).Msg("loading cos layout")
// populate from env here
s.OverlayDir = strings.Split(env["RW_PATHS"], " ")
@@ -383,9 +412,9 @@ func (s *State) Register(g *herd.Graph) error {
mountRootCondition,
herd.WithCallback(
s.MountOP(
"/dev/disk/by-label/COS_OEM",
runtime.OEM.Label,
s.path("/oem"),
"auto",
runtime.OEM.Type,
[]string{
"rw",
"suid",
@@ -394,7 +423,7 @@ func (s *State) Register(g *herd.Graph) error {
"noauto",
"nouser",
"async",
}, 60*time.Second),
}, 10*time.Second),
),
)
if err != nil {

View File

@@ -18,5 +18,6 @@ func (m mountOperation) run() error {
return err
}
}
return mount.All([]mount.Mount{m.MountOption}, m.Target)
}