Fix machine volumes with long path and paths with dashes

AppleHV accepts a max 36 bytes for mount tags. Instead of using the fully qualified path for the mount tag, SHA256 the path, and truncate the shasum to 36 bytes.
Also correctly escape dashes in mounted paths.

Signed-off-by: Ashley Cui <acui@redhat.com>
This commit is contained in:
Ashley Cui
2024-04-05 17:53:54 -04:00
parent ba73c604c4
commit a140c74ba4
5 changed files with 40 additions and 23 deletions

View File

@ -1,7 +1,8 @@
package machine
import (
"strings"
"crypto/sha256"
"encoding/hex"
"github.com/containers/podman/v5/pkg/machine/vmconfigs"
)
@ -29,14 +30,13 @@ func (v VirtIoFs) Kind() string {
return string(VirtIOFsVk)
}
// unitName is the fq path where /'s are replaced with -'s
func (v VirtIoFs) unitName() string {
// delete the leading -
unit := strings.ReplaceAll(v.Target, "/", "-")
if strings.HasPrefix(unit, "-") {
return unit[1:]
}
return unit
// generateTag generates a tag for VirtIOFs mounts.
// AppleHV requires tags to be 36 bytes or fewer.
// SHA256 the path, then truncate to 36 bytes
func (v VirtIoFs) generateTag() string {
sum := sha256.Sum256([]byte(v.Target))
stringSum := hex.EncodeToString(sum[:])
return stringSum[:36]
}
func (v VirtIoFs) ToMount() vmconfigs.Mount {
@ -58,7 +58,7 @@ func NewVirtIoFsMount(src, target string, readOnly bool) VirtIoFs {
Source: src,
Target: target,
}
vfs.Tag = vfs.unitName()
vfs.Tag = vfs.generateTag()
return vfs
}