Files
gotgt/cmd/daemon.go
Lei Xue 76ab15b0df feat: add S3-compatible object storage backend
Add a new backend store that enables iSCSI targets backed by
S3-compatible object storage (AWS S3, MinIO, Ceph RGW, etc.).

The implementation uses a chunked storage strategy where the virtual
block device is divided into fixed-size chunks (default 4 MiB), each
stored as an independent S3 object. This enables efficient random
read/write access on top of object storage.

Key features:
- Chunked storage with configurable chunk size
- Sparse device support (unwritten chunks treated as zeros)
- Concurrent multi-chunk I/O via errgroup
- Per-chunk locking for safe read-modify-write
- AWS SDK v2 with default credential chain
- In-process gofakes3 test server (no Docker needed)
- 12 unit tests + 2 integration tests

Also updates CI workflow to run S3 backend tests and updates
README with S3 backend documentation.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-16 16:22:57 +08:00

152 lines
4.0 KiB
Go

/*
Copyright 2016 The GoStor Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"fmt"
"os"
"os/signal"
"strings"
"syscall"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/gostor/gotgt/pkg/apiserver"
"github.com/gostor/gotgt/pkg/config"
_ "github.com/gostor/gotgt/pkg/port/iscsit"
"github.com/gostor/gotgt/pkg/scsi"
_ "github.com/gostor/gotgt/pkg/scsi/backingstore"
_ "github.com/gostor/gotgt/pkg/scsi/backingstore/s3store"
)
func newDaemonCommand() *cobra.Command {
var (
host string
driver string
logLevel string
blockMultipleHosts bool
port int
)
var cmd = &cobra.Command{
Use: "daemon",
Short: "Setup a daemon",
Long: `Setup the Gotgt's daemon`,
PreRun: func(cmd *cobra.Command, args []string) {
viper.BindPFlags(cmd.Flags())
},
RunE: func(cmd *cobra.Command, args []string) error {
host = viper.GetString("host")
return createDaemon(host, driver, logLevel, blockMultipleHosts, port)
},
}
flags := cmd.Flags()
flags.StringVar(&logLevel, "log", "info", "Log level of SCSI target daemon")
flags.StringVar(&driver, "driver", "iscsi", "SCSI low level driver")
flags.IntVar(&port, "port", 3260, "iSCSI default target port")
flags.BoolVar(&blockMultipleHosts, "block-multiple-hosts", false, "Disable login from multiple hosts")
return cmd
}
func createDaemon(host, driver, level string, blockMultipleHosts bool, port int) error {
logLevel, err := log.ParseLevel(level)
if err != nil {
return fmt.Errorf("invalid log level %q: %w", level, err)
}
log.SetLevel(logLevel)
config, err := config.Load(config.ConfigDir())
if err != nil {
log.Error(err)
return err
}
err = scsi.InitSCSILUMap(config)
if err != nil {
log.Error(err)
return err
}
scsiTarget := scsi.NewSCSITargetService()
targetDriver, err := scsi.NewTargetDriver(driver, scsiTarget)
if err != nil {
log.Error(err)
return err
}
for tgtname := range config.ISCSITargets {
targetDriver.NewTarget(tgtname, config)
}
if blockMultipleHosts {
targetDriver.EnableBlockMultipleHostLogin()
}
// comment this to avoid concurrent issue
// runtime.GOMAXPROCS(runtime.NumCPU())
// run a service
go targetDriver.Run(port)
serverConfig := &apiserver.Config{
Addrs: []apiserver.Addr{},
}
hosts := []string{}
if host != "" {
hosts = append(hosts, host)
}
for _, protoAddr := range hosts {
protoAddrParts := strings.SplitN(protoAddr, "://", 2)
if len(protoAddrParts) != 2 {
err = fmt.Errorf("bad format %s, expected PROTO://ADDR", protoAddr)
log.Error(err)
return err
}
serverConfig.Addrs = append(serverConfig.Addrs, apiserver.Addr{Proto: protoAddrParts[0], Addr: protoAddrParts[1]})
}
s, err := apiserver.New(serverConfig)
if err != nil {
log.Error(err)
return err
}
s.InitRouters()
// The serve API routine never exits unless an error occurs
// We need to start it as a goroutine and wait on it so
// daemon doesn't exit
serveAPIWait := make(chan error)
go s.Wait(serveAPIWait)
stopAll := make(chan os.Signal, 1)
signal.Notify(stopAll, syscall.SIGINT, syscall.SIGTERM)
// Daemon is fully initialized and handling API traffic
// Wait for serve API job to complete
select {
case errAPI := <-serveAPIWait:
// If we have an error here it is unique to API (as daemonErr would have
// exited the daemon process above)
if errAPI != nil {
log.Warnf("Shutting down due to ServeAPI error: %v", errAPI)
}
case <-stopAll:
break
}
s.Close()
return nil
}