Separate top-level declarations and multi-line statements with blank lines, keeping an "if err != nil" check next to the statement it belongs to. Expand semicolon one-liners, group the nativeTransport fields by purpose, and format the cgo preamble as readable C. Apart from the expanded one-liners and the C preamble, the change is whitespace only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
209 lines
5.0 KiB
Go
209 lines
5.0 KiB
Go
package migration
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
"strings"
|
|
"syscall"
|
|
|
|
"git.zb-server.de/Sebi/CephfsMig/internal/storage"
|
|
)
|
|
|
|
type nativeAccess struct{ backend *nativeTransport }
|
|
|
|
func (a nativeAccess) Inspect(ctx context.Context, c Config, x Executor) (*Inspection, error) {
|
|
if err := c.Validate(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
sf, si, err := queryEndpoint(ctx, ceph{endpoint: c.Source, exec: x, native: true})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
tf, ti, err := queryEndpoint(ctx, ceph{endpoint: c.Target, exec: x, native: true})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if sf == tf {
|
|
return nil, errors.New("native source and target must be different clusters")
|
|
}
|
|
|
|
for _, p := range []struct {
|
|
fs storage.FS
|
|
path, fsid string
|
|
}{{a.backend.source, si.Path, sf}, {a.backend.target, ti.Path, tf}} {
|
|
if _, err := nativePath(p.path); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := validateNativeDirectory(p.fs, p.path); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
id, err := p.fs.GetXattr(p.path, "ceph.cluster_fsid")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if string(id) != p.fsid {
|
|
return nil, errors.New("native connection belongs to the wrong cluster")
|
|
}
|
|
}
|
|
|
|
names, err := (ceph{endpoint: c.Source, exec: x, native: true}).names(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fingerprint := sha256.Sum256([]byte(sf + "\x00" + tf + "\x00" + si.Path + "\x00" + ti.Path))
|
|
a.backend.identity = hex.EncodeToString(fingerprint[:])
|
|
return &Inspection{SourceFSID: sf, TargetFSID: tf, SourcePath: si.Path, TargetPath: ti.Path,
|
|
SourceBytes: si.BytesUsed, SourceSnapshots: names, Notes: []string{SnapshotIdentityNote}}, nil
|
|
}
|
|
|
|
func (a nativeAccess) Empty(ctx context.Context, p string) error {
|
|
if err := ctx.Err(); err != nil {
|
|
return err
|
|
}
|
|
|
|
d, err := a.backend.target.OpenDir(p)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
defer d.Close()
|
|
_, err = d.Next()
|
|
if errors.Is(err, io.EOF) {
|
|
return nil
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return errors.New("new native migration requires an empty target subvolume")
|
|
}
|
|
|
|
func (a nativeAccess) Claim(ctx context.Context, c Config, fsid, id string) (io.Closer, error) {
|
|
if err := ctx.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fs := a.backend.target
|
|
current := "/"
|
|
for _, component := range []string{".cephmig", "claims"} {
|
|
current = path.Join(current, component)
|
|
n, err := fs.Stat(current)
|
|
if errors.Is(err, syscall.ENOENT) {
|
|
if err = fs.Mkdir(current, 0700); err != nil && !errors.Is(err, syscall.EEXIST) {
|
|
return nil, err
|
|
}
|
|
|
|
n, err = fs.Stat(current)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if n.Kind() != storage.Directory {
|
|
return nil, errors.New("native claim directory contains a symlink or non-directory")
|
|
}
|
|
}
|
|
|
|
sum := sha256.Sum256([]byte(fsid + "\x00" + c.Target.Filesystem + "\x00" + c.Target.Group + "\x00" + c.Target.Subvolume))
|
|
p := path.Join(current, hex.EncodeToString(sum[:]))
|
|
f, err := fs.Open(p, syscall.O_CREAT|syscall.O_EXCL|syscall.O_RDWR|syscall.O_NOFOLLOW, 0600)
|
|
if err == nil {
|
|
if err = writeNativeAt(f, []byte(id+"\n"), 0); err == nil {
|
|
err = f.Sync()
|
|
}
|
|
|
|
closeErr := f.Close()
|
|
if err == nil {
|
|
err = closeErr
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
} else if !errors.Is(err, syscall.EEXIST) {
|
|
return nil, err
|
|
}
|
|
|
|
n, err := fs.Stat(p)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if n.Kind() != storage.Regular {
|
|
return nil, errors.New("native target claim is not a regular file")
|
|
}
|
|
|
|
f, err = fs.Open(p, syscall.O_RDWR|syscall.O_NOFOLLOW, 0)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err = f.Lock(1); err != nil {
|
|
f.Close()
|
|
return nil, fmt.Errorf("native target is locked: %w", err)
|
|
}
|
|
|
|
b := make([]byte, 256)
|
|
count, readErr := f.ReadAt(b, 0)
|
|
if readErr != nil && !errors.Is(readErr, io.EOF) {
|
|
f.Close()
|
|
return nil, readErr
|
|
}
|
|
|
|
if string(b[:count]) != id+"\n" {
|
|
f.Close()
|
|
return nil, errors.New("native target belongs to a different job")
|
|
}
|
|
|
|
return f, nil
|
|
}
|
|
|
|
func (a nativeAccess) Prepare(ctx context.Context, _, target, _ string) error {
|
|
return clearNativeQuotas(ctx, a.backend.target, target)
|
|
}
|
|
|
|
func (a nativeAccess) Apply(context.Context, string, string) error { return a.backend.target.Sync() }
|
|
func (a nativeAccess) Verify(context.Context, string, string, string) error { return nil } // Transport verifies all native metadata and links.
|
|
|
|
type nativeVerifyIndex struct {
|
|
index *linkIndex
|
|
dir string
|
|
}
|
|
|
|
func osNativeVerifyIndex(parent string) (*nativeVerifyIndex, error) {
|
|
dir, err := os.MkdirTemp(parent, "verify-")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
index, err := openLinkIndex(filepath.Join(dir, "links.bolt"))
|
|
if err != nil {
|
|
os.RemoveAll(dir)
|
|
return nil, err
|
|
}
|
|
|
|
return &nativeVerifyIndex{index: index, dir: dir}, nil
|
|
}
|
|
|
|
func (i *nativeVerifyIndex) cleanup() {
|
|
_ = i.index.close()
|
|
_ = os.RemoveAll(i.dir)
|
|
}
|
|
|
|
func options(ep Endpoint) storage.Options {
|
|
return storage.Options{Config: ep.Config, Keyring: ep.Keyring, User: strings.TrimPrefix(ep.User, "client."), Filesystem: ep.Filesystem}
|
|
}
|