Initial import of the existing CephFS subvolume migration prototype, including the native libcephfs transport, rsync backend, local Ceph lab environment and integration tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
131 lines
3.8 KiB
Go
131 lines
3.8 KiB
Go
package migration
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
type Endpoint struct {
|
|
User string `json:"user,omitempty"`
|
|
Config string `json:"config"`
|
|
Keyring string `json:"keyring"`
|
|
Filesystem string `json:"filesystem"`
|
|
Group string `json:"group"`
|
|
Subvolume string `json:"subvolume"`
|
|
Mount string `json:"mount,omitempty"`
|
|
}
|
|
|
|
type Config struct {
|
|
Backend string `json:"backend,omitempty"`
|
|
Workers int `json:"workers,omitempty"`
|
|
Source Endpoint `json:"source"`
|
|
Target Endpoint `json:"target"`
|
|
BandwidthKiB int64 `json:"bandwidth_kib,omitempty"`
|
|
}
|
|
|
|
func ReadConfig(path string) (Config, error) {
|
|
var c Config
|
|
f, err := os.Open(path)
|
|
if err != nil {
|
|
return c, err
|
|
}
|
|
defer f.Close()
|
|
st, err := f.Stat()
|
|
if err != nil {
|
|
return c, err
|
|
}
|
|
if !st.Mode().IsRegular() || st.Size() > 1<<20 {
|
|
return c, errors.New("config must be a regular file of at most 1 MiB")
|
|
}
|
|
dec := json.NewDecoder(io.LimitReader(f, 1<<20))
|
|
dec.DisallowUnknownFields()
|
|
if err = dec.Decode(&c); err != nil {
|
|
return c, fmt.Errorf("config: %w", err)
|
|
}
|
|
if err = dec.Decode(new(any)); !errors.Is(err, io.EOF) {
|
|
return c, errors.New("config must contain exactly one JSON object")
|
|
}
|
|
return c, c.Validate()
|
|
}
|
|
|
|
func validName(s string) bool {
|
|
return s != "" && s != "." && s != ".." && !strings.HasPrefix(s, "-") &&
|
|
!strings.ContainsAny(s, "/\x00\n\r")
|
|
}
|
|
|
|
func (c Config) Validate() error {
|
|
if c.Backend != "" && c.Backend != "rsync" && c.Backend != "native" {
|
|
return errors.New("backend must be native or rsync")
|
|
}
|
|
if c.Workers < 0 || c.Workers > 64 {
|
|
return errors.New("workers must be between 0 and 64 (0 selects the default)")
|
|
}
|
|
for _, p := range []struct {
|
|
name string
|
|
e Endpoint
|
|
}{{"source", c.Source}, {"target", c.Target}} {
|
|
if p.e.User != "" && (!strings.HasPrefix(p.e.User, "client.") || !validName(p.e.User)) {
|
|
return fmt.Errorf("%s: user must be a client identity", p.name)
|
|
}
|
|
for _, s := range []string{p.e.Filesystem, p.e.Subvolume} {
|
|
if !validName(s) {
|
|
return fmt.Errorf("%s: invalid filesystem or subvolume name", p.name)
|
|
}
|
|
}
|
|
if p.e.Group != "" && !validName(p.e.Group) {
|
|
return fmt.Errorf("%s: invalid group", p.name)
|
|
}
|
|
paths := []string{p.e.Config, p.e.Keyring}
|
|
if c.Backend != "native" || p.e.Mount != "" {
|
|
paths = append(paths, p.e.Mount)
|
|
}
|
|
for _, path := range paths {
|
|
if !filepath.IsAbs(path) || filepath.Clean(path) != path {
|
|
return fmt.Errorf("%s: paths must be absolute and clean: %q", p.name, path)
|
|
}
|
|
}
|
|
}
|
|
if c.BandwidthKiB < 0 || c.BandwidthKiB > (1<<63-1)/1024 {
|
|
return errors.New("bandwidth_kib must be nonnegative and fit in bytes per second")
|
|
}
|
|
if c.Backend != "native" && overlaps(c.Source.Mount, c.Target.Mount) {
|
|
return errors.New("source and target mounts must not overlap")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func beneath(root, path string) bool {
|
|
rel, err := filepath.Rel(root, path)
|
|
return err == nil && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator))
|
|
}
|
|
func overlaps(a, b string) bool { return beneath(a, b) || beneath(b, a) }
|
|
|
|
// Resolve only real directories below the configured filesystem mount. No symlink
|
|
// component is permitted, including in the mount path.
|
|
func dataPath(root, cephPath string) (string, error) {
|
|
if !filepath.IsAbs(cephPath) || filepath.Clean(cephPath) != cephPath || cephPath == "/" {
|
|
return "", fmt.Errorf("invalid Ceph path %q", cephPath)
|
|
}
|
|
p := filepath.Join(root, strings.TrimPrefix(cephPath, "/"))
|
|
resolved, err := filepath.EvalSymlinks(p)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if resolved != p || !beneath(root, p) {
|
|
return "", fmt.Errorf("path escapes mount or contains symlinks: %q", p)
|
|
}
|
|
st, err := os.Stat(p)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if !st.IsDir() {
|
|
return "", fmt.Errorf("not a directory: %s", p)
|
|
}
|
|
return p, nil
|
|
}
|