diff --git a/config/config.go b/config/config.go index 87926aa6..26ba1fd2 100644 --- a/config/config.go +++ b/config/config.go @@ -82,7 +82,7 @@ func NewConfig() *Config { ImagesPattern: "images*.tar", StateRequired: false, StateDev: "LABEL=RANCHER_STATE", - StateDevFSType: "ext4", + StateDevFSType: "auto", SysInit: "/sbin/init-sys", SystemDockerArgs: []string{"docker", "-d", "-s", "overlay", "-b", "none"}, UserInit: "/sbin/init-user", diff --git a/init/init.go b/init/init.go index 0f2426b4..5025d979 100644 --- a/init/init.go +++ b/init/init.go @@ -213,12 +213,31 @@ func MainInit() { func mountState(cfg *config.Config) error { var err error - if len(cfg.StateDev) == 0 { - log.Debugf("State will not be persisted") - err = util.Mount("none", STATE, "tmpfs", "") - } else { - log.Debugf("Mounting state device %s", cfg.StateDev) - err = util.Mount(cfg.StateDev, STATE, cfg.StateDevFSType, "") + + dev := util.ResolveDevice(cfg.StateDev) + log.Debugf("Mounting state device %s", dev) + + fsType := cfg.StateDevFSType + log.Debugf("FsType has been set to %s", fsType) + if fsType == "auto" { + actualFsType, fsErr := util.GetFsType(dev) + if fsErr != nil { + return fsErr + } + fsType = actualFsType + } + err = util.Mount(dev, STATE, fsType, "") + + if err != nil { + if cfg.StateRequired { + return err + } else { + log.Debugf("State will not be persisted") + err = util.Mount("none", STATE, "tmpfs", "") + if err != nil { + return err + } + } } if err != nil { diff --git a/util/cutil.go b/util/cutil.go index 6c77b3b8..1261b44a 100644 --- a/util/cutil.go +++ b/util/cutil.go @@ -1,16 +1,41 @@ package util /* -#cgo LDFLAGS: -lblkid -luuid +#cgo LDFLAGS: -lmount -lblkid -luuid #include +#include #include */ import "C" import "unsafe" +import ( + "errors" +) + func ResolveDevice(spec string) string { - cString := C.blkid_evaluate_spec(C.CString(spec), nil) + cSpec := C.CString(spec) + defer C.free(unsafe.Pointer(cSpec)) + cString := C.blkid_evaluate_spec(cSpec, nil) defer C.free(unsafe.Pointer(cString)) return C.GoString(cString) } +func GetFsType(device string) (string, error) { + var ambi * C.int + cDevice := C.CString(device) + defer C.free(unsafe.Pointer(cDevice)) + cString := C.mnt_get_fstype(cDevice, ambi, nil) + defer C.free(unsafe.Pointer(cString)) + if cString != nil { + return C.GoString(cString), nil + } + return "", errors.New("Error while getting fstype") +} + +func intToBool(value C.int) bool { + if value == 0 { + return false + } + return true +}