2023-02-01 21:33:44 +00:00
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/containerd/containerd/mount"
|
|
|
|
"github.com/deniswernert/go-fstab"
|
2023-02-09 11:35:45 +00:00
|
|
|
"github.com/kairos-io/immucore/internal/constants"
|
2023-02-07 21:25:28 +00:00
|
|
|
"github.com/moby/sys/mountinfo"
|
2023-02-28 18:46:15 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"os"
|
2023-02-01 21:33:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type mountOperation struct {
|
|
|
|
FstabEntry fstab.Mount
|
|
|
|
MountOption mount.Mount
|
|
|
|
Target string
|
|
|
|
PrepareCallback func() error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m mountOperation) run() error {
|
2023-02-28 18:46:15 +00:00
|
|
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr}).With().Str("what", m.MountOption.Source).Str("where", m.Target).Str("type", m.MountOption.Type).Strs("options", m.MountOption.Options).Logger()
|
2023-02-01 21:33:44 +00:00
|
|
|
if m.PrepareCallback != nil {
|
|
|
|
if err := m.PrepareCallback(); err != nil {
|
2023-02-28 18:46:15 +00:00
|
|
|
log.Logger.Err(err).Msg("executing mount callback")
|
2023-02-01 21:33:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-02-07 21:25:28 +00:00
|
|
|
//TODO: not only check if mounted but also if the type,options and source are the same?
|
|
|
|
mounted, err := mountinfo.Mounted(m.Target)
|
|
|
|
if err != nil {
|
2023-02-28 18:46:15 +00:00
|
|
|
log.Logger.Err(err).Msg("checking mount status")
|
2023-02-07 21:25:28 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if mounted {
|
2023-02-28 18:46:15 +00:00
|
|
|
log.Logger.Debug().Msg("Already mounted")
|
2023-02-09 11:35:45 +00:00
|
|
|
return constants.ErrAlreadyMounted
|
2023-02-07 21:25:28 +00:00
|
|
|
}
|
2023-02-28 18:46:15 +00:00
|
|
|
log.Logger.Debug().Msg("mount ready")
|
2023-02-01 21:33:44 +00:00
|
|
|
return mount.All([]mount.Mount{m.MountOption}, m.Target)
|
|
|
|
}
|