1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
// Copyright 2021 Canonical Ltd.
// Licensed under the LGPLv3 with static-linking exception.
// See LICENCE file for details.
package linux
import (
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
const (
virtioBlockDevice uint32 = 2
virtioSCSIHost uint32 = 8
)
var (
blkRE = regexp.MustCompile(`^block\/vd[[:alpha:]]$`)
virtioRE = regexp.MustCompile(`^virtio[[:digit:]]`)
)
func handleVirtioDevicePathNode(state *devicePathBuilderState) error {
if !virtioRE.MatchString(state.PeekUnhandledSysfsComponents(1)) {
return errors.New("invalid path")
}
state.AdvanceSysfsPath(1)
data, err := os.ReadFile(filepath.Join(state.SysfsPath(), "modalias"))
if err != nil {
return err
}
var device uint32
var vendor uint32
n, err := fmt.Sscanf(strings.TrimSpace(string(data)), "virtio:d%08xv%08x", &device, &vendor)
if err != nil {
return fmt.Errorf("cannot scan modalias: %w", err)
}
if n != 2 {
return errors.New("invalid modalias format")
}
switch device {
case virtioBlockDevice:
if !blkRE.MatchString(state.PeekUnhandledSysfsComponents(2)) {
return errors.New("invalid path for block device")
}
state.AdvanceSysfsPath(2)
case virtioSCSIHost:
state.Interface = interfaceTypeSCSI
default:
return fmt.Errorf("unrecognized virtio device type %#08x", device)
}
return nil
}
|