-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65b06eb
commit f8c1504
Showing
7 changed files
with
557 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package api | ||
|
||
// API operations on rbd maps | ||
|
||
import ( | ||
"os" | ||
"path" | ||
|
||
"github.com/kraken-hpc/imageapi/models" | ||
"github.com/sirupsen/logrus" | ||
"github.com/u-root/u-root/pkg/mount/loop" | ||
) | ||
|
||
func init() { | ||
AttachDrivers[models.AttachKindLoopback] = &AttachDriverLoopback{} | ||
} | ||
|
||
type AttachDriverLoopback struct { | ||
log *logrus.Entry | ||
} | ||
|
||
func (a *AttachDriverLoopback) Init(log *logrus.Entry) { | ||
a.log = log | ||
a.log.Trace("initialized") | ||
} | ||
|
||
func (a *AttachDriverLoopback) Attach(att *Attach) (ret *Attach, err error) { | ||
// sanity check | ||
l := a.log.WithField("operation", "attach") | ||
if att.Loopback == nil { | ||
l.Trace("attempted to attach loopback with no loopback definition") | ||
return nil, ERRINVALDAT | ||
} | ||
l = l.WithFields(logrus.Fields{ | ||
"path": att.Loopback.Path, | ||
"base": att.Loopback.Base, | ||
}) | ||
|
||
base := "/" | ||
if *att.Loopback.Base == models.MountBindBaseMount { | ||
if att.Loopback.Mount == nil { | ||
l.Debug("bind mount called with mount base but no mount definition") | ||
return nil, ERRINVALDAT | ||
} | ||
m, err := API.Mounts.GetOrMount((*Mount)(att.Loopback.Mount)) | ||
if err != nil { | ||
l.Error("base mount failed to GetOrMount") | ||
return nil, ERRFAIL | ||
} | ||
att.Loopback.Mount = (*models.Mount)(m) | ||
defer func() { | ||
if err != nil { | ||
API.Store.RefAdd(att.Loopback.Mount.ID, -1) | ||
} | ||
}() | ||
base = att.Loopback.Mount.Mountpoint | ||
} | ||
fullPath := path.Join(base, *att.Loopback.Path) | ||
l.WithField("fullPath", fullPath) | ||
info, err := os.Stat(fullPath) | ||
if err != nil { | ||
l.Debug("loopback attach called on file that doesn't exist") | ||
return nil, ERRINVALDAT | ||
} | ||
if !info.Mode().IsRegular() { | ||
l.Debug("loopback attach call on a non-regular file") | ||
return nil, ERRINVALDAT | ||
} | ||
|
||
att.DeviceFile, err = loop.FindDevice() | ||
if err != nil { | ||
l.WithError(err).Error("failed to acquire loopback device") | ||
return nil, ERRFAIL | ||
} | ||
if err = loop.SetFile(att.DeviceFile, fullPath); err != nil { | ||
l.WithError(err).Debug("failed to assign file to loopback device") | ||
return nil, ERRFAIL | ||
} | ||
|
||
l.Info("successfully mapped") | ||
return att, err | ||
} | ||
|
||
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, | ||
}) | ||
if err = loop.ClearFile(att.DeviceFile); err != nil { | ||
l.Debug("failed to clear loopback association") | ||
return nil, ERRFAIL | ||
} | ||
return att, nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.