|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: MPL-2.0 |
| 3 | + |
| 4 | +package common |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "regexp" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | +) |
| 12 | + |
| 13 | +// FindNextAvailableCDROMSlot locates the next available CD-ROM device slot for |
| 14 | +// the specified adapter type. |
| 15 | +func FindNextAvailableCDROMSlot(vmxData map[string]string, adapterType string) (string, error) { |
| 16 | + if adapterType == "" { |
| 17 | + return "", fmt.Errorf("adapter type cannot be empty") |
| 18 | + } |
| 19 | + |
| 20 | + adapterType = strings.ToLower(adapterType) |
| 21 | + |
| 22 | + validAdapters := []string{"ide", "sata", "scsi"} |
| 23 | + isValid := false |
| 24 | + for _, valid := range validAdapters { |
| 25 | + if adapterType == valid { |
| 26 | + isValid = true |
| 27 | + break |
| 28 | + } |
| 29 | + } |
| 30 | + if !isValid { |
| 31 | + return "", fmt.Errorf("invalid adapter type: %s; must be one of %v", adapterType, validAdapters) |
| 32 | + } |
| 33 | + |
| 34 | + devicePattern := regexp.MustCompile(fmt.Sprintf(`^%s(\d+):(\d+)\.present$`, adapterType)) |
| 35 | + usedSlots := make(map[string]map[int]bool) // bus -> slot -> used |
| 36 | + |
| 37 | + for key := range vmxData { |
| 38 | + if matches := devicePattern.FindStringSubmatch(key); matches != nil { |
| 39 | + bus := matches[1] |
| 40 | + slot, err := strconv.Atoi(matches[2]) |
| 41 | + if err != nil { |
| 42 | + continue |
| 43 | + } |
| 44 | + |
| 45 | + if usedSlots[bus] == nil { |
| 46 | + usedSlots[bus] = make(map[int]bool) |
| 47 | + } |
| 48 | + usedSlots[bus][slot] = true |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + // Find the next available slot. |
| 53 | + for busNum := 0; busNum < 4; busNum++ { // VMware supports up to 4 buses for most adapter types |
| 54 | + busStr := strconv.Itoa(busNum) |
| 55 | + busSlots := usedSlots[busStr] |
| 56 | + |
| 57 | + // Check slots 0-15 (or 0-6,8-15 for SCSI to skip reserved slot 7). |
| 58 | + maxSlots := 16 |
| 59 | + if adapterType == "ide" { |
| 60 | + maxSlots = 2 // IDE typically supports only 2 devices per bus. |
| 61 | + } |
| 62 | + |
| 63 | + for slot := 0; slot < maxSlots; slot++ { |
| 64 | + // Skip reserved slot 7 for SCSI adapters. |
| 65 | + if adapterType == "scsi" && slot == 7 { |
| 66 | + continue |
| 67 | + } |
| 68 | + |
| 69 | + if busSlots == nil || !busSlots[slot] { |
| 70 | + return fmt.Sprintf("%s%d:%d", adapterType, busNum, slot), nil |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return "", fmt.Errorf("no available CD-ROM slots found for adapter type %s", adapterType) |
| 76 | +} |
| 77 | + |
| 78 | +// AttachCDROMDevice adds CD-ROM device entries to VMX data for the specified |
| 79 | +// device path and ISO file. |
| 80 | +func AttachCDROMDevice(vmxData map[string]string, devicePath, isoPath, adapterType string) error { |
| 81 | + if vmxData == nil { |
| 82 | + return fmt.Errorf("vmxData cannot be nil") |
| 83 | + } |
| 84 | + if devicePath == "" { |
| 85 | + return fmt.Errorf("devicePath cannot be empty") |
| 86 | + } |
| 87 | + if isoPath == "" { |
| 88 | + return fmt.Errorf("isoPath cannot be empty") |
| 89 | + } |
| 90 | + if adapterType == "" { |
| 91 | + return fmt.Errorf("adapterType cannot be empty") |
| 92 | + } |
| 93 | + |
| 94 | + adapterType = strings.ToLower(adapterType) |
| 95 | + devicePattern := regexp.MustCompile(`^(ide|sata|scsi)(\d+):(\d+)$`) |
| 96 | + matches := devicePattern.FindStringSubmatch(devicePath) |
| 97 | + if matches == nil { |
| 98 | + return fmt.Errorf("invalid device path format: %s; expected format like 'ide0:1'", devicePath) |
| 99 | + } |
| 100 | + |
| 101 | + pathAdapterType := matches[1] |
| 102 | + busNum := matches[2] |
| 103 | + |
| 104 | + if pathAdapterType != adapterType { |
| 105 | + return fmt.Errorf("device path adapter type %s does not match specified adapter type %s", pathAdapterType, adapterType) |
| 106 | + } |
| 107 | + |
| 108 | + presentKey := fmt.Sprintf("%s.present", devicePath) |
| 109 | + if existing, exists := vmxData[presentKey]; exists && strings.ToLower(existing) == "true" { |
| 110 | + return fmt.Errorf("device %s is already in use", devicePath) |
| 111 | + } |
| 112 | + |
| 113 | + adapterKey := fmt.Sprintf("%s%s.present", adapterType, busNum) |
| 114 | + vmxData[adapterKey] = "TRUE" |
| 115 | + |
| 116 | + vmxData[fmt.Sprintf("%s.present", devicePath)] = "TRUE" |
| 117 | + vmxData[fmt.Sprintf("%s.filename", devicePath)] = isoPath |
| 118 | + vmxData[fmt.Sprintf("%s.devicetype", devicePath)] = "cdrom-image" |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +// DetachCDROMDevice removes CD-ROM device entries from VMX data for the |
| 124 | +// specified device path. |
| 125 | +func DetachCDROMDevice(vmxData map[string]string, devicePath string) error { |
| 126 | + if vmxData == nil { |
| 127 | + return fmt.Errorf("vmxData cannot be nil") |
| 128 | + } |
| 129 | + if devicePath == "" { |
| 130 | + return fmt.Errorf("devicePath cannot be empty") |
| 131 | + } |
| 132 | + |
| 133 | + devicePattern := regexp.MustCompile(`^(ide|sata|scsi)(\d+):(\d+)$`) |
| 134 | + if !devicePattern.MatchString(devicePath) { |
| 135 | + return fmt.Errorf("invalid device path format: %s; expected format like 'ide0:1'", devicePath) |
| 136 | + } |
| 137 | + |
| 138 | + devicePrefix := devicePath + "." |
| 139 | + keysToDelete := make([]string, 0) |
| 140 | + |
| 141 | + for key := range vmxData { |
| 142 | + if strings.HasPrefix(key, devicePrefix) { |
| 143 | + keysToDelete = append(keysToDelete, key) |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + for _, key := range keysToDelete { |
| 148 | + delete(vmxData, key) |
| 149 | + } |
| 150 | + |
| 151 | + return nil |
| 152 | +} |
0 commit comments