getting defaults mounts from warewulf.conf

This commit is contained in:
Christian Goll
2023-01-31 09:18:49 +01:00
parent d82314460f
commit 157c3cd923
7 changed files with 61 additions and 120 deletions

View File

@@ -248,8 +248,6 @@ config_defaults: vendor cmd/config_defaults/config_defaults.go
print_defaults: vendor cmd/print_defaults/print_defaults.go print_defaults: vendor cmd/print_defaults/print_defaults.go
cd cmd/print_defaults && go build -ldflags="-X 'github.com/hpcng/warewulf/internal/pkg/warewulfconf.ConfigFile=./etc/warewulf.conf'" -o ../../print_defaults cd cmd/print_defaults && go build -ldflags="-X 'github.com/hpcng/warewulf/internal/pkg/warewulfconf.ConfigFile=./etc/warewulf.conf'" -o ../../print_defaults
print_mnts: vendor cmd/print_mnts/print_mnts.go
cd cmd/print_mnts && go build -ldflags="-X 'github.com/hpcng/warewulf/internal/pkg/warewulfconf.ConfigFile=./etc/warewulf.conf'" -o ../../print_mnts
update_configuration: vendor cmd/update_configuration/update_configuration.go update_configuration: vendor cmd/update_configuration/update_configuration.go
cd cmd/update_configuration && go build -ldflags="-X 'github.com/hpcng/warewulf/internal/pkg/warewulfconf.ConfigFile=./etc/warewulf.conf'\ cd cmd/update_configuration && go build -ldflags="-X 'github.com/hpcng/warewulf/internal/pkg/warewulfconf.ConfigFile=./etc/warewulf.conf'\
@@ -312,7 +310,6 @@ clean:
rm -f config_defaults rm -f config_defaults
rm -f update_configuration rm -f update_configuration
rm -f print_defaults rm -f print_defaults
rm -f print_mnts
rm -f etc/wwapi{c,d,rd}.conf rm -f etc/wwapi{c,d,rd}.conf
install: files install_wwclient install: files install_wwclient

View File

@@ -1,37 +0,0 @@
package print_mnts
import (
"fmt"
"github.com/hpcng/warewulf/internal/pkg/container"
"gopkg.in/yaml.v2"
)
func main() {
mounts := []container.MntDetails{
container.MntDetails{
Source: "/etc/resolv.conf",
Dest: "/etc/resolv.conf",
ReadOnly: false,
},
container.MntDetails{
Source: "etc/zypp/credentials.d/SCCcredentials",
Dest: "etc/zypp/credentials.d/SCCcredentials",
ReadOnly: false,
},
container.MntDetails{
Source: "/etc/SUSEConnect",
Dest: "/etc/SUSEConnect",
ReadOnly: false,
},
}
var ptrs []*container.MntDetails
for _, m := range mounts {
ptrs = append(ptrs, &m)
}
mountPts := container.MountPoints{
MountPnts: ptrs,
}
data, _ := yaml.Marshal(mountPts)
fmt.Println(data)
}

View File

