Skip to content

Commit

Permalink
mount: Add a proper rollback path to addStorages()
Browse files Browse the repository at this point in the history
We need a proper rollback to decrement the sandbox storage
reference counter when addStorages() failed.

Fixes: kata-containers#549

Signed-off-by: Xie Yongji <xieyongji@baidu.com>
  • Loading branch information
Xie Yongji committed May 16, 2019
1 parent f1fd336 commit c66349b
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,28 @@ func mountStorage(storage pb.Storage) error {
// associated operations such as waiting for the device to show up, and mount
// it to a specific location, according to the type of handler chosen, and for
// each storage.
func addStorages(ctx context.Context, storages []*pb.Storage, s *sandbox) ([]string, error) {
func addStorages(ctx context.Context, storages []*pb.Storage, s *sandbox) (mounts []string, err error) {
span, ctx := trace(ctx, "mount", "addStorages")
span.SetTag("sandbox", s.id)
defer span.Finish()

var mountList []string
var storageList []string

defer func() {
if err != nil {
s.Lock()
for _, path := range storageList {
if err := s.unsetAndRemoveSandboxStorage(path); err != nil {
agentLog.WithFields(logrus.Fields{
"error": err,
"path": path,
}).Error("failed to roll back addStorages")
}
}
s.Unlock()
}
}()

for _, storage := range storages {
if storage == nil {
Expand All @@ -342,6 +358,10 @@ func addStorages(ctx context.Context, storages []*pb.Storage, s *sandbox) ([]str
mountPoint, err := devHandler(*storage, s)
handlerSpan.Finish()

if _, ok := s.storages[storage.MountPoint]; ok {
storageList = append([]string{storage.MountPoint}, storageList...)
}

if err != nil {
return nil, err
}
Expand Down

0 comments on commit c66349b

Please sign in to comment.