Skip to content

Commit

Permalink
Add local attach support (#40)
Browse files Browse the repository at this point in the history
* add local attach support

* use os.Mode* for backwards compatability

* fix incorrect driver naming for attach local

* fix some incorrect pointer references in log messages

* log message tweaks for attach-local
  • Loading branch information
jlowellwofford authored Jul 26, 2021
1 parent f8c1504 commit b73b9e9
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 12 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/kraken-hpc/imageapi
go 1.15

require (
github.com/u-root/u-root v7.0.0+incompatible
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d // indirect
github.com/bensallen/rbd v0.0.0-20210224155049-baf486eceefa
github.com/go-openapi/errors v0.20.0
Expand All @@ -19,11 +18,12 @@ require (
github.com/kraken-hpc/uinit v0.1.1
github.com/mailru/easyjson v0.7.7 // indirect
github.com/sirupsen/logrus v1.8.1
github.com/u-root/u-root v7.0.0+incompatible
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20210331060903-cb1fcc7394e5
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44
google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 // indirect
)

replace github.com/u-root/u-root v7.0.0+incompatible => github.com/u-root/u-root v1.0.1-0.20201119150355-04f343dd1922
replace github.com/u-root/u-root v7.0.0+incompatible => github.com/u-root/u-root v1.0.1-0.20201119150355-04f343dd1922
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,6 @@ github.com/u-root/iscsinl v0.1.0/go.mod h1:UGTrNqGTX3vAz6y/VmHC7pY8FLqoigybttGad
github.com/u-root/u-root v1.0.1-0.20201119150355-04f343dd1922 h1:t9B62nb5RYgZNwrK+gaF//CDcJnYSt1HqE+MAROggM0=
github.com/u-root/u-root v1.0.1-0.20201119150355-04f343dd1922/go.mod h1:c7G35Qvc1z58PWx/p1R9ROyiSuBc0ckqMU74klQ5g/s=
github.com/u-root/u-root v6.0.1-0.20200118052101-6bcd1cda5996+incompatible/go.mod h1:RYkpo8pTHrNjW08opNd/U6p/RJE7K0D8fXO0d47+3YY=
github.com/u-root/u-root v7.0.0+incompatible h1:u+KSS04pSxJGI5E7WE4Bs9+Zd75QjFv+REkjy/aoAc8=
github.com/u-root/u-root v7.0.0+incompatible/go.mod h1:RYkpo8pTHrNjW08opNd/U6p/RJE7K0D8fXO0d47+3YY=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/ulikunitz/xz v0.5.8/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0oWt/14=
github.com/vektah/gqlparser v1.1.2/go.mod h1:1ycwN7Ij5njmMkPPAOaRFY4rET2Enx7IkVv3vaXspKw=
Expand Down
62 changes: 62 additions & 0 deletions internal/api/attach_local.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package api

import (
"os"

"github.com/kraken-hpc/imageapi/models"
"github.com/sirupsen/logrus"
)

func init() {
AttachDrivers[models.AttachKindLocal] = &AttachDriverLocal{}
}

type AttachDriverLocal struct {
log *logrus.Entry
}

func (a *AttachDriverLocal) Init(log *logrus.Entry) {
a.log = log
a.log.Trace("initialized")
}

func (a *AttachDriverLocal) Attach(att *Attach) (ret *Attach, err error) {
// sanity check
l := a.log.WithField("operation", "attach")
if att.Local == nil {
l.Trace("attempted to attach local with no local definition")
return nil, ERRINVALDAT
}
l = l.WithField("path", *att.Local.Path)

finfo, err := os.Stat(*att.Local.Path)
if err != nil {
l.WithError(err).Debug("failed to stat device file")
return nil, ERRFAIL
}

if finfo.Mode()&os.ModeDevice == 0 {
l.Trace("path is not a device file")
return nil, ERRINVALDAT
}

if finfo.Mode()&os.ModeCharDevice != 0 {
l.Trace("path points to character device")
return nil, ERRINVALDAT
}
att.DeviceFile = *att.Local.Path

l.Info("successfully attached")
return att, nil
}

func (a *AttachDriverLocal) Detach(att *Attach) (ret *Attach, err error) {
// this is a dummy operation
l := a.log.WithFields(logrus.Fields{
"operation": "detach",
"id": att.ID,
"path": *att.Local.Path,
})
l.Info("successfully detached")
return att, nil
}
10 changes: 4 additions & 6 deletions internal/api/attach_loopback.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package api

// API operations on rbd maps

import (
"os"
"path"
Expand Down Expand Up @@ -32,8 +30,8 @@ func (a *AttachDriverLoopback) Attach(att *Attach) (ret *Attach, err error) {
return nil, ERRINVALDAT
}
l = l.WithFields(logrus.Fields{
"path": att.Loopback.Path,
"base": att.Loopback.Base,
"path": *att.Loopback.Path,
"base": *att.Loopback.Base,
})

base := "/"
Expand Down Expand Up @@ -85,8 +83,8 @@ func (a *AttachDriverLoopback) Detach(att *Attach) (ret *Attach, err error) {
l := a.log.WithFields(logrus.Fields{
"operation": "unmap",
"id": att.ID,
"path": att.Loopback.Path,
"base": att.Loopback.Base,
"path": *att.Loopback.Path,
"base": *att.Loopback.Base,
})
if err = loop.ClearFile(att.DeviceFile); err != nil {
l.Debug("failed to clear loopback association")
Expand Down
4 changes: 2 additions & 2 deletions internal/api/mount_nfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func (m *MountDriverNFS) Unmount(mnt *Mount) (ret *Mount, err error) {
l := m.log.WithFields(logrus.Fields{
"operation": "unmount",
"id": mnt.ID,
"host": mnt.Nfs.Host,
"path": mnt.Nfs.Path,
"host": *mnt.Nfs.Host,
"path": *mnt.Nfs.Path,
})

// always lazy unmount. Good idea?
Expand Down
42 changes: 42 additions & 0 deletions models/attach.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 76 additions & 0 deletions models/attach_local.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions restapi/embedded_spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,21 @@ definitions:
mount:
$ref: "#/definitions/mount"

attach_local:
description: |
`attach_local` describes a block device that is locally present.
This can be used to get a reference to a local disk, for instance.
Local only supports finding device files on the local (root) system.
It only takes one parameter: the path to the device file.
type: object
required:
- path
properties:
path:
type: string
description: A unix-formatted filesystem path pointing to a block device file.

attach:
description: |
Generically address attachments. Attachments are objects that ultimately provide a block device file.
Expand Down Expand Up @@ -197,6 +212,8 @@ definitions:
readOnly: true
rbd:
$ref: "#/definitions/attach_rbd"
local:
$ref: "#/definitions/attach_local"
loopback:
$ref: "#/definitions/attach_loopback"

Expand Down

0 comments on commit b73b9e9

Please sign in to comment.