2021-07-26 11:51:19 +00:00
|
|
|
package mounter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-04-26 08:47:00 +00:00
|
|
|
"strings"
|
2023-03-02 13:12:41 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
systemd "github.com/coreos/go-systemd/v22/dbus"
|
|
|
|
dbus "github.com/godbus/dbus/v5"
|
2023-03-06 21:41:41 +00:00
|
|
|
"github.com/golang/glog"
|
2021-07-26 11:51:19 +00:00
|
|
|
|
2021-08-24 10:33:26 +00:00
|
|
|
"github.com/yandex-cloud/k8s-csi-s3/pkg/s3"
|
2021-07-26 11:51:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
geesefsCmd = "geesefs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Implements Mounter
|
|
|
|
type geesefsMounter struct {
|
|
|
|
meta *s3.FSMeta
|
|
|
|
endpoint string
|
|
|
|
region string
|
|
|
|
accessKeyID string
|
|
|
|
secretAccessKey string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newGeeseFSMounter(meta *s3.FSMeta, cfg *s3.Config) (Mounter, error) {
|
|
|
|
return &geesefsMounter{
|
|
|
|
meta: meta,
|
|
|
|
endpoint: cfg.Endpoint,
|
2021-07-27 10:53:34 +00:00
|
|
|
region: cfg.Region,
|
2021-07-26 11:51:19 +00:00
|
|
|
accessKeyID: cfg.AccessKeyID,
|
|
|
|
secretAccessKey: cfg.SecretAccessKey,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2023-03-02 13:12:41 +00:00
|
|
|
func (geesefs *geesefsMounter) CopyBinary(from, to string) error {
|
|
|
|
st, err := os.Stat(from)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to stat %s: %v", from, err)
|
|
|
|
}
|
|
|
|
st2, err := os.Stat(to)
|
|
|
|
if err != nil && !os.IsNotExist(err) {
|
|
|
|
return fmt.Errorf("Failed to stat %s: %v", to, err)
|
|
|
|
}
|
|
|
|
if err != nil || st2.Size() != st.Size() || st2.ModTime() != st.ModTime() {
|
|
|
|
if err == nil {
|
|
|
|
// remove the file first to not hit "text file busy" errors
|
|
|
|
err = os.Remove(to)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error removing %s to update it: %v", to, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bin, err := os.ReadFile(from)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error copying %s to %s: %v", from, to, err)
|
|
|
|
}
|
|
|
|
err = os.WriteFile(to, bin, 0755)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error copying %s to %s: %v", from, to, err)
|
|
|
|
}
|
|
|
|
err = os.Chtimes(to, st.ModTime(), st.ModTime())
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error copying %s to %s: %v", from, to, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (geesefs *geesefsMounter) MountDirect(target string, args []string) error {
|
|
|
|
args = append([]string{
|
2021-07-26 11:51:19 +00:00
|
|
|
"--endpoint", geesefs.endpoint,
|
|
|
|
"-o", "allow_other",
|
2021-09-07 10:37:04 +00:00
|
|
|
"--log-file", "/dev/stderr",
|
2023-03-02 13:12:41 +00:00
|
|
|
}, args...)
|
2023-06-03 07:36:03 +00:00
|
|
|
envs := []string{
|
|
|
|
"AWS_ACCESS_KEY_ID=" + geesefs.accessKeyID,
|
|
|
|
"AWS_SECRET_ACCESS_KEY=" + geesefs.secretAccessKey,
|
|
|
|
}
|
|
|
|
return fuseMount(target, geesefsCmd, args, envs)
|
2023-03-02 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 10:02:32 +00:00
|
|
|
type execCmd struct {
|
|
|
|
Path string
|
|
|
|
Args []string
|
|
|
|
UncleanIsFailure bool
|
|
|
|
}
|
|
|
|
|
2023-03-06 21:41:41 +00:00
|
|
|
func (geesefs *geesefsMounter) Mount(target, volumeID string) error {
|
2023-03-02 13:12:41 +00:00
|
|
|
fullPath := fmt.Sprintf("%s:%s", geesefs.meta.BucketName, geesefs.meta.Prefix)
|
|
|
|
var args []string
|
2021-09-07 10:37:04 +00:00
|
|
|
if geesefs.region != "" {
|
|
|
|
args = append(args, "--region", geesefs.region)
|
2021-07-26 11:51:19 +00:00
|
|
|
}
|
2023-03-04 10:03:58 +00:00
|
|
|
args = append(
|
|
|
|
args,
|
|
|
|
"--setuid", "65534", // nobody. drop root privileges
|
|
|
|
"--setgid", "65534", // nogroup
|
|
|
|
)
|
2023-03-02 13:12:41 +00:00
|
|
|
useSystemd := true
|
|
|
|
for i := 0; i < len(geesefs.meta.MountOptions); i++ {
|
2023-04-26 08:47:00 +00:00
|
|
|
opt := geesefs.meta.MountOptions[i]
|
|
|
|
if opt == "--no-systemd" {
|
2023-03-02 13:12:41 +00:00
|
|
|
useSystemd = false
|
2023-04-26 08:47:00 +00:00
|
|
|
} else if len(opt) > 0 && opt[0] == '-' {
|
|
|
|
// Remove unsafe options
|
|
|
|
s := 1
|
|
|
|
if len(opt) > 1 && opt[1] == '-' {
|
|
|
|
s++
|
|
|
|
}
|
|
|
|
key := opt[s:]
|
|
|
|
e := strings.Index(opt, "=")
|
|
|
|
if e >= 0 {
|
|
|
|
key = opt[s:e]
|
|
|
|
}
|
|
|
|
if key == "log-file" || key == "shared-config" || key == "cache" {
|
|
|
|
// Skip options accessing local FS
|
|
|
|
if e < 0 {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
} else if key != "" {
|
|
|
|
args = append(args, opt)
|
|
|
|
}
|
|
|
|
} else if len(opt) > 0 {
|
|
|
|
args = append(args, opt)
|
2023-03-02 13:12:41 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-27 10:49:43 +00:00
|
|
|
args = append(args, fullPath, target)
|
2023-03-02 13:12:41 +00:00
|
|
|
// Try to start geesefs using systemd so it doesn't get killed when the container exits
|
|
|
|
if !useSystemd {
|
|
|
|
return geesefs.MountDirect(target, args)
|
|
|
|
}
|
|
|
|
conn, err := systemd.New()
|
|
|
|
if err != nil {
|
2023-03-06 21:41:41 +00:00
|
|
|
glog.Errorf("Failed to connect to systemd dbus service: %v, starting geesefs directly", err)
|
2023-03-02 13:12:41 +00:00
|
|
|
return geesefs.MountDirect(target, args)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
// systemd is present
|
|
|
|
if err = geesefs.CopyBinary("/usr/bin/geesefs", "/csi/geesefs"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pluginDir := os.Getenv("PLUGIN_DIR")
|
|
|
|
if pluginDir == "" {
|
|
|
|
pluginDir = "/var/lib/kubelet/plugins/ru.yandex.s3.csi"
|
|
|
|
}
|
|
|
|
args = append([]string{pluginDir+"/geesefs", "-f", "-o", "allow_other", "--endpoint", geesefs.endpoint}, args...)
|
2023-04-26 08:47:00 +00:00
|
|
|
glog.Info("Starting geesefs using systemd: "+strings.Join(args, " "))
|
2023-03-02 13:12:41 +00:00
|
|
|
unitName := "geesefs-"+systemd.PathBusEscape(volumeID)+".service"
|
2023-03-06 21:41:41 +00:00
|
|
|
newProps := []systemd.Property{
|
2023-03-02 13:12:41 +00:00
|
|
|
systemd.Property{
|
|
|
|
Name: "Description",
|
|
|
|
Value: dbus.MakeVariant("GeeseFS mount for Kubernetes volume "+volumeID),
|
|
|
|
},
|
|
|
|
systemd.PropExecStart(args, false),
|
2023-05-23 10:02:32 +00:00
|
|
|
systemd.Property{
|
|
|
|
Name: "ExecStopPost",
|
|
|
|
// force & lazy unmount to cleanup possibly dead mountpoints
|
|
|
|
Value: dbus.MakeVariant([]execCmd{ execCmd{ "/bin/umount", []string{ "/bin/umount", "-f", "-l", target }, false } }),
|
|
|
|
},
|
2023-03-02 13:12:41 +00:00
|
|
|
systemd.Property{
|
|
|
|
Name: "Environment",
|
|
|
|
Value: dbus.MakeVariant([]string{ "AWS_ACCESS_KEY_ID="+geesefs.accessKeyID, "AWS_SECRET_ACCESS_KEY="+geesefs.secretAccessKey }),
|
|
|
|
},
|
|
|
|
systemd.Property{
|
|
|
|
Name: "CollectMode",
|
|
|
|
Value: dbus.MakeVariant("inactive-or-failed"),
|
|
|
|
},
|
|
|
|
}
|
2023-03-06 21:41:41 +00:00
|
|
|
unitProps, err := conn.GetAllProperties(unitName)
|
|
|
|
if err == nil {
|
|
|
|
// Unit already exists
|
|
|
|
if s, ok := unitProps["ActiveState"].(string); ok && (s == "active" || s == "activating" || s == "reloading") {
|
|
|
|
// Unit is already active
|
|
|
|
curPath := ""
|
|
|
|
prevExec, ok := unitProps["ExecStart"].([][]interface{})
|
|
|
|
if ok && len(prevExec) > 0 && len(prevExec[0]) >= 2 {
|
|
|
|
execArgs, ok := prevExec[0][1].([]string)
|
|
|
|
if ok && len(execArgs) >= 2 {
|
|
|
|
curPath = execArgs[len(execArgs)-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if curPath != target {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"GeeseFS for volume %v is already mounted on host, but"+
|
|
|
|
" in a different directory. We want %v, but it's in %v",
|
|
|
|
volumeID, target, curPath,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
// Already mounted at right location
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
// Stop and garbage collect the unit if automatic collection didn't work for some reason
|
|
|
|
conn.StopUnit(unitName, "replace", nil)
|
|
|
|
conn.ResetFailedUnit(unitName)
|
|
|
|
}
|
2023-03-02 13:12:41 +00:00
|
|
|
}
|
2023-03-06 21:41:41 +00:00
|
|
|
_, err = conn.StartTransientUnit(unitName, "replace", newProps, nil)
|
2023-03-02 13:12:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error starting systemd unit %s on host: %v", unitName, err)
|
|
|
|
}
|
|
|
|
return waitForMount(target, 10*time.Second)
|
2021-07-26 11:51:19 +00:00
|
|
|
}
|