diff --git a/seaweed-block/cmd/sw-block/main.go b/seaweed-block/cmd/sw-block/main.go index e5f9694350..fb3d5f17e1 100644 --- a/seaweed-block/cmd/sw-block/main.go +++ b/seaweed-block/cmd/sw-block/main.go @@ -79,6 +79,9 @@ func run(args []string, stdout, stderr io.Writer) int { if args[0] == "mini" { return runMini(args[1:], stdout, stderr) } + if args[0] == "volume" { + return runVolume(args[1:], stdout, stderr) + } if args[0] != "ops" { fmt.Fprintf(stderr, "sw-block: unknown command %q\n", args[0]) usage(stderr) diff --git a/seaweed-block/cmd/sw-block/mini.go b/seaweed-block/cmd/sw-block/mini.go index 42666eba9e..87b8b103ae 100644 --- a/seaweed-block/cmd/sw-block/mini.go +++ b/seaweed-block/cmd/sw-block/mini.go @@ -187,17 +187,20 @@ func miniMasterArgs(c miniConfig, chunk bool) []string { // miniVolumeArgs is blockvolume as swb_start runs it, plus, with a chunk // store, the walstore-chunk settings and the volume's layout id. func miniVolumeArgs(c miniConfig, chunk bool, layoutID string) []string { - args := []string{"--master", loopback(c.Ports.Master), "--server-id", miniServer, "--volume-id", miniVolume, "--replica-id", miniReplica, - "--data-addr", loopback(c.Ports.Data), "--ctrl-addr", loopback(c.Ports.Ctrl), "--status-addr", loopback(c.Ports.Status), - "--durable-root", filepath.Join(c.Dir, "vol")} - blocks := strconv.FormatUint(c.SizeBytes/miniBlockSize, 10) - if !chunk { - args = append(args, "--durable-impl", "walstore", "--durable-blocks", blocks) - } else { - args = append(args, "--durable-impl", "walstore-chunk", "--durable-blocks", blocks, "--durable-blocksize", strconv.Itoa(miniBlockSize), - "--chunk-filer", loopback(c.Ports.WeedFG), "--chunk-path", miniChunkPath, "--chunk-control-endpoint", "--workspace-lease", "--layout-id", layoutID) + return miniVolumeSpec(c, chunk, layoutID).args() +} + +// miniVolumeSpec is mini's v1: the same settings volume start writes for a +// unit (volumeSpec), on loopback under the run's dir. +func miniVolumeSpec(c miniConfig, chunk bool, layoutID string) volumeSpec { + v := volumeSpec{Master: loopback(c.Ports.Master), ServerID: miniServer, VolumeID: miniVolume, ReplicaID: miniReplica, + DataAddr: loopback(c.Ports.Data), CtrlAddr: loopback(c.Ports.Ctrl), StatusAddr: loopback(c.Ports.Status), + DurableRoot: filepath.Join(c.Dir, "vol"), Blocks: c.SizeBytes / miniBlockSize, + NVMeListen: loopback(c.Ports.NVMe), NQN: c.NQN} + if chunk { + v.Chunk, v.BlockSize, v.ChunkFiler, v.ChunkPath, v.LayoutID = true, miniBlockSize, loopback(c.Ports.WeedFG), miniChunkPath, layoutID } - return append(args, "--nvme-listen", loopback(c.Ports.NVMe), "--nvme-subsysnqn", c.NQN) + return v } // miniTopology and miniClusterSpec place v1 on s1/r1. diff --git a/seaweed-block/cmd/sw-block/volume_spec.go b/seaweed-block/cmd/sw-block/volume_spec.go new file mode 100644 index 0000000000..567fbf5bf6 --- /dev/null +++ b/seaweed-block/cmd/sw-block/volume_spec.go @@ -0,0 +1,110 @@ +package main + +import ( + "fmt" + "sort" + "strconv" + "strings" +) + +// volumeSpec is one blockvolume's settings, the one source of both forms +// they take: the flags sw-block mini passes to its blockvolume child +// (args), and the settings file volume start writes for the +// sw-blockvolume@ unit (env), whose ExecStart passes each key to +// the same flag. A test renders env through the shipped unit and compares +// it with args, so the two cannot drift. +type volumeSpec struct { + Master, ServerID, VolumeID, ReplicaID string + DataAddr, CtrlAddr, StatusAddr string + DurableRoot string + Blocks uint64 + BlockSize int // 0: blockvolume's default (walstore) + Chunk bool + ChunkFiler, ChunkPath, LayoutID string // with Chunk + NVMeListen, NQN string + ISCSIListen, IQN string // optional (volume start) +} + +// args are the blockvolume flags, in the order mini has always passed them. +func (v volumeSpec) args() []string { + a := []string{"--master", v.Master, "--server-id", v.ServerID, "--volume-id", v.VolumeID, "--replica-id", v.ReplicaID, + "--data-addr", v.DataAddr, "--ctrl-addr", v.CtrlAddr, "--status-addr", v.StatusAddr, "--durable-root", v.DurableRoot} + blocks := strconv.FormatUint(v.Blocks, 10) + if !v.Chunk { + a = append(a, "--durable-impl", "walstore", "--durable-blocks", blocks) + } else { + a = append(a, "--durable-impl", "walstore-chunk", "--durable-blocks", blocks, "--durable-blocksize", strconv.Itoa(v.BlockSize), + "--chunk-filer", v.ChunkFiler, "--chunk-path", v.ChunkPath, "--chunk-control-endpoint", "--workspace-lease", "--layout-id", v.LayoutID) + } + a = append(a, "--nvme-listen", v.NVMeListen, "--nvme-subsysnqn", v.NQN) + if v.ISCSIListen != "" { + a = append(a, "--iscsi-listen", v.ISCSIListen, "--iscsi-iqn", v.IQN) + } + return a +} + +// blockvolumeEnvDefaults are deploy/config/blockvolume.env's values (a test +// keeps them equal): what the unit passes for a key the spec does not set. +var blockvolumeEnvDefaults = map[string]string{ + "SWB_MASTER_ADDR": "127.0.0.1:9180", "SWB_SERVER_ID": "s1", "SWB_VOLUME_ID": "v1", "SWB_REPLICA_ID": "r1", + "SWB_DATA_ADDR": "127.0.0.1:9201", "SWB_CTRL_ADDR": "127.0.0.1:9202", "SWB_STATUS_ADDR": "127.0.0.1:9203", + "SWB_HEARTBEAT_INTERVAL": "2s", "SWB_RECOVER_TIMEOUT": "30s", + "SWB_DURABLE_ROOT": "/var/lib/sw-block-volume/volumes", "SWB_DURABLE_IMPL": "walstore", + "SWB_DURABLE_BLOCKS": "65536", "SWB_DURABLE_BLOCKSIZE": "4096", + "SWB_NVME_LISTEN": "127.0.0.1:4420", "SWB_NVME_SUBSYSNQN": "nqn.2026-05.io.seaweedfs:v1", "SWB_NVME_NS": "1", + "SWB_NVME_MAX_H2C_DATA_LENGTH": "0", "SWB_UBLK_DEVICE": "-1", "SWB_UBLK_QUEUES": "0", "SWB_UBLK_QUEUE_DEPTH": "0", + "SWB_ISCSI_LISTEN": "", "SWB_ISCSI_IQN": "", "SWB_ISCSI_PORTAL_ADDR": "", "SWB_ISCSI_LUN": "0", "SWB_ISCSI_DATAOUT_TIMEOUT": "0", + "SWB_REPLICATION_ACK": "best-effort", "SWB_WAL_RETENTION_LSNS": "0", "SWB_RECOVERY_MODE": "legacy", + "SWB_DEGRADED_PROBE_INTERVAL": "0", "SWB_DEGRADED_PROBE_COOLDOWN_BASE": "5s", "SWB_DEGRADED_PROBE_COOLDOWN_CAP": "60s", + "SWB_CHUNK_FILER": "", "SWB_CHUNK_PATH": "/swblock/chunks", "SWB_CHUNK_COLLECTION": "", "SWB_CHUNK_REPLICATION": "", + "SWB_CHUNK_CAPTURE_INTERVAL": "0", "SWB_CHUNK_SNAPSHOT_SCHEDULE": "daily:7", "SWB_CHUNK_UPLOAD_CONCURRENCY": "8", + "SWB_CHUNK_CONTROL_ENDPOINT": "false", "SWB_WORKSPACE_LEASE": "false", "SWB_VOLUME_EXTRA_ARGS": "", +} + +// env is the unit's settings file for v: every key of the shipped +// defaults, with the spec's values over them. The layout id has no key of +// its own: it goes in SWB_VOLUME_EXTRA_ARGS. +func (v volumeSpec) env() map[string]string { + e := make(map[string]string, len(blockvolumeEnvDefaults)) + for k, val := range blockvolumeEnvDefaults { + e[k] = val + } + set := map[string]string{ + "SWB_MASTER_ADDR": v.Master, "SWB_SERVER_ID": v.ServerID, "SWB_VOLUME_ID": v.VolumeID, "SWB_REPLICA_ID": v.ReplicaID, + "SWB_DATA_ADDR": v.DataAddr, "SWB_CTRL_ADDR": v.CtrlAddr, "SWB_STATUS_ADDR": v.StatusAddr, + "SWB_DURABLE_ROOT": v.DurableRoot, "SWB_DURABLE_IMPL": "walstore", "SWB_DURABLE_BLOCKS": strconv.FormatUint(v.Blocks, 10), + "SWB_NVME_LISTEN": v.NVMeListen, "SWB_NVME_SUBSYSNQN": v.NQN, + "SWB_ISCSI_LISTEN": v.ISCSIListen, "SWB_ISCSI_IQN": v.IQN, + } + if v.Chunk { + set["SWB_DURABLE_IMPL"] = "walstore-chunk" + set["SWB_DURABLE_BLOCKSIZE"] = strconv.Itoa(v.BlockSize) + set["SWB_CHUNK_FILER"], set["SWB_CHUNK_PATH"] = v.ChunkFiler, v.ChunkPath + set["SWB_CHUNK_CONTROL_ENDPOINT"], set["SWB_WORKSPACE_LEASE"] = "true", "true" + set["SWB_VOLUME_EXTRA_ARGS"] = "--layout-id " + v.LayoutID + } + for k, val := range set { + e[k] = val + } + return e +} + +// renderEnv is a settings file: KEY=VALUE lines in key order, each value +// quoted for systemd's EnvironmentFile when it is not plain. +func renderEnv(header string, e map[string]string) string { + keys := make([]string, 0, len(e)) + for k := range e { + keys = append(keys, k) + } + sort.Strings(keys) + var b strings.Builder + b.WriteString(header) + for _, k := range keys { + val := e[k] + if strings.ContainsAny(val, " \t\"'\\$#") { + val = strconv.Quote(val) + } + fmt.Fprintf(&b, "%s=%s\n", k, val) + } + return b.String() +} diff --git a/seaweed-block/cmd/sw-block/volume_verbs.go b/seaweed-block/cmd/sw-block/volume_verbs.go new file mode 100644 index 0000000000..eb56d54666 --- /dev/null +++ b/seaweed-block/cmd/sw-block/volume_verbs.go @@ -0,0 +1,554 @@ +package main + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "flag" + "fmt" + "io" + "net/http" + "os" + "os/exec" + "os/signal" + "path/filepath" + "regexp" + "strconv" + "strings" + "syscall" + "time" + + "github.com/seaweedfs/seaweed-block/core/ops" +) + +// The whole-job volume commands: each runs on the volume's host, prints +// every step before it runs it, and stops at the first step that answers +// otherwise than expected, saying how to go on. A volume is an instance of +// the sw-blockvolume@ unit; its settings file is the only state they keep. + +// The sentence volume stop ends with (and the help and the page say): what +// it promises, and only that. +const stopRecoveryPoint = "recovery point in the object store: the cut at %s; writes after it are on this host only" + +const volumeUsage = `usage: sudo sw-block volume start [flags] + sudo sw-block volume stop [--force] [--no-cut] [--timeout 2m] + +start: an existing volume (in the cluster spec) as the systemd unit +sw-blockvolume@: writes /etc/sw-block/volumes/.env, resolves +the volume's layout id (with --chunk-filer), starts the unit, waits until it +serves, and prints the connect command. A new volume, until volume start +can create one: add it to blockmaster's cluster spec and restart blockmaster +(the standalone page's "Required setup"), then volume start. + +stop: refuses while a guest is connected (unless --force), takes a cut, +refuses when the cut failed or its outcome is unknown (unless --no-cut), +stops the unit, and ends with: "` + stopRecoveryPoint + `".` + +// volumeHelp is the usage, with the stop sentence's time shown as