101 lines
2.6 KiB
Go
101 lines
2.6 KiB
Go
package disk
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
"path/filepath"
|
|
"strings"
|
|
)
|
|
|
|
// MapToiSCSI maps a repository to an iSCSI LUN
|
|
func (s *Service) MapToiSCSI(repoID string, targetID string, lunNumber int) error {
|
|
s.logger.Info("Mapping repository to iSCSI", "repo", repoID, "target", targetID, "lun", lunNumber)
|
|
|
|
repo, err := s.GetRepository(repoID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Get device path for the repository
|
|
var devicePath string
|
|
if repo.Type == "lvm" {
|
|
// LVM device path: /dev/vgname/lvname
|
|
devicePath = fmt.Sprintf("/dev/%s/%s", repo.VGName, repo.Name)
|
|
} else if repo.Type == "zfs" {
|
|
// ZFS zvol path: /dev/zvol/poolname/volname
|
|
devicePath = fmt.Sprintf("/dev/zvol/%s/%s", repo.PoolName, repo.Name)
|
|
} else {
|
|
return fmt.Errorf("unsupported repository type: %s", repo.Type)
|
|
}
|
|
|
|
// Verify device exists
|
|
if _, err := filepath.EvalSymlinks(devicePath); err != nil {
|
|
return fmt.Errorf("device not found: %s", devicePath)
|
|
}
|
|
|
|
// The actual mapping is done by the iSCSI service
|
|
// This is just a helper to get the device path
|
|
return nil
|
|
}
|
|
|
|
// GetDevicePath returns the device path for a repository
|
|
func (s *Service) GetDevicePath(repoID string) (string, error) {
|
|
repo, err := s.GetRepository(repoID)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if repo.Type == "lvm" {
|
|
return fmt.Sprintf("/dev/%s/%s", repo.VGName, repo.Name), nil
|
|
} else if repo.Type == "zfs" {
|
|
return fmt.Sprintf("/dev/zvol/%s/%s", repo.PoolName, repo.Name), nil
|
|
}
|
|
|
|
return "", fmt.Errorf("unsupported repository type: %s", repo.Type)
|
|
}
|
|
|
|
// GetRepositoryInfo returns detailed information about a repository
|
|
func (s *Service) GetRepositoryInfo(repoID string) (map[string]interface{}, error) {
|
|
repo, err := s.GetRepository(repoID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
info := map[string]interface{}{
|
|
"id": repo.ID,
|
|
"name": repo.Name,
|
|
"type": repo.Type,
|
|
"size": repo.Size,
|
|
"used": repo.Used,
|
|
"status": repo.Status,
|
|
"mount_point": repo.MountPoint,
|
|
}
|
|
|
|
// Get device path
|
|
devicePath, err := s.GetDevicePath(repoID)
|
|
if err == nil {
|
|
info["device_path"] = devicePath
|
|
}
|
|
|
|
// Get filesystem info if mounted
|
|
if repo.MountPoint != "" {
|
|
cmd := exec.Command("df", "-h", repo.MountPoint)
|
|
output, err := cmd.CombinedOutput()
|
|
if err == nil {
|
|
lines := strings.Split(string(output), "\n")
|
|
if len(lines) > 1 {
|
|
fields := strings.Fields(lines[1])
|
|
if len(fields) >= 5 {
|
|
info["filesystem"] = fields[0]
|
|
info["total_size"] = fields[1]
|
|
info["used_size"] = fields[2]
|
|
info["available"] = fields[3]
|
|
info["usage_percent"] = fields[4]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return info, nil
|
|
}
|