feat: support blk UUID in preset

This commit is contained in:
Yiyang Kang 2023-03-04 13:10:57 +08:00
parent a6a1efc397
commit 0a56aa51e7
3 changed files with 21 additions and 3 deletions

View File

@ -98,8 +98,11 @@ type Mounter struct {
func NewMounterFromPreset(p *mnt.Preset) (mnt.Mounter, error) { func NewMounterFromPreset(p *mnt.Preset) (mnt.Mounter, error) {
preset := *p preset := *p
if preset.Path == "" { if preset.Path == "" && preset.UUID == "" {
return nil, errors.New("preset path is empty") 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) { if !util.IsValidMountPoint(preset.MountPoint) {
return nil, errors.Errorf("invalid mount point %q", 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 { 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 { if err != nil {
return errors.Wrap(err, 0) return errors.Wrap(err, 0)
} }

9
blk/uuid.go Normal file
View File

@ -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)
}

View File

@ -16,6 +16,7 @@ type Preset struct {
Name string `yaml:"name"` Name string `yaml:"name"`
Type string `yaml:"type"` Type string `yaml:"type"`
Path string `yaml:"path"` Path string `yaml:"path"`
UUID string `yaml:"uuid"`
MountPoint string `yaml:"mountpoint"` MountPoint string `yaml:"mountpoint"`
AuthCmd string `yaml:"auth_cmd"` // e.g. for loading the encryption key AuthCmd string `yaml:"auth_cmd"` // e.g. for loading the encryption key