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
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
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 update_configuration
rm -f print_defaults
rm -f print_mnts
rm -f etc/wwapi{c,d,rd}.conf
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/container"
"github.com/hpcng/warewulf/internal/pkg/util"
"github.com/hpcng/warewulf/internal/pkg/warewulfconf"
"github.com/hpcng/warewulf/internal/pkg/wwlog"
"github.com/pkg/errors"
"github.com/spf13/cobra"
@@ -31,8 +32,11 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
wwlog.Error("Unknown Warewulf container: %s", containerName)
os.Exit(1)
}
var err error
mountPts := container.DefaultMntPts()
conf, err := warewulfconf.New()
if err != nil {
wwlog.Verbose("Couldn't get warewulf ocnfiguration: %s", err)
}
mountPts := conf.MountsContainer
mountPts = append(container.InitMountPnts(binds), mountPts...)
// check for valid mount points
lowerObjects := checkMountPoints(containerName, mountPts)
@@ -105,25 +109,21 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
return errors.Wrap(err, "failed to mount /dev")
}
for _, b := range binds {
var source string
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, "")
for _, mntPnt := range mountPts {
err = syscall.Mount(mntPnt.Source, path.Join(containerPath, mntPnt.Dest), "", syscall.MS_BIND, "")
if err != nil {
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)
@@ -156,16 +156,16 @@ func CobraRunE(cmd *cobra.Command, args []string) error {
Check if the bind mount points exists in the given container. Returns
the invalid mount points. Directories always have '/' as suffix
*/
func checkMountPoints(containerName string, binds []container.MntDetails) (overlayObjects []string) {
wwlog.Debug("Checking if container %s has paths: %v", containerName, binds)
func checkMountPoints(containerName string, binds []*warewulfconf.MountEntry) (overlayObjects []string) {
overlayObjects = []string{}
for _, b := range binds {
err, _ := os.Stat(b.Source)
if err == nil {
// no need to create a mount location if source doesn't exist
_, err := os.Stat(b.Source)
if err != nil {
wwlog.Debug("Couldn't stat %s create no mount point in container", b.Source)
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 util.IsDir(b.Dest) && !strings.HasSuffix(b.Dest, "/") {
b.Dest += "/"

View File

@@ -1,38 +1,19 @@
package container
import (
"io/ioutil"
"path"
"strings"
"github.com/hpcng/warewulf/internal/pkg/buildconfig"
"github.com/hpcng/warewulf/internal/pkg/warewulfconf"
"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
format "source:[:destination][:readonly]" if destination is not
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 {
bind := strings.Split(b, ":")
dest := bind[0]
@@ -43,31 +24,12 @@ func InitMountPnts(binds []string) (mounts []MntDetails) {
if len(bind) >= 3 && bind[2] == "ro" {
readonly = true
}
mntPnt := MntDetails{
mntPnt := warewulfconf.MountEntry{
Source: bind[0],
Dest: dest,
ReadOnly: readonly,
}
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)
mounts = append(mounts, &mntPnt)
}
return mounts
}

View File

@@ -7,19 +7,20 @@ import (
)
type ControllerConf struct {
WWInternal int `yaml:"WW_INTERNAL"`
Comment string `yaml:"comment,omitempty"`
Ipaddr string `yaml:"ipaddr"`
Ipaddr6 string `yaml:"ipaddr6,omitempty"`
Netmask string `yaml:"netmask"`
Network string `yaml:"network,omitempty"`
Ipv6net string `yaml:"ipv6net,omitempty"`
Fqdn string `yaml:"fqdn,omitempty"`
Warewulf *WarewulfConf `yaml:"warewulf"`
Dhcp *DhcpConf `yaml:"dhcp"`
Tftp *TftpConf `yaml:"tftp"`
Nfs *NfsConf `yaml:"nfs"`
current bool
WWInternal int `yaml:"WW_INTERNAL"`
Comment string `yaml:"comment,omitempty"`
Ipaddr string `yaml:"ipaddr"`
Ipaddr6 string `yaml:"ipaddr6,omitempty"`
Netmask string `yaml:"netmask"`
Network string `yaml:"network,omitempty"`
Ipv6net string `yaml:"ipv6net,omitempty"`
Fqdn string `yaml:"fqdn,omitempty"`
Warewulf *WarewulfConf `yaml:"warewulf"`
Dhcp *DhcpConf `yaml:"dhcp"`
Tftp *TftpConf `yaml:"tftp"`
Nfs *NfsConf `yaml:"nfs"`
MountsContainer []*MountEntry `yaml:"container mounts"`
current bool
}
type WarewulfConf struct {
@@ -59,6 +60,16 @@ type NfsExportConf struct {
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 {
if err := defaults.Set(s); err != nil {
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.