diff --git a/blk/blk.go b/blk/blk.go index a695224..44a8f90 100644 --- a/blk/blk.go +++ b/blk/blk.go @@ -98,8 +98,11 @@ type Mounter struct { func NewMounterFromPreset(p *mnt.Preset) (mnt.Mounter, error) { preset := *p - if preset.Path == "" { - return nil, errors.New("preset path is empty") + if preset.Path == "" && preset.UUID == "" { + return nil, errors.New("preset path and uuid cannot be both empty") + } + if preset.UUID != "" && !IsValidUUID(preset.UUID) { + return nil, errors.New("invalid UUID") } if !util.IsValidMountPoint(preset.MountPoint) { return nil, errors.Errorf("invalid mount point %q", preset.MountPoint) @@ -114,7 +117,12 @@ func NewMounterFromPreset(p *mnt.Preset) (mnt.Mounter, error) { } func (m *Mounter) refresh() error { - devs, err := List(m.preset.Path) + queryPath := m.preset.Path + if m.preset.UUID != "" { + queryPath = "/dev/disk/by-uuid/" + m.preset.UUID + } + + devs, err := List(queryPath) if err != nil { return errors.Wrap(err, 0) } diff --git a/blk/uuid.go b/blk/uuid.go new file mode 100644 index 0000000..8768b80 --- /dev/null +++ b/blk/uuid.go @@ -0,0 +1,9 @@ +package blk + +import "regexp" + +var blkUuidPattern = regexp.MustCompile("^[0-9a-zA-Z-]+$") + +func IsValidUUID(uuid string) bool { + return blkUuidPattern.MatchString(uuid) +} diff --git a/mnt/preset.go b/mnt/preset.go index 8d5061d..e3c0e71 100644 --- a/mnt/preset.go +++ b/mnt/preset.go @@ -16,6 +16,7 @@ type Preset struct { Name string `yaml:"name"` Type string `yaml:"type"` Path string `yaml:"path"` + UUID string `yaml:"uuid"` MountPoint string `yaml:"mountpoint"` AuthCmd string `yaml:"auth_cmd"` // e.g. for loading the encryption key