50%
This commit is contained in:
@@ -740,7 +740,12 @@ func (s *VTLService) ListMediaChangers() ([]models.VTLMediaChanger, error) {
|
||||
|
||||
// Parse device.conf to get libraries
|
||||
deviceConfig, err := s.parseDeviceConfig()
|
||||
if err != nil {
|
||||
log.Printf("Warning: failed to parse device.conf: %v", err)
|
||||
}
|
||||
|
||||
if err == nil && len(deviceConfig.Libraries) > 0 {
|
||||
log.Printf("Found %d libraries in device.conf", len(deviceConfig.Libraries))
|
||||
// Count drives per library
|
||||
drivesPerLibrary := make(map[int]int)
|
||||
for _, drive := range deviceConfig.Drives {
|
||||
@@ -750,11 +755,37 @@ func (s *VTLService) ListMediaChangers() ([]models.VTLMediaChanger, error) {
|
||||
// Count slots per library from library_contents
|
||||
slotsPerLibrary := make(map[int]int)
|
||||
for _, lib := range deviceConfig.Libraries {
|
||||
slotMap, err := s.parseLibraryContents(lib.ID)
|
||||
if err == nil {
|
||||
slotsPerLibrary[lib.ID] = len(slotMap)
|
||||
// Count all slots (including empty ones) by reading file directly
|
||||
contentsPath := fmt.Sprintf("/etc/mhvtl/library_contents.%d", lib.ID)
|
||||
if file, err := os.Open(contentsPath); err == nil {
|
||||
scanner := bufio.NewScanner(file)
|
||||
slotRegex := regexp.MustCompile(`^Slot\s+(\d+):`)
|
||||
maxSlot := 0
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
if matches := slotRegex.FindStringSubmatch(line); len(matches) >= 2 {
|
||||
if slotID, err := strconv.Atoi(matches[1]); err == nil && slotID > maxSlot {
|
||||
maxSlot = slotID
|
||||
}
|
||||
}
|
||||
}
|
||||
file.Close()
|
||||
if maxSlot > 0 {
|
||||
slotsPerLibrary[lib.ID] = maxSlot
|
||||
} else {
|
||||
// Fallback: try parseLibraryContents
|
||||
slotMap, _ := s.parseLibraryContents(lib.ID)
|
||||
slotsPerLibrary[lib.ID] = len(slotMap)
|
||||
if slotsPerLibrary[lib.ID] == 0 {
|
||||
slotsPerLibrary[lib.ID] = 10 // Default
|
||||
}
|
||||
}
|
||||
} else {
|
||||
slotsPerLibrary[lib.ID] = 10 // Default
|
||||
// File doesn't exist, use default
|
||||
slotsPerLibrary[lib.ID] = 10
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1026,6 +1057,542 @@ func (s *VTLService) parseDeviceConfig() (*DeviceConfig, error) {
|
||||
return config, scanner.Err()
|
||||
}
|
||||
|
||||
// writeDeviceConfig writes device.conf from DeviceConfig struct
|
||||
func (s *VTLService) writeDeviceConfig(config *DeviceConfig) error {
|
||||
// Ensure filesystem is writable (remount if needed)
|
||||
parentDir := filepath.Dir(s.deviceConfigPath)
|
||||
if err := s.ensureWritableFilesystem(parentDir); err != nil {
|
||||
log.Printf("Warning: failed to ensure writable filesystem for %s: %v", parentDir, err)
|
||||
// Continue anyway, might still work
|
||||
}
|
||||
|
||||
// Create backup of existing config
|
||||
backupPath := s.deviceConfigPath + ".backup"
|
||||
if _, err := os.Stat(s.deviceConfigPath); err == nil {
|
||||
// File exists, create backup
|
||||
if err := exec.Command("cp", s.deviceConfigPath, backupPath).Run(); err != nil {
|
||||
log.Printf("Warning: failed to create backup of device.conf: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Try to create file, if it fails due to read-only, try remount again
|
||||
file, err := os.Create(s.deviceConfigPath)
|
||||
if err != nil {
|
||||
// Check if it's a read-only filesystem error
|
||||
if strings.Contains(err.Error(), "read-only") || strings.Contains(err.Error(), "read only") {
|
||||
log.Printf("Filesystem is read-only, attempting remount for %s", parentDir)
|
||||
if remountErr := s.remountReadWrite(parentDir); remountErr != nil {
|
||||
return fmt.Errorf("failed to remount filesystem as read-write: %v", remountErr)
|
||||
}
|
||||
// Try again after remount
|
||||
file, err = os.Create(s.deviceConfigPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create device.conf after remount: %v", err)
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("failed to create device.conf: %v", err)
|
||||
}
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
writer := bufio.NewWriter(file)
|
||||
|
||||
// Write libraries and their drives
|
||||
for _, lib := range config.Libraries {
|
||||
// Write library header
|
||||
writer.WriteString(fmt.Sprintf("Library: %d CHANNEL: 00 TARGET: 00 LUN: 00\n", lib.ID))
|
||||
if lib.Vendor != "" {
|
||||
writer.WriteString(fmt.Sprintf("Vendor identification: %s\n", lib.Vendor))
|
||||
} else {
|
||||
writer.WriteString("Vendor identification: STK\n")
|
||||
}
|
||||
if lib.Product != "" {
|
||||
writer.WriteString(fmt.Sprintf("Product identification: %s\n", lib.Product))
|
||||
} else {
|
||||
// Default product based on library ID
|
||||
if lib.ID == 30 {
|
||||
writer.WriteString("Product identification: L80\n")
|
||||
} else {
|
||||
writer.WriteString("Product identification: L700\n")
|
||||
}
|
||||
}
|
||||
if lib.Serial != "" {
|
||||
writer.WriteString(fmt.Sprintf("Unit serial number: %s\n", lib.Serial))
|
||||
} else {
|
||||
writer.WriteString(fmt.Sprintf("Unit serial number: %08d\n", lib.ID))
|
||||
}
|
||||
writer.WriteString("\n")
|
||||
|
||||
// Write drives for this library
|
||||
for _, drive := range config.Drives {
|
||||
if drive.LibraryID == lib.ID {
|
||||
// Calculate CHANNEL, TARGET, LUN from drive ID
|
||||
// Drive ID format: libraryID * 10 + slot (e.g., 11 = library 10, slot 1)
|
||||
channel := "00"
|
||||
target := fmt.Sprintf("%02d", (drive.ID%100)/10)
|
||||
lun := "00"
|
||||
|
||||
writer.WriteString(fmt.Sprintf("Drive: %d CHANNEL: %s TARGET: %s LUN: %s\n", drive.ID, channel, target, lun))
|
||||
writer.WriteString(fmt.Sprintf("Library ID: %d\n", drive.LibraryID))
|
||||
writer.WriteString(fmt.Sprintf("Slot: %d\n", drive.SlotID))
|
||||
if drive.Vendor != "" {
|
||||
writer.WriteString(fmt.Sprintf("Vendor identification: %s\n", drive.Vendor))
|
||||
} else {
|
||||
writer.WriteString("Vendor identification: IBM\n")
|
||||
}
|
||||
if drive.Product != "" {
|
||||
writer.WriteString(fmt.Sprintf("Product identification: %s\n", drive.Product))
|
||||
} else {
|
||||
writer.WriteString("Product identification: ULT3580-TD5\n")
|
||||
}
|
||||
if drive.Serial != "" {
|
||||
writer.WriteString(fmt.Sprintf("Unit serial number: %s\n", drive.Serial))
|
||||
} else {
|
||||
writer.WriteString(fmt.Sprintf("Unit serial number: %08d\n", drive.ID))
|
||||
}
|
||||
writer.WriteString("\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Flush and sync to ensure data is written to disk
|
||||
if err := writer.Flush(); err != nil {
|
||||
return fmt.Errorf("failed to flush device.conf: %v", err)
|
||||
}
|
||||
if err := file.Sync(); err != nil {
|
||||
log.Printf("Warning: failed to sync device.conf: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("Successfully wrote device.conf with %d libraries and %d drives", len(config.Libraries), len(config.Drives))
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddMediaChanger adds a new media changer/library to device.conf
|
||||
func (s *VTLService) AddMediaChanger(libraryID int, vendor, product, serial string, numSlots int) error {
|
||||
if libraryID <= 0 {
|
||||
return fmt.Errorf("library ID must be greater than 0")
|
||||
}
|
||||
|
||||
// Parse existing config
|
||||
config, err := s.parseDeviceConfig()
|
||||
if err != nil {
|
||||
// If file doesn't exist, create new config
|
||||
config = &DeviceConfig{
|
||||
Libraries: []LibraryConfig{},
|
||||
Drives: []DriveConfig{},
|
||||
}
|
||||
}
|
||||
|
||||
// Check if library already exists
|
||||
for _, lib := range config.Libraries {
|
||||
if lib.ID == libraryID {
|
||||
return fmt.Errorf("library %d already exists", libraryID)
|
||||
}
|
||||
}
|
||||
|
||||
// Set defaults
|
||||
if vendor == "" {
|
||||
vendor = "STK"
|
||||
}
|
||||
if product == "" {
|
||||
if libraryID == 30 {
|
||||
product = "L80"
|
||||
} else {
|
||||
product = "L700"
|
||||
}
|
||||
}
|
||||
if serial == "" {
|
||||
serial = fmt.Sprintf("%08d", libraryID)
|
||||
}
|
||||
|
||||
// Add new library
|
||||
newLib := LibraryConfig{
|
||||
ID: libraryID,
|
||||
Vendor: vendor,
|
||||
Product: product,
|
||||
Serial: serial,
|
||||
}
|
||||
config.Libraries = append(config.Libraries, newLib)
|
||||
|
||||
// Write updated config
|
||||
if err := s.writeDeviceConfig(config); err != nil {
|
||||
return fmt.Errorf("failed to write device.conf: %v", err)
|
||||
}
|
||||
|
||||
// Create library_contents file if it doesn't exist
|
||||
contentsPath := fmt.Sprintf("/etc/mhvtl/library_contents.%d", libraryID)
|
||||
if _, err := os.Stat(contentsPath); os.IsNotExist(err) {
|
||||
// Ensure filesystem is writable
|
||||
parentDir := filepath.Dir(contentsPath)
|
||||
if err := s.ensureWritableFilesystem(parentDir); err != nil {
|
||||
log.Printf("Warning: failed to ensure writable filesystem for %s: %v", parentDir, err)
|
||||
}
|
||||
|
||||
file, err := os.Create(contentsPath)
|
||||
if err != nil {
|
||||
// If read-only, try remount
|
||||
if strings.Contains(err.Error(), "read-only") || strings.Contains(err.Error(), "read only") {
|
||||
if remountErr := s.remountReadWrite(parentDir); remountErr != nil {
|
||||
log.Printf("Warning: failed to remount for library_contents: %v", remountErr)
|
||||
} else {
|
||||
// Retry after remount
|
||||
file, err = os.Create(contentsPath)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("Warning: failed to create library_contents.%d: %v", libraryID, err)
|
||||
} else {
|
||||
defer file.Close()
|
||||
// Write library_contents in correct format
|
||||
file.WriteString("VERSION: 2\n\n")
|
||||
// Drives will be added when drives are assigned to this library
|
||||
file.WriteString("Picker 1:\n\n")
|
||||
// Initialize with empty slots (MAP entries)
|
||||
for i := 1; i <= numSlots; i++ {
|
||||
file.WriteString(fmt.Sprintf("MAP %d:\n", i))
|
||||
}
|
||||
log.Printf("Created library_contents.%d with %d slots", libraryID, numSlots)
|
||||
}
|
||||
} else {
|
||||
defer file.Close()
|
||||
// Write library_contents in correct format
|
||||
file.WriteString("VERSION: 2\n\n")
|
||||
// Drives will be added when drives are assigned to this library
|
||||
file.WriteString("Picker 1:\n\n")
|
||||
// Initialize with empty slots (MAP entries)
|
||||
for i := 1; i <= numSlots; i++ {
|
||||
file.WriteString(fmt.Sprintf("MAP %d:\n", i))
|
||||
}
|
||||
log.Printf("Created library_contents.%d with %d slots", libraryID, numSlots)
|
||||
}
|
||||
}
|
||||
|
||||
// Restart mhvtl service to reflect changes (new library needs to be detected)
|
||||
if err := s.RestartService(); err != nil {
|
||||
log.Printf("Warning: failed to restart mhvtl service after adding media changer: %v", err)
|
||||
// Continue even if restart fails - library is added to config
|
||||
}
|
||||
|
||||
log.Printf("Added media changer: Library %d (%s %s)", libraryID, vendor, product)
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveMediaChanger removes a media changer/library from device.conf
|
||||
func (s *VTLService) RemoveMediaChanger(libraryID int) error {
|
||||
if libraryID <= 0 {
|
||||
return fmt.Errorf("library ID must be greater than 0")
|
||||
}
|
||||
|
||||
// Parse existing config
|
||||
config, err := s.parseDeviceConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse device.conf: %v", err)
|
||||
}
|
||||
|
||||
// Find and remove library
|
||||
found := false
|
||||
newLibraries := []LibraryConfig{}
|
||||
for _, lib := range config.Libraries {
|
||||
if lib.ID != libraryID {
|
||||
newLibraries = append(newLibraries, lib)
|
||||
} else {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return fmt.Errorf("library %d not found", libraryID)
|
||||
}
|
||||
|
||||
// Remove all drives associated with this library
|
||||
newDrives := []DriveConfig{}
|
||||
for _, drive := range config.Drives {
|
||||
if drive.LibraryID != libraryID {
|
||||
newDrives = append(newDrives, drive)
|
||||
}
|
||||
}
|
||||
|
||||
config.Libraries = newLibraries
|
||||
config.Drives = newDrives
|
||||
|
||||
// Write updated config
|
||||
if err := s.writeDeviceConfig(config); err != nil {
|
||||
return fmt.Errorf("failed to write device.conf: %v", err)
|
||||
}
|
||||
|
||||
// Optionally remove library_contents file (but keep it for safety)
|
||||
// contentsPath := fmt.Sprintf("/etc/mhvtl/library_contents.%d", libraryID)
|
||||
// os.Remove(contentsPath)
|
||||
|
||||
// Restart mhvtl service to reflect changes (library removal)
|
||||
if err := s.RestartService(); err != nil {
|
||||
log.Printf("Warning: failed to restart mhvtl service after removing media changer: %v", err)
|
||||
// Continue even if restart fails - library is removed from config
|
||||
}
|
||||
|
||||
log.Printf("Removed media changer: Library %d", libraryID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateMediaChanger updates a media changer/library configuration
|
||||
func (s *VTLService) UpdateMediaChanger(libraryID int, vendor, product, serial string) error {
|
||||
if libraryID <= 0 {
|
||||
return fmt.Errorf("library ID must be greater than 0")
|
||||
}
|
||||
|
||||
// Parse existing config
|
||||
config, err := s.parseDeviceConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse device.conf: %v", err)
|
||||
}
|
||||
|
||||
// Find and update library
|
||||
found := false
|
||||
for i := range config.Libraries {
|
||||
if config.Libraries[i].ID == libraryID {
|
||||
if vendor != "" {
|
||||
config.Libraries[i].Vendor = vendor
|
||||
}
|
||||
if product != "" {
|
||||
config.Libraries[i].Product = product
|
||||
}
|
||||
if serial != "" {
|
||||
config.Libraries[i].Serial = serial
|
||||
}
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return fmt.Errorf("library %d not found", libraryID)
|
||||
}
|
||||
|
||||
// Write updated config
|
||||
if err := s.writeDeviceConfig(config); err != nil {
|
||||
return fmt.Errorf("failed to write device.conf: %v", err)
|
||||
}
|
||||
|
||||
// Restart mhvtl service to reflect changes (library config update)
|
||||
if err := s.RestartService(); err != nil {
|
||||
log.Printf("Warning: failed to restart mhvtl service after updating media changer: %v", err)
|
||||
// Continue even if restart fails - library is updated in config
|
||||
}
|
||||
|
||||
log.Printf("Updated media changer: Library %d", libraryID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// AddDrive adds a new drive to device.conf
|
||||
func (s *VTLService) AddDrive(driveID, libraryID, slotID int, vendor, product, serial string) error {
|
||||
if driveID <= 0 {
|
||||
return fmt.Errorf("drive ID must be greater than 0")
|
||||
}
|
||||
if libraryID <= 0 {
|
||||
return fmt.Errorf("library ID must be greater than 0")
|
||||
}
|
||||
if slotID <= 0 {
|
||||
return fmt.Errorf("slot ID must be greater than 0")
|
||||
}
|
||||
|
||||
// Parse existing config
|
||||
config, err := s.parseDeviceConfig()
|
||||
if err != nil {
|
||||
// If file doesn't exist, create new config
|
||||
config = &DeviceConfig{
|
||||
Libraries: []LibraryConfig{},
|
||||
Drives: []DriveConfig{},
|
||||
}
|
||||
}
|
||||
|
||||
// Check if library exists
|
||||
libraryExists := false
|
||||
for _, lib := range config.Libraries {
|
||||
if lib.ID == libraryID {
|
||||
libraryExists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !libraryExists {
|
||||
return fmt.Errorf("library %d does not exist", libraryID)
|
||||
}
|
||||
|
||||
// Check if drive already exists
|
||||
for _, drive := range config.Drives {
|
||||
if drive.ID == driveID {
|
||||
return fmt.Errorf("drive %d already exists", driveID)
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate TARGET for this drive (used to determine device path)
|
||||
// TARGET = (driveID % 100) / 10
|
||||
// This ensures each drive gets a unique TARGET
|
||||
newTarget := (driveID % 100) / 10
|
||||
|
||||
// Check for TARGET conflict with existing drives
|
||||
// Each TARGET maps to a unique device path (/dev/stX), so we need to ensure uniqueness
|
||||
for _, drive := range config.Drives {
|
||||
existingTarget := (drive.ID % 100) / 10
|
||||
if existingTarget == newTarget {
|
||||
// TARGET maps to device: TARGET 01 -> /dev/st0, TARGET 02 -> /dev/st1, etc.
|
||||
deviceNum := newTarget - 1
|
||||
return fmt.Errorf("drive ID %d would conflict with drive %d (both use TARGET %02d, device /dev/st%d). Please use a different drive ID", driveID, drive.ID, existingTarget, deviceNum)
|
||||
}
|
||||
}
|
||||
|
||||
// Set defaults
|
||||
if vendor == "" {
|
||||
vendor = "IBM"
|
||||
}
|
||||
if product == "" {
|
||||
product = "ULT3580-TD5"
|
||||
}
|
||||
if serial == "" {
|
||||
serial = fmt.Sprintf("%08d", driveID)
|
||||
}
|
||||
|
||||
// Add new drive
|
||||
newDrive := DriveConfig{
|
||||
ID: driveID,
|
||||
LibraryID: libraryID,
|
||||
SlotID: slotID,
|
||||
Vendor: vendor,
|
||||
Product: product,
|
||||
Serial: serial,
|
||||
}
|
||||
config.Drives = append(config.Drives, newDrive)
|
||||
|
||||
// Write updated config
|
||||
if err := s.writeDeviceConfig(config); err != nil {
|
||||
return fmt.Errorf("failed to write device.conf: %v", err)
|
||||
}
|
||||
|
||||
// Update library_contents to add Drive entry if not exists
|
||||
if err := s.updateLibraryContentsForDrive(libraryID, driveID); err != nil {
|
||||
log.Printf("Warning: failed to update library_contents.%d for drive %d: %v", libraryID, driveID, err)
|
||||
// Continue even if library_contents update fails
|
||||
}
|
||||
|
||||
// Restart mhvtl service to reflect changes (new drive needs to be detected)
|
||||
if err := s.RestartService(); err != nil {
|
||||
log.Printf("Warning: failed to restart mhvtl service after adding drive: %v", err)
|
||||
// Continue even if restart fails - drive is added to config
|
||||
}
|
||||
|
||||
log.Printf("Added drive: Drive %d (Library %d, Slot %d)", driveID, libraryID, slotID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveDrive removes a drive from device.conf
|
||||
func (s *VTLService) RemoveDrive(driveID int) error {
|
||||
if driveID <= 0 {
|
||||
return fmt.Errorf("drive ID must be greater than 0")
|
||||
}
|
||||
|
||||
// Parse existing config
|
||||
config, err := s.parseDeviceConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse device.conf: %v", err)
|
||||
}
|
||||
|
||||
// Find and remove drive
|
||||
found := false
|
||||
newDrives := []DriveConfig{}
|
||||
for _, drive := range config.Drives {
|
||||
if drive.ID != driveID {
|
||||
newDrives = append(newDrives, drive)
|
||||
} else {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return fmt.Errorf("drive %d not found", driveID)
|
||||
}
|
||||
|
||||
config.Drives = newDrives
|
||||
|
||||
// Write updated config
|
||||
if err := s.writeDeviceConfig(config); err != nil {
|
||||
return fmt.Errorf("failed to write device.conf: %v", err)
|
||||
}
|
||||
|
||||
// Restart mhvtl service to reflect changes (drive removal)
|
||||
if err := s.RestartService(); err != nil {
|
||||
log.Printf("Warning: failed to restart mhvtl service after removing drive: %v", err)
|
||||
// Continue even if restart fails - drive is removed from config
|
||||
}
|
||||
|
||||
log.Printf("Removed drive: Drive %d", driveID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateDrive updates a drive configuration
|
||||
func (s *VTLService) UpdateDrive(driveID, libraryID, slotID int, vendor, product, serial string) error {
|
||||
if driveID <= 0 {
|
||||
return fmt.Errorf("drive ID must be greater than 0")
|
||||
}
|
||||
|
||||
// Parse existing config
|
||||
config, err := s.parseDeviceConfig()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to parse device.conf: %v", err)
|
||||
}
|
||||
|
||||
// Find and update drive
|
||||
found := false
|
||||
for i := range config.Drives {
|
||||
if config.Drives[i].ID == driveID {
|
||||
if libraryID > 0 {
|
||||
// Check if library exists
|
||||
libraryExists := false
|
||||
for _, lib := range config.Libraries {
|
||||
if lib.ID == libraryID {
|
||||
libraryExists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !libraryExists {
|
||||
return fmt.Errorf("library %d does not exist", libraryID)
|
||||
}
|
||||
config.Drives[i].LibraryID = libraryID
|
||||
}
|
||||
if slotID > 0 {
|
||||
config.Drives[i].SlotID = slotID
|
||||
}
|
||||
if vendor != "" {
|
||||
config.Drives[i].Vendor = vendor
|
||||
}
|
||||
if product != "" {
|
||||
config.Drives[i].Product = product
|
||||
}
|
||||
if serial != "" {
|
||||
config.Drives[i].Serial = serial
|
||||
}
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !found {
|
||||
return fmt.Errorf("drive %d not found", driveID)
|
||||
}
|
||||
|
||||
// Write updated config
|
||||
if err := s.writeDeviceConfig(config); err != nil {
|
||||
return fmt.Errorf("failed to write device.conf: %v", err)
|
||||
}
|
||||
|
||||
// Restart mhvtl service to reflect changes (drive config update)
|
||||
if err := s.RestartService(); err != nil {
|
||||
log.Printf("Warning: failed to restart mhvtl service after updating drive: %v", err)
|
||||
// Continue even if restart fails - drive is updated in config
|
||||
}
|
||||
|
||||
log.Printf("Updated drive: Drive %d", driveID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// parseLibraryContents parses library_contents.X file
|
||||
func (s *VTLService) parseLibraryContents(libraryID int) (map[int]string, error) {
|
||||
// Map slot ID to barcode
|
||||
@@ -1067,32 +1634,31 @@ func (s *VTLService) parseLibraryContents(libraryID int) (map[int]string, error)
|
||||
}
|
||||
|
||||
// findDeviceForDrive finds device path for a drive ID
|
||||
// In mhvtl, device path is determined by the order drives appear in device.conf
|
||||
// TARGET number in device.conf maps to device: TARGET 01 -> /dev/st0, TARGET 02 -> /dev/st1, etc.
|
||||
func (s *VTLService) findDeviceForDrive(driveID int) string {
|
||||
// Try to find device in /sys/class/scsi_tape/
|
||||
tapePath := "/sys/class/scsi_tape"
|
||||
entries, err := os.ReadDir(tapePath)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("/dev/st%d", driveID%10) // Fallback
|
||||
}
|
||||
|
||||
for _, entry := range entries {
|
||||
if !entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
deviceName := entry.Name()
|
||||
if !strings.HasPrefix(deviceName, "st") && !strings.HasPrefix(deviceName, "nst") {
|
||||
continue
|
||||
}
|
||||
|
||||
// Check if this device matches the drive ID
|
||||
// This is a simplified check - in real implementation, we'd need to map device to drive ID
|
||||
deviceID := s.getDriveIDFromDevice(deviceName)
|
||||
if deviceID == driveID {
|
||||
return fmt.Sprintf("/dev/%s", deviceName)
|
||||
// Parse device.conf to get TARGET for this drive
|
||||
deviceConfig, err := s.parseDeviceConfig()
|
||||
if err == nil {
|
||||
// Find the drive and calculate its TARGET
|
||||
for _, drive := range deviceConfig.Drives {
|
||||
if drive.ID == driveID {
|
||||
// Calculate TARGET from drive ID (same as in writeDeviceConfig)
|
||||
target := (driveID % 100) / 10
|
||||
// In mhvtl: TARGET 01 -> /dev/st0, TARGET 02 -> /dev/st1, TARGET 03 -> /dev/st2
|
||||
// Device numbering is 0-based, TARGET is effectively 1-based for the ones digit
|
||||
// So: TARGET 01 (ones=1) -> st0, TARGET 02 (ones=2) -> st1, etc.
|
||||
return fmt.Sprintf("/dev/st%d", target-1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Sprintf("/dev/st%d", driveID%10) // Fallback
|
||||
// Fallback: calculate TARGET from drive ID
|
||||
target := (driveID % 100) / 10
|
||||
if target > 0 {
|
||||
return fmt.Sprintf("/dev/st%d", target-1) // TARGET 01 -> st0, TARGET 02 -> st1
|
||||
}
|
||||
return fmt.Sprintf("/dev/st0") // Default fallback
|
||||
}
|
||||
|
||||
// checkMediaLoadedByDriveID checks if media is loaded in a drive by drive ID
|
||||
@@ -1477,6 +2043,200 @@ func (s *VTLService) addTapeToLibraryContents(libraryID int, slotID int, barcode
|
||||
return nil
|
||||
}
|
||||
|
||||
// updateLibraryContentsForDrive adds or updates Drive entry in library_contents file
|
||||
func (s *VTLService) updateLibraryContentsForDrive(libraryID, driveID int) error {
|
||||
contentsPath := fmt.Sprintf("/etc/mhvtl/library_contents.%d", libraryID)
|
||||
|
||||
// Ensure filesystem is writable
|
||||
parentDir := filepath.Dir(contentsPath)
|
||||
if err := s.ensureWritableFilesystem(parentDir); err != nil {
|
||||
log.Printf("Warning: failed to ensure writable filesystem for %s: %v", parentDir, err)
|
||||
}
|
||||
|
||||
// Check if file exists, if not create it with proper format
|
||||
file, err := os.OpenFile(contentsPath, os.O_RDWR|os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
// If read-only, try remount
|
||||
if strings.Contains(err.Error(), "read-only") || strings.Contains(err.Error(), "read only") {
|
||||
if remountErr := s.remountReadWrite(parentDir); remountErr != nil {
|
||||
return fmt.Errorf("failed to remount filesystem: %v", remountErr)
|
||||
}
|
||||
// Retry after remount
|
||||
file, err = os.OpenFile(contentsPath, os.O_RDWR|os.O_CREATE, 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to open library_contents file after remount: %v", err)
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("failed to open library_contents file: %v", err)
|
||||
}
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Read all lines
|
||||
var lines []string
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
lines = append(lines, scanner.Text())
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
return fmt.Errorf("failed to read library_contents file: %v", err)
|
||||
}
|
||||
|
||||
// If file is empty or doesn't have VERSION, create new format
|
||||
if len(lines) == 0 {
|
||||
lines = []string{
|
||||
"VERSION: 2",
|
||||
"",
|
||||
"Picker 1:",
|
||||
"",
|
||||
}
|
||||
} else {
|
||||
// Check if VERSION exists
|
||||
hasVersion := false
|
||||
for _, line := range lines {
|
||||
if strings.Contains(strings.TrimSpace(line), "VERSION:") {
|
||||
hasVersion = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasVersion {
|
||||
// Prepend VERSION
|
||||
lines = append([]string{"VERSION: 2", ""}, lines...)
|
||||
}
|
||||
}
|
||||
|
||||
// Find drive number (sequential number for drives in this library)
|
||||
// Count existing drives
|
||||
driveRegex := regexp.MustCompile(`^Drive\s+(\d+):`)
|
||||
maxDriveNum := 0
|
||||
driveExists := false
|
||||
driveNum := 0
|
||||
|
||||
for _, line := range lines {
|
||||
trimmed := strings.TrimSpace(line)
|
||||
if matches := driveRegex.FindStringSubmatch(trimmed); len(matches) >= 2 {
|
||||
if num, err := strconv.Atoi(matches[1]); err == nil {
|
||||
if num > maxDriveNum {
|
||||
maxDriveNum = num
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check if this drive already exists (by checking device.conf drive ID mapping)
|
||||
// For now, we'll use sequential numbering
|
||||
// Find if drive entry already exists by checking all drives in device.conf for this library
|
||||
deviceConfig, err := s.parseDeviceConfig()
|
||||
if err == nil {
|
||||
driveIndex := 1
|
||||
for _, drive := range deviceConfig.Drives {
|
||||
if drive.LibraryID == libraryID {
|
||||
if drive.ID == driveID {
|
||||
driveNum = driveIndex
|
||||
break
|
||||
}
|
||||
driveIndex++
|
||||
}
|
||||
}
|
||||
if driveNum == 0 {
|
||||
driveNum = maxDriveNum + 1
|
||||
}
|
||||
} else {
|
||||
driveNum = maxDriveNum + 1
|
||||
}
|
||||
|
||||
// Check if Drive entry already exists
|
||||
drivePattern := fmt.Sprintf("Drive %d:", driveNum)
|
||||
for _, line := range lines {
|
||||
if strings.Contains(strings.TrimSpace(line), drivePattern) {
|
||||
driveExists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// If drive already exists, no need to update
|
||||
if driveExists {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Build new lines
|
||||
var newLines []string
|
||||
insertedDrive := false
|
||||
afterVersion := false
|
||||
pickerFound := false
|
||||
|
||||
for _, line := range lines {
|
||||
trimmed := strings.TrimSpace(line)
|
||||
|
||||
// Track position after VERSION
|
||||
if strings.Contains(trimmed, "VERSION:") {
|
||||
afterVersion = true
|
||||
newLines = append(newLines, line)
|
||||
continue
|
||||
}
|
||||
|
||||
// Insert Drive entries after VERSION and before Picker
|
||||
if afterVersion && !insertedDrive {
|
||||
if strings.HasPrefix(trimmed, "Picker") {
|
||||
pickerFound = true
|
||||
// Insert drive before Picker
|
||||
newLines = append(newLines, fmt.Sprintf("Drive %d:", driveNum))
|
||||
newLines = append(newLines, "") // Empty line
|
||||
insertedDrive = true
|
||||
} else if strings.HasPrefix(trimmed, "MAP") && !pickerFound {
|
||||
// If no Picker found, insert before MAP
|
||||
newLines = append(newLines, fmt.Sprintf("Drive %d:", driveNum))
|
||||
newLines = append(newLines, "") // Empty line
|
||||
insertedDrive = true
|
||||
}
|
||||
}
|
||||
|
||||
newLines = append(newLines, line)
|
||||
}
|
||||
|
||||
// If drive wasn't inserted, add it at appropriate position
|
||||
if !insertedDrive {
|
||||
// Find position to insert (after VERSION, before Picker or MAP)
|
||||
insertPos := -1
|
||||
for idx, line := range newLines {
|
||||
trimmed := strings.TrimSpace(line)
|
||||
if strings.HasPrefix(trimmed, "Picker") || strings.HasPrefix(trimmed, "MAP") {
|
||||
insertPos = idx
|
||||
break
|
||||
}
|
||||
}
|
||||
if insertPos == -1 {
|
||||
// Add at end
|
||||
newLines = append(newLines, fmt.Sprintf("Drive %d:", driveNum))
|
||||
} else {
|
||||
// Insert at position
|
||||
newLines = append(newLines[:insertPos], append([]string{fmt.Sprintf("Drive %d:", driveNum), ""}, newLines[insertPos:]...)...)
|
||||
}
|
||||
}
|
||||
|
||||
// Write back to file
|
||||
if err := file.Truncate(0); err != nil {
|
||||
return fmt.Errorf("failed to truncate library_contents file: %v", err)
|
||||
}
|
||||
if _, err := file.Seek(0, 0); err != nil {
|
||||
return fmt.Errorf("failed to seek library_contents file: %v", err)
|
||||
}
|
||||
|
||||
writer := bufio.NewWriter(file)
|
||||
for _, line := range newLines {
|
||||
writer.WriteString(line + "\n")
|
||||
}
|
||||
if err := writer.Flush(); err != nil {
|
||||
return fmt.Errorf("failed to flush library_contents file: %v", err)
|
||||
}
|
||||
if err := file.Sync(); err != nil {
|
||||
log.Printf("Warning: failed to sync library_contents file: %v", err)
|
||||
}
|
||||
|
||||
log.Printf("Updated library_contents.%d with Drive %d entry", libraryID, driveNum)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ensureWritableFilesystem checks if filesystem is writable and remounts if needed
|
||||
func (s *VTLService) ensureWritableFilesystem(path string) error {
|
||||
// Try to create a test file to check if writable
|
||||
@@ -1498,9 +2258,12 @@ func (s *VTLService) remountReadWrite(path string) error {
|
||||
// Try /opt first, then /, then use findmnt
|
||||
var mountPoint string
|
||||
|
||||
// Check if path is under /opt
|
||||
// Check if path is under /opt or /etc
|
||||
if strings.HasPrefix(path, "/opt") {
|
||||
mountPoint = "/opt"
|
||||
} else if strings.HasPrefix(path, "/etc") {
|
||||
// For /etc, we need to remount root filesystem
|
||||
mountPoint = "/"
|
||||
} else {
|
||||
mountPoint = "/"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user