Users of a subvolume should not see traces of the migration. Each sync round now deletes the older cephmig-* round snapshots at source and target and keeps only the newest one as base for the next round. The new command `cephmig cleanup` deletes the remaining one after the cutover. Application snapshots are never touched. The journal marks a snapshot as removed before deleting it, so an interrupted run finishes the deletion instead of reporting a vanished snapshot. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
221 lines
10 KiB
Bash
221 lines
10 KiB
Bash
#!/usr/bin/env bash
|
|
set -Eeuo pipefail
|
|
mountpoint -q /mnt/source-fs
|
|
mountpoint -q /mnt/target-fs
|
|
id=$(cat /proc/sys/kernel/random/uuid)
|
|
name="itest-$id"
|
|
mkdir -p /state/test-runs
|
|
work=$(mktemp -d /state/test-runs/cephmig-itest.XXXXXX)
|
|
state="/state/$name.json"
|
|
config="$work/config.json"
|
|
binary=/workspace/bin/cephmig
|
|
ceph_real=$(command -v ceph)
|
|
rsync_real=$(command -v rsync)
|
|
source_ceph() { "$ceph_real" -c /clusters/source/ceph.conf -k /clusters/source/ceph.client.admin.keyring "$@"; }
|
|
target_ceph() { "$ceph_real" -c /clusters/target/ceph.conf -k /clusters/target/ceph.client.admin.keyring "$@"; }
|
|
completed=false
|
|
cleanup() {
|
|
if [[ $completed != true ]]; then
|
|
echo "FAIL: retained fixtures subvolume=$name state=$state config=$config" >&2
|
|
return
|
|
fi
|
|
for cluster in source target; do
|
|
helper="$cluster"_ceph
|
|
while IFS= read -r snap; do
|
|
"$helper" fs subvolume snapshot rm cephfs "$name" "$snap" --group_name migration
|
|
done < <("$helper" fs subvolume snapshot ls cephfs "$name" --group_name migration |
|
|
python3 -c 'import json,sys; print("\n".join(x["name"] for x in json.load(sys.stdin)))')
|
|
"$helper" fs subvolume rm cephfs "$name" --group_name migration --force
|
|
done
|
|
target_fsid=$(target_ceph fsid)
|
|
claim=$(python3 - "$target_fsid" "$name" <<'PY'
|
|
import hashlib, sys
|
|
print(hashlib.sha256("\0".join([sys.argv[1], "cephfs", "migration", sys.argv[2]]).encode()).hexdigest())
|
|
PY
|
|
)
|
|
rm -f "/mnt/target-fs/.cephmig/claims/$claim" "$state" "$state.lock" "$state.second" "$state.second.lock"
|
|
rm -rf "$work"
|
|
}
|
|
trap cleanup EXIT
|
|
source_ceph fs subvolume create cephfs "$name" --group_name migration
|
|
target_ceph fs subvolume create cephfs "$name" --group_name migration
|
|
src="/mnt/source-fs$(source_ceph fs subvolume getpath cephfs "$name" --group_name migration)"
|
|
dst="/mnt/target-fs$(target_ceph fs subvolume getpath cephfs "$name" --group_name migration)"
|
|
python3 - /lab/migration.example.json "$config" "$name" <<'PY'
|
|
import json,sys
|
|
c=json.load(open(sys.argv[1]))
|
|
c["backend"]="rsync" # Retained as an explicit regression/reference backend.
|
|
c["source"]["subvolume"]=c["target"]["subvolume"]=sys.argv[3]
|
|
with open(sys.argv[2],"w") as f: json.dump(c,f)
|
|
PY
|
|
python3 - "$src" <<'PY'
|
|
import os, struct, sys
|
|
root=sys.argv[1]
|
|
os.mkdir(root+"/d")
|
|
with open(root+"/data","wb") as f: f.write(b"version-A")
|
|
os.chmod(root+"/data",0o640)
|
|
os.chown(root+"/data",12345,23456)
|
|
os.link(root+"/data",root+"/d/hardlink")
|
|
os.symlink("../data",root+"/d/symlink")
|
|
os.symlink("nonexistent",root+"/dangling")
|
|
os.symlink("/etc/passwd",root+"/external")
|
|
os.mkfifo(root+"/fifo",0o600)
|
|
# A separate link group will be split in a later snapshot without changing bytes.
|
|
with open(root+"/split-a","wb") as f: f.write(b"unchanged")
|
|
os.link(root+"/split-a",root+"/d/split-b")
|
|
with open(root+"/sparse","wb") as f: f.truncate(16*1024*1024)
|
|
with open(root+"/deleted-later","w") as f: f.write("historical")
|
|
with open(root+"/name with spaces\nand newline","w") as f: f.write("name")
|
|
os.setxattr(root+"/data","user.cephmig",b"preserve")
|
|
entries=[(1,6,0xffffffff),(2,4,12345),(4,0,0xffffffff),(16,4,0xffffffff),(32,0,0xffffffff)]
|
|
acl=struct.pack("<I",2)+b"".join(struct.pack("<HHI",*e) for e in entries)
|
|
os.setxattr(root+"/data","system.posix_acl_access",acl)
|
|
os.setxattr(root+"/d","system.posix_acl_default",acl)
|
|
os.setxattr(root+"/d","ceph.quota.max_bytes",b"33554432")
|
|
os.setxattr(root+"/d","ceph.quota.max_files",b"1000")
|
|
os.setxattr(root,"ceph.quota.max_bytes",b"134217728")
|
|
PY
|
|
source_ceph fs subvolume metadata set cephfs "$name" owner --value="-value with spaces" --group_name migration
|
|
source_ceph fs subvolume snapshot create cephfs "$name" history-1 --group_name migration
|
|
source_ceph fs subvolume snapshot metadata set cephfs "$name" history-1 app first --group_name migration
|
|
python3 - "$src" <<'PY'
|
|
import os,sys
|
|
p=sys.argv[1]+"/data"
|
|
st=os.stat(p)
|
|
with open(p,"wb") as f: f.write(b"version-B")
|
|
os.utime(p,ns=(st.st_atime_ns,st.st_mtime_ns))
|
|
os.unlink(sys.argv[1]+"/deleted-later")
|
|
split=sys.argv[1]+"/d/split-b"
|
|
st=os.stat(split)
|
|
attrs={k:os.getxattr(split,k) for k in os.listxattr(split) if not k.startswith("ceph.")}
|
|
os.unlink(split)
|
|
with open(split,"wb") as f: f.write(b"unchanged")
|
|
for k in os.listxattr(split):
|
|
if k not in attrs and not k.startswith("ceph."): os.removexattr(split,k)
|
|
for k,v in attrs.items(): os.setxattr(split,k,v)
|
|
os.chmod(split,st.st_mode & 0o7777)
|
|
os.utime(split,ns=(st.st_atime_ns,st.st_mtime_ns))
|
|
os.setxattr(sys.argv[1]+"/d","ceph.quota.max_bytes",b"50331648")
|
|
os.setxattr(sys.argv[1],"ceph.quota.max_bytes",b"201326592")
|
|
PY
|
|
source_ceph fs subvolume snapshot create cephfs "$name" history-2 --group_name migration
|
|
source_ceph fs subvolume snapshot metadata set cephfs "$name" history-2 app second --group_name migration
|
|
printf 'version-C' > "$src/data"
|
|
python3 - "$src" <<'PY'
|
|
import os,sys
|
|
os.setxattr(sys.argv[1]+"/d","ceph.quota.max_files",b"2000")
|
|
PY
|
|
"$binary" inspect --config "$config" > "$work/inspection.json"
|
|
|
|
mkdir "$work/bin"
|
|
cat > "$work/bin/rsync" <<'SH'
|
|
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
if [[ -f $CEPHMIG_FAIL_COPY_ONCE && " $* " != *" --dry-run "* ]]; then
|
|
rm "$CEPHMIG_FAIL_COPY_ONCE"
|
|
"$CEPHMIG_REAL_RSYNC" "$@"
|
|
exit 23
|
|
fi
|
|
exec "$CEPHMIG_REAL_RSYNC" "$@"
|
|
SH
|
|
cat > "$work/bin/ceph" <<'SH'
|
|
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
if [[ -f $CEPHMIG_CRASH_ONCE &&
|
|
" $* " == *"/clusters/target/ceph.conf"* &&
|
|
" $* " == *" fs subvolume snapshot create "* ]]; then
|
|
"$CEPHMIG_REAL_CEPH" "$@"
|
|
rm "$CEPHMIG_CRASH_ONCE"
|
|
kill -KILL "$PPID"
|
|
exit 0
|
|
fi
|
|
exec "$CEPHMIG_REAL_CEPH" "$@"
|
|
SH
|
|
chmod +x "$work/bin/rsync" "$work/bin/ceph"
|
|
export CEPHMIG_REAL_RSYNC="$rsync_real" CEPHMIG_REAL_CEPH="$ceph_real"
|
|
export CEPHMIG_FAIL_COPY_ONCE="$work/fail-copy" CEPHMIG_CRASH_ONCE="$work/crash"
|
|
touch "$CEPHMIG_FAIL_COPY_ONCE"
|
|
if PATH="$work/bin:$PATH" "$binary" sync --config "$config" --state "$state" > "$work/interrupted.json"; then
|
|
echo 'expected injected transfer failure' >&2; exit 1
|
|
fi
|
|
python3 - "$state" <<'PY'
|
|
import json,sys
|
|
s=json.load(open(sys.argv[1]))
|
|
assert s["phase"]=="syncing" and not s["snapshots"][0]["done"]
|
|
PY
|
|
touch "$CEPHMIG_CRASH_ONCE"
|
|
if PATH="$work/bin:$PATH" "$binary" sync --config "$config" --state "$state" > "$work/crashed.json"; then
|
|
echo 'expected injected process crash' >&2; exit 1
|
|
fi
|
|
test ! -f "$CEPHMIG_CRASH_ONCE"
|
|
"$binary" sync --config "$config" --state "$state" > "$work/synced.json"
|
|
for snap in history-1 history-2; do
|
|
path=$(target_ceph fs subvolume snapshot getpath cephfs "$name" "$snap" migration)
|
|
value=$(cat "/mnt/target-fs$path/data")
|
|
if [[ $snap = history-1 ]]; then test "$value" = version-A; else test "$value" = version-B; fi
|
|
python3 - "/mnt/target-fs$path" "$snap" <<'PY'
|
|
import os,sys
|
|
root,snap=sys.argv[1:3]
|
|
linked=os.stat(root+"/split-a").st_ino==os.stat(root+"/d/split-b").st_ino
|
|
assert linked==(snap=="history-1"), (snap,linked)
|
|
PY
|
|
done
|
|
test "$(cat "$dst/data")" = version-C
|
|
if "$binary" sync --config "$config" --state "$state.second" > "$work/stolen.json"; then
|
|
echo 'second job adopted existing target' >&2; exit 1
|
|
fi
|
|
printf 'version-D' > "$src/data"
|
|
printf extra > "$dst/obsolete"
|
|
source_ceph fs subvolume metadata rm cephfs "$name" owner --group_name migration
|
|
source_ceph fs subvolume metadata set cephfs "$name" project new-value --group_name migration
|
|
"$binary" sync --config "$config" --state "$state" > "$work/caught-up.json"
|
|
test "$(cat "$dst/data")" = version-D
|
|
test ! -e "$dst/obsolete"
|
|
if "$binary" finalize --config "$config" --state "$state" > "$work/refused.json"; then
|
|
echo 'finalization accepted missing writer-stop acknowledgement' >&2; exit 1
|
|
fi
|
|
"$binary" finalize --config "$config" --state "$state" --writers-stopped > "$work/final.json"
|
|
"$binary" status --state "$state" > "$work/status.json"
|
|
"$binary" finalize --config "$config" --state "$state" --writers-stopped > "$work/idempotent.json"
|
|
python3 - "$src" "$dst" "$work/status.json" "$work/idempotent.json" <<'PY'
|
|
import json,os,stat,sys
|
|
src,dst=sys.argv[1:3]
|
|
s=json.load(open(sys.argv[3])); again=json.load(open(sys.argv[4]))
|
|
assert s==again and s["phase"]=="ready-for-cutover"
|
|
assert s["notes"] and "birthtimes" in s["notes"][0]
|
|
assert all(x["done"] for x in s["snapshots"])
|
|
assert len(s["snapshots"])==5, s["snapshots"]
|
|
assert os.stat(dst+"/data").st_uid==12345 and os.stat(dst+"/data").st_gid==23456
|
|
assert os.stat(dst+"/data").st_ino==os.stat(dst+"/d/hardlink").st_ino
|
|
assert os.stat(dst+"/split-a").st_ino!=os.stat(dst+"/d/split-b").st_ino
|
|
assert stat.S_ISFIFO(os.lstat(dst+"/fifo").st_mode)
|
|
assert os.readlink(dst+"/external")=="/etc/passwd"
|
|
for rel,key in [("data","user.cephmig"),("data","system.posix_acl_access"),
|
|
("d","system.posix_acl_default"),("d","ceph.quota.max_bytes"),
|
|
("d","ceph.quota.max_files"),(".","ceph.quota.max_bytes")]:
|
|
assert os.getxattr(src+"/"+rel,key)==os.getxattr(dst+"/"+rel,key),(rel,key)
|
|
# CephFS st_blocks reflects logical size and cannot prove physical sparseness.
|
|
assert os.stat(dst+"/sparse").st_size==16*1024*1024
|
|
with open(dst+"/sparse","rb") as f:
|
|
while chunk:=f.read(65536):
|
|
assert not any(chunk)
|
|
PY
|
|
a=$(source_ceph fs subvolume metadata ls cephfs "$name" --group_name migration)
|
|
b=$(target_ceph fs subvolume metadata ls cephfs "$name" --group_name migration)
|
|
test "$a" = "$b"
|
|
for snap in history-1 history-2; do
|
|
a=$(source_ceph fs subvolume snapshot metadata ls cephfs "$name" "$snap" --group_name migration)
|
|
b=$(target_ceph fs subvolume snapshot metadata ls cephfs "$name" "$snap" --group_name migration)
|
|
test "$a" = "$b"
|
|
done
|
|
python3 /lab/snapshot-identity.py --config "$config" --snapshot history-1
|
|
"$binary" cleanup --config "$config" --state "$state" > "$work/cleaned.json"
|
|
for side in source_ceph target_ceph; do
|
|
names=$("$side" fs subvolume snapshot ls cephfs "$name" --group_name migration |
|
|
python3 -c 'import json,sys; print(" ".join(sorted(x["name"] for x in json.load(sys.stdin))))')
|
|
test "$names" = "history-1 history-2"
|
|
done
|
|
completed=true
|
|
echo 'PASS: Go migration, historical snapshots, live catch-up, quotas, ACLs, xattrs, sparse-file contents, crash/error resume, exclusive target, finalization, snapshot cleanup'
|
|
|