102 lines
2.6 KiB
Go
102 lines
2.6 KiB
Go
package disk
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
|
|
"github.com/bams/backend/internal/config"
|
|
"github.com/bams/backend/internal/logger"
|
|
)
|
|
|
|
type Repository struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Type string `json:"type"` // "lvm" or "zfs"
|
|
Size string `json:"size"`
|
|
Used string `json:"used"`
|
|
MountPoint string `json:"mount_point"`
|
|
Status string `json:"status"`
|
|
VGName string `json:"vg_name,omitempty"`
|
|
PoolName string `json:"pool_name,omitempty"`
|
|
}
|
|
|
|
type Service struct {
|
|
config *config.Config
|
|
logger *logger.Logger
|
|
}
|
|
|
|
func NewService(cfg *config.Config, log *logger.Logger) *Service {
|
|
return &Service{
|
|
config: cfg,
|
|
logger: log,
|
|
}
|
|
}
|
|
|
|
func (s *Service) ListRepositories() ([]*Repository, error) {
|
|
// TODO: Implement actual LVM/ZFS listing
|
|
s.logger.Debug("Listing disk repositories")
|
|
return []*Repository{}, nil
|
|
}
|
|
|
|
func (s *Service) GetRepository(id string) (*Repository, error) {
|
|
// TODO: Implement actual repository retrieval
|
|
s.logger.Debug("Getting repository", "id", id)
|
|
return nil, fmt.Errorf("repository not found: %s", id)
|
|
}
|
|
|
|
func (s *Service) CreateRepository(name, size, repoType, vgName, poolName string) (*Repository, error) {
|
|
s.logger.Info("Creating repository", "name", name, "size", size, "type", repoType)
|
|
|
|
var err error
|
|
if repoType == "lvm" {
|
|
err = s.createLVMRepository(name, size, vgName)
|
|
} else if repoType == "zfs" {
|
|
err = s.createZFSRepository(name, size, poolName)
|
|
} else {
|
|
return nil, fmt.Errorf("unsupported repository type: %s", repoType)
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &Repository{
|
|
ID: name,
|
|
Name: name,
|
|
Type: repoType,
|
|
Size: size,
|
|
Status: "active",
|
|
VGName: vgName,
|
|
PoolName: poolName,
|
|
}, nil
|
|
}
|
|
|
|
func (s *Service) createLVMRepository(name, size, vgName string) error {
|
|
// Create LVM logical volume
|
|
cmd := exec.Command("lvcreate", "-L", size, "-n", name, vgName)
|
|
output, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
s.logger.Error("Failed to create LVM volume", "error", string(output))
|
|
return fmt.Errorf("failed to create LVM volume: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) createZFSRepository(name, size, poolName string) error {
|
|
// Create ZFS zvol
|
|
zvolPath := fmt.Sprintf("%s/%s", poolName, name)
|
|
cmd := exec.Command("zfs", "create", "-V", size, zvolPath)
|
|
output, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
s.logger.Error("Failed to create ZFS zvol", "error", string(output))
|
|
return fmt.Errorf("failed to create ZFS zvol: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Service) DeleteRepository(id string) error {
|
|
s.logger.Info("Deleting repository", "id", id)
|
|
// TODO: Implement actual deletion
|
|
return nil
|
|
}
|