Skip to content

Commit

Permalink
lxd/instance/drivers: Indicate name device name max length with variable
Browse files Browse the repository at this point in the history
Signed-off-by: hamistao <pedro.ribeiro@canonical.com>
  • Loading branch information
hamistao committed Jun 2, 2024
1 parent 87ffb7c commit 1513425
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lxd/instance/drivers/driver_qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ const qemuDeviceIDPrefix = "dev-lxd_"
// qemuDeviceNamePrefix used as part of the name given QEMU blockdevs, netdevs and device tags generated from user added devices.
const qemuDeviceNamePrefix = "lxd_"

// qemuDeviceNameMaxLenght used to indicate the maximum length of a qemu block node name and device tags.
const qemuDeviceNameMaxLenght = 31

// qemuMigrationNBDExportName is the name of the disk device export by the migration NBD server.
const qemuMigrationNBDExportName = "lxd_root"

Expand Down Expand Up @@ -8895,15 +8898,16 @@ func (d *qemu) deviceDetachUSB(usbDev deviceConfig.USBDeviceItem) error {

// Block node names and device tags may only be up to 31 characters long, so use a hash if longer.
func (d *qemu) generateQemuDeviceName(name string) string {
if len(name) > 27 {
maxNameLenght := qemuDeviceNameMaxLenght - len(qemuDeviceNamePrefix)
if len(name) > maxNameLenght {
// If the name is too long, hash it as SHA-256 (32 bytes).
// Then encode the SHA-256 binary hash as Base64 Raw URL format and trim down to 27 chars.
// Raw URL avoids the use of "+" character and the padding "=" character which QEMU doesn't allow.
hash := sha256.New()
hash.Write([]byte(name))
binaryHash := hash.Sum(nil)
name = base64.RawURLEncoding.EncodeToString(binaryHash)
name = name[0:27]
name = name[0:maxNameLenght]
}

// Apply the lxd_ prefix.
Expand Down

0 comments on commit 1513425

Please sign in to comment.