Skip to content

Commit

Permalink
lxd/instance/drivers: Indicate 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 5, 2024
1 parent 6eb67f2 commit 342bef9
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 @@ -111,6 +111,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_"

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

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

Expand Down Expand Up @@ -8906,15 +8909,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 {
maxNameLength := qemuDeviceNameMaxLength - len(qemuDeviceNamePrefix)
if len(name) > maxNameLength {
// 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:maxNameLength]
}

// Apply the lxd_ prefix.
Expand Down

0 comments on commit 342bef9

Please sign in to comment.