Skip to content

Commit

Permalink
Add a boolean return form unmount to indicate when the layer is reall…
Browse files Browse the repository at this point in the history
…y unmounted

Signed-off-by: Daniel J Walsh <[email protected]>
  • Loading branch information
rhatdan committed Jul 16, 2018
1 parent 7e4d33e commit 25806b7
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 16 deletions.
1 change: 1 addition & 0 deletions cmd/containers-storage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type command struct {
var (
commands = []command{}
jsonOutput = false
force = false
)

func main() {
Expand Down
46 changes: 45 additions & 1 deletion cmd/containers-storage/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,45 @@ func unmount(flags *mflag.FlagSet, action string, m storage.Store, args []string
mes := []mountPointError{}
errors := false
for _, arg := range args {
err := m.Unmount(arg, false)
mounted, err := m.Unmount(arg, force)
errText := ""
if err != nil {
errText = fmt.Sprintf("%v", err)
errors = true
}
if !mounted {
fmt.Printf("%s mountpoint unmounted\n", arg)
}
mes = append(mes, mountPointError{arg, errText})
}
if jsonOutput {
json.NewEncoder(os.Stdout).Encode(mes)
} else {
for _, me := range mes {
if me.Error != "" {
fmt.Fprintf(os.Stderr, "%s while unmounting %s\n", me.Error, me.ID)
}
}
}
if errors {
return 1
}
return 0
}

func mounted(flags *mflag.FlagSet, action string, m storage.Store, args []string) int {
mes := []mountPointError{}
errors := false
for _, arg := range args {
mounted, err := m.Mounted(arg)
errText := ""
if err != nil {
errText = fmt.Sprintf("%v", err)
errors = true
}
if mounted {
fmt.Printf("%s mounted\n", arg)
}
mes = append(mes, mountPointError{arg, errText})
}
if jsonOutput {
Expand Down Expand Up @@ -92,6 +125,17 @@ func init() {
usage: "Unmount a layer or container",
minArgs: 1,
action: unmount,
addFlags: func(flags *mflag.FlagSet, cmd *command) {
flags.BoolVar(&jsonOutput, []string{"-json", "j"}, jsonOutput, "Prefer JSON output")
flags.BoolVar(&force, []string{"-force", "f"}, jsonOutput, "Force the umount")
},
})
commands = append(commands, command{
names: []string{"mounted"},
optionsHelp: "LayerOrContainerNameOrID",
usage: "Check if a file system is mounted",
minArgs: 1,
action: mounted,
addFlags: func(flags *mflag.FlagSet, cmd *command) {
flags.BoolVar(&jsonOutput, []string{"-json", "j"}, jsonOutput, "Prefer JSON output")
},
Expand Down
19 changes: 10 additions & 9 deletions layers.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ type LayerStore interface {
Mount(id, mountLabel string) (string, error)

// Unmount unmounts a layer when it is no longer in use.
Unmount(id string, force bool) error
Unmount(id string, force bool) (bool, error)

// Mounted returns whether or not the layer is mounted
// Mounted returns whether or not the layer is mounted.
Mounted(id string) (bool, error)

// ParentOwners returns the UIDs and GIDs of parents of the layer's mountpoint
Expand Down Expand Up @@ -663,15 +663,15 @@ func (r *layerStore) Mount(id, mountLabel string) (string, error) {
return mountpoint, err
}

func (r *layerStore) Unmount(id string, force bool) error {
func (r *layerStore) Unmount(id string, force bool) (bool, error) {
if !r.IsReadWrite() {
return errors.Wrapf(ErrStoreIsReadOnly, "not allowed to update mount locations for layers at %q", r.mountspath())
return false, errors.Wrapf(ErrStoreIsReadOnly, "not allowed to update mount locations for layers at %q", r.mountspath())
}
layer, ok := r.lookup(id)
if !ok {
layerByMount, ok := r.bymount[filepath.Clean(id)]
if !ok {
return ErrLayerUnknown
return false, ErrLayerUnknown
}
layer = layerByMount
}
Expand All @@ -680,7 +680,7 @@ func (r *layerStore) Unmount(id string, force bool) error {
}
if layer.MountCount > 1 {
layer.MountCount--
return r.Save()
return true, r.Save()
}
err := r.driver.Put(id)
if err == nil || os.IsNotExist(err) {
Expand All @@ -691,7 +691,7 @@ func (r *layerStore) Unmount(id string, force bool) error {
layer.MountPoint = ""
err = r.Save()
}
return err
return err != nil, err
}

func (r *layerStore) ParentOwners(id string) (uids, gids []int, err error) {
Expand Down Expand Up @@ -811,7 +811,7 @@ func (r *layerStore) Delete(id string) error {
return ErrLayerUnknown
}
id = layer.ID
if err := r.Unmount(id, true); err != nil {
if _, err := r.Unmount(id, true); err != nil {
return err
}
err := r.driver.Remove(id)
Expand Down Expand Up @@ -929,7 +929,8 @@ func (s *simpleGetCloser) Get(path string) (io.ReadCloser, error) {
}

func (s *simpleGetCloser) Close() error {
return s.r.Unmount(s.id, false)
_, err := s.r.Unmount(s.id, false)
return err
}

func (r *layerStore) newFileGetter(id string) (drivers.FileGetCloser, error) {
Expand Down
15 changes: 9 additions & 6 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,11 @@ type Store interface {
Mount(id, mountLabel string) (string, error)

// Unmount attempts to unmount a layer, image, or container, given an ID, a
// name, or a mount path.
Unmount(id string, force bool) error
// name, or a mount path. Returns whether or not the layer is still mounted.
Unmount(id string, force bool) (bool, error)

// Unmount attempts to discover whether the specified id is mounted.
Mounted(id string) (bool, error)

// Changes returns a summary of the changes which would need to be made
// to one layer to make its contents the same as a second layer. If
Expand Down Expand Up @@ -2262,13 +2265,13 @@ func (s *store) Mounted(id string) (bool, error) {
return rlstore.Mounted(id)
}

func (s *store) Unmount(id string, force bool) error {
func (s *store) Unmount(id string, force bool) (bool, error) {
if layerID, err := s.ContainerLayerID(id); err == nil {
id = layerID
}
rlstore, err := s.LayerStore()
if err != nil {
return err
return false, err
}
rlstore.Lock()
defer rlstore.Unlock()
Expand All @@ -2278,7 +2281,7 @@ func (s *store) Unmount(id string, force bool) error {
if rlstore.Exists(id) {
return rlstore.Unmount(id, force)
}
return ErrLayerUnknown
return false, ErrLayerUnknown
}

func (s *store) Changes(from, to string) ([]archive.Change, error) {
Expand Down Expand Up @@ -2828,7 +2831,7 @@ func (s *store) Shutdown(force bool) ([]string, error) {
mounted = append(mounted, layer.ID)
if force {
for layer.MountCount > 0 {
err2 := rlstore.Unmount(layer.ID, force)
_, err2 := rlstore.Unmount(layer.ID, force)
if err2 != nil {
if err == nil {
err = err2
Expand Down
81 changes: 81 additions & 0 deletions tests/mount-layer.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/env bats

load helpers

@test "mount-layer" {
# Create a layer.
run storage --debug=false create-layer
[ "$status" -eq 0 ]
[ "$output" != "" ]
layer="$output"

# Mount the layer.
run storage --debug=false mount $layer
[ "$status" -eq 0 ]
[ "$output" != "" ]
# Check it layer is mounted
run storage --debug=false mounted $layer
[ "$status" -eq 0 ]
[ "$output" != "" ]
# Unmount the layer.
run storage --debug=false unmount $layer
[ "$status" -eq 0 ]
[ "$output" != "" ]
# Check it layer is mounted
run storage --debug=false mounted $layer
[ "$status" -eq 0 ]
[ "$output" == "" ]

# Mount the layer twice.
run storage --debug=false mount $layer
[ "$status" -eq 0 ]
[ "$output" != "" ]
run storage --debug=false mount $layer
[ "$status" -eq 0 ]
[ "$output" != "" ]
# Check it layer is mounted
run storage --debug=false mounted $layer
[ "$status" -eq 0 ]
[ "$output" != "" ]
# Unmount the third layer.
run storage --debug=false unmount $layer
[ "$status" -eq 0 ]
[ "$output" == "" ]
# Check it layer is mounted
run storage --debug=false mounted $layer
[ "$status" -eq 0 ]
[ "$output" != "" ]
# Unmount the third layer.
run storage --debug=false unmount $layer
[ "$status" -eq 0 ]
[ "$output" != "" ]
# Check it layer is mounted
run storage --debug=false mounted $layer
[ "$status" -eq 0 ]
[ "$output" == "" ]


# Mount the layer twice and force umount
run storage --debug=false mount $layer
[ "$status" -eq 0 ]
[ "$output" != "" ]
run storage --debug=false mount $layer
[ "$status" -eq 0 ]
[ "$output" != "" ]
# Check it layer is mounted
run storage --debug=false mounted $layer
[ "$status" -eq 0 ]
[ "$output" != "" ]
# Unmount the third layer.
run storage --debug=false unmount --force $layer
[ "$status" -eq 0 ]
[ "$output" != "" ]
# Check it layer is mounted
run storage --debug=false mounted $layer
[ "$status" -eq 0 ]
[ "$output" == "" ]

# Try to delete the first layer, which should fail because it has children.
run storage delete-layer $layer
[ "$status" -eq 0 ]
}

0 comments on commit 25806b7

Please sign in to comment.