@@ -14,6 +14,7 @@ import (
"github.com/hpcng/warewulf/internal/pkg/buildconfig" "github.com/hpcng/warewulf/internal/pkg/buildconfig"
"github.com/hpcng/warewulf/internal/pkg/container" "github.com/hpcng/warewulf/internal/pkg/container"
"github.com/hpcng/warewulf/internal/pkg/util" "github.com/hpcng/warewulf/internal/pkg/util"
"github.com/hpcng/warewulf/internal/pkg/warewulfconf"
"github.com/hpcng/warewulf/internal/pkg/wwlog" "github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@@ -31,8 +32,11 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
wwlog.Error("Unknown Warewulf container: %s", containerName) wwlog.Error("Unknown Warewulf container: %s", containerName)
os.Exit(1) os.Exit(1)
} }
var err error conf, err := warewulfconf.New()
mountPts := container.DefaultMntPts() if err != nil {
wwlog.Verbose("Couldn't get warewulf ocnfiguration: %s", err)
}
mountPts := conf.MountsContainer
mountPts = append(container.InitMountPnts(binds), mountPts...) mountPts = append(container.InitMountPnts(binds), mountPts...)
// check for valid mount points // check for valid mount points
lowerObjects := checkMountPoints(containerName, mountPts) lowerObjects := checkMountPoints(containerName, mountPts)
@@ -105,25 +109,21 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "failed to mount /dev") return errors.Wrap(err, "failed to mount /dev")
} }
for _, b := range binds { for _, mntPnt := range mountPts {
var source string err = syscall.Mount(mntPnt.Source, path.Join(containerPath, mntPnt.Dest), "", syscall.MS_BIND, "")
var dest string
bind := util.SplitValidPaths(b, ":")
source = bind[0]
if len(bind) == 1 {
dest = source
} else {
dest = bind[1]
}
err := syscall.Mount(source, path.Join(containerPath, dest), "", syscall.MS_BIND, "")
if err != nil { if err != nil {
fmt.Printf("BIND ERROR: %s\n", err) fmt.Printf("BIND ERROR: %s\n", err)
wwlog.Warn("Couldn't mount %s", source) wwlog.Warn("Couldn't mount %s to %s", mntPnt.Source, mntPnt.Dest)
} else if mntPnt.ReadOnly {
err = syscall.Mount(mntPnt.Source, path.Join(containerPath, mntPnt.Dest), "", syscall.MS_REMOUNT|syscall.MS_RDONLY|syscall.MS_BIND, "")
if err != nil {
wwlog.Warn("failed to following mount readonly: %s", mntPnt.Source)
} else {
wwlog.Verbose("mounted readonly from host to container: %s:%s", mntPnt.Source, mntPnt.Dest)
}
} else {
wwlog.Verbose("mounted from host to container: %s:%s", mntPnt.Source, mntPnt.Dest)
} }
wwlog.Verbose("mounted from host to container: %s:%s", source, dest)
} }
err = syscall.Chroot(containerPath) err = syscall.Chroot(containerPath)
@@ -156,16 +156,16 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
Check if the bind mount points exists in the given container. Returns Check if the bind mount points exists in the given container. Returns
the invalid mount points. Directories always have '/' as suffix the invalid mount points. Directories always have '/' as suffix
*/ */
func checkMountPoints(containerName string, binds []container.MntDetails) (overlayObjects []string) { func checkMountPoints(containerName string, binds []*warewulfconf.MountEntry) (overlayObjects []string) {
wwlog.Debug("Checking if container %s has paths: %v", containerName, binds)
overlayObjects = []string{} overlayObjects = []string{}
for _, b := range binds { for _, b := range binds {
err, _ := os.Stat(b.Source) _, err := os.Stat(b.Source)
if err == nil { if err != nil {
// no need to create a mount location if source doesn't exist wwlog.Debug("Couldn't stat %s create no mount point in container", b.Source)
continue continue
} }
if _, err := os.Stat(path.Join(container.RootFsDir(containerName), b.Dest)); err != nil { wwlog.Debug("Checking in container for %s", path.Join(container.RootFsDir(containerName), b.Dest))
if _, err = os.Stat(path.Join(container.RootFsDir(containerName), b.Dest)); err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {
if util.IsDir(b.Dest) && !strings.HasSuffix(b.Dest, "/") { if util.IsDir(b.Dest) && !strings.HasSuffix(b.Dest, "/") {
b.Dest += "/" b.Dest += "/"

View File

@@ -1,38 +1,19 @@
package container package container
import ( import (
"io/ioutil"
"path"
"strings" "strings"
"github.com/hpcng/warewulf/internal/pkg/buildconfig" "github.com/hpcng/warewulf/internal/pkg/warewulfconf"
"github.com/hpcng/warewulf/internal/pkg/wwlog" "github.com/hpcng/warewulf/internal/pkg/wwlog"
"gopkg.in/yaml.v2"
) )
/*
Describe a mount point for a container exec
*/
type MntDetails struct {
Dest string `yaml:"Dest"`
Source string `yaml:"Source,omitempty"`
ReadOnly bool `yaml:"Readonly,omitempty" default:"false"`
Options string `yaml:"Options,omitempty"` // ignored at the moment
}
/*
Holds the containers, so that they can be marshalled
*/
type MountPoints struct {
MountPnts []*MntDetails `yaml:"Mount Points"`
}
/* /*
Create a slice iof MntDetails from a string slice with following Create a slice iof MntDetails from a string slice with following
format "source:[:destination][:readonly]" if destination is not format "source:[:destination][:readonly]" if destination is not
given, the source is used as destination given, the source is used as destination
*/ */
func InitMountPnts(binds []string) (mounts []MntDetails) { func InitMountPnts(binds []string) (mounts []*warewulfconf.MountEntry) {
wwlog.Debug("Trying to mount following mount points: %s", mounts)
for _, b := range binds { for _, b := range binds {
bind := strings.Split(b, ":") bind := strings.Split(b, ":")
dest := bind[0] dest := bind[0]
@@ -43,31 +24,12 @@ func InitMountPnts(binds []string) (mounts []MntDetails) {
if len(bind) >= 3 && bind[2] == "ro" { if len(bind) >= 3 && bind[2] == "ro" {
readonly = true readonly = true
} }
mntPnt := MntDetails{ mntPnt := warewulfconf.MountEntry{
Source: bind[0], Source: bind[0],
Dest: dest, Dest: dest,
ReadOnly: readonly, ReadOnly: readonly,
} }
mounts = append(mounts, mntPnt) mounts = append(mounts, &mntPnt)
}
return mounts
}
/*
Read in the default bind mounts from the configuration file
warewulf/mounts.conf
*/
func DefaultMntPts() (mounts []MntDetails) {
data, err := ioutil.ReadFile(path.Join(buildconfig.SYSCONFDIR(), "warewulf/mounts.conf"))
if err != nil {
wwlog.Verbose("No default bind mounts for containers: %s", err)
return mounts
}
wwlog.Debug("Unmarshaling the mounts configuration")
err = yaml.Unmarshal(data, &mounts)
if err != nil {
wwlog.Verbose("Couldn't unmarshall default bind mounts for containers: %s", err)
} }
return mounts return mounts
} }

View File

@@ -7,19 +7,20 @@ import (
) )
type ControllerConf struct { type ControllerConf struct {
WWInternal int `yaml:"WW_INTERNAL"` WWInternal int `yaml:"WW_INTERNAL"`
Comment string `yaml:"comment,omitempty"` Comment string `yaml:"comment,omitempty"`
Ipaddr string `yaml:"ipaddr"` Ipaddr string `yaml:"ipaddr"`
Ipaddr6 string `yaml:"ipaddr6,omitempty"` Ipaddr6 string `yaml:"ipaddr6,omitempty"`
Netmask string `yaml:"netmask"` Netmask string `yaml:"netmask"`
Network string `yaml:"network,omitempty"` Network string `yaml:"network,omitempty"`
Ipv6net string `yaml:"ipv6net,omitempty"` Ipv6net string `yaml:"ipv6net,omitempty"`
Fqdn string `yaml:"fqdn,omitempty"` Fqdn string `yaml:"fqdn,omitempty"`
Warewulf *WarewulfConf `yaml:"warewulf"` Warewulf *WarewulfConf `yaml:"warewulf"`
Dhcp *DhcpConf `yaml:"dhcp"` Dhcp *DhcpConf `yaml:"dhcp"`
Tftp *TftpConf `yaml:"tftp"` Tftp *TftpConf `yaml:"tftp"`
Nfs *NfsConf `yaml:"nfs"` Nfs *NfsConf `yaml:"nfs"`
current bool MountsContainer []*MountEntry `yaml:"container mounts"`
current bool
} }
type WarewulfConf struct { type WarewulfConf struct {
@@ -59,6 +60,16 @@ type NfsExportConf struct {
Mount bool `default:"true" yaml:"mount"` Mount bool `default:"true" yaml:"mount"`
} }
/*
Describe a mount point for a container exec
*/
type MountEntry struct {
Source string `yaml:"source" default:"/etc/resolv.conf"`
Dest string `yaml:"dest,omitempty" default:"/etc/resolv.conf"`
ReadOnly bool `yaml:"readonly,omitempty" default:"false"`
Options string `yaml:"options,omitempty"` // ignored at the moment
}
func (s *NfsConf) Unmarshal(unmarshal func(interface{}) error) error { func (s *NfsConf) Unmarshal(unmarshal func(interface{}) error) error {
if err := defaults.Set(s); err != nil { if err := defaults.Set(s); err != nil {
return err return err

8
mounts.conf Normal file
View File

@@ -0,0 +1,8 @@
Mount Points:
- Dest: /etc/SUSEConnect
Source: /etc/SUSEConnect
- Dest: /etc/SUSEConnect
Source: /etc/SUSEConnect
- Dest: /etc/SUSEConnect
Source: /etc/SUSEConnect

Binary file not shown.