Skip to content

Commit b16f9cd

Browse files
committed
BACKPORT: take reference to RWLayer while committing/exporting
Upstream reference: moby#30696 Trello: https://trello.com/c/vNlHE2kV Signed-off-by: Antonio Murdaca <runcom@redhat.com>
1 parent c4618fb commit b16f9cd

3 files changed

Lines changed: 55 additions & 14 deletions

File tree

container/state.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,30 @@ func (s *State) ResetRemovalInProgress() {
322322
s.Unlock()
323323
}
324324

325+
// IsRemovalInProgress returns whether the RemovalInProgress flag is set.
326+
// Used by Container to check whether a container is being removed.
327+
func (s *State) IsRemovalInProgress() bool {
328+
s.Lock()
329+
res := s.RemovalInProgress
330+
s.Unlock()
331+
return res
332+
}
333+
325334
// SetDead sets the container state to "dead"
326335
func (s *State) SetDead() {
327336
s.Lock()
328337
s.Dead = true
329338
s.Unlock()
330339
}
331340

341+
// IsDead returns whether the Dead flag is set. Used by Container to check whether a container is dead.
342+
func (s *State) IsDead() bool {
343+
s.Lock()
344+
res := s.Dead
345+
s.Unlock()
346+
return res
347+
}
348+
332349
// Error returns current error for the state.
333350
func (s *State) Error() string {
334351
return s.ErrorMsg

daemon/commit.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/docker/docker/image"
1515
"github.com/docker/docker/layer"
1616
"github.com/docker/docker/pkg/archive"
17-
"github.com/docker/docker/pkg/ioutils"
1817
"github.com/docker/docker/reference"
1918
containertypes "github.com/docker/engine-api/types/container"
2019
"github.com/docker/go-connections/nat"
@@ -128,6 +127,14 @@ func (daemon *Daemon) Commit(name string, c *backend.ContainerCommitConfig) (str
128127
return "", fmt.Errorf("Windows does not support commit of a running container")
129128
}
130129

130+
if container.IsDead() {
131+
return "", fmt.Errorf("You cannot commit container %s which is Dead", container.ID)
132+
}
133+
134+
if container.IsRemovalInProgress() {
135+
return "", fmt.Errorf("You cannot commit container %s which is being removed", container.ID)
136+
}
137+
131138
if c.Pause && !container.IsPaused() {
132139
daemon.containerPause(container)
133140
defer daemon.containerUnpause(container)
@@ -246,18 +253,15 @@ func (daemon *Daemon) Commit(name string, c *backend.ContainerCommitConfig) (str
246253
}
247254

248255
func (daemon *Daemon) exportContainerRw(container *container.Container) (archive.Archive, error) {
249-
if err := daemon.Mount(container); err != nil {
256+
rwLayer, err := daemon.layerStore.GetRWLayer(container.ID)
257+
if err != nil {
250258
return nil, err
251259
}
260+
defer daemon.layerStore.ReleaseRWLayer(rwLayer)
252261

253-
archive, err := container.RWLayer.TarStream()
262+
archive, err := rwLayer.TarStream()
254263
if err != nil {
255-
daemon.Unmount(container) // logging is already handled in the `Unmount` function
256264
return nil, err
257265
}
258-
return ioutils.NewReadCloserWrapper(archive, func() error {
259-
archive.Close()
260-
return container.RWLayer.Unmount()
261-
}),
262-
nil
266+
return archive, nil
263267
}

daemon/export.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ func (daemon *Daemon) ContainerExport(name string, out io.Writer) error {
1717
return err
1818
}
1919

20+
if container.IsDead() {
21+
return fmt.Errorf("You cannot export container %s which is Dead", container.ID)
22+
}
23+
24+
if container.IsRemovalInProgress() {
25+
return fmt.Errorf("You cannot export container %s which is being removed", container.ID)
26+
}
27+
2028
data, err := daemon.containerExport(container)
2129
if err != nil {
2230
return fmt.Errorf("Error exporting container %s: %v", name, err)
@@ -30,8 +38,19 @@ func (daemon *Daemon) ContainerExport(name string, out io.Writer) error {
3038
return nil
3139
}
3240

33-
func (daemon *Daemon) containerExport(container *container.Container) (archive.Archive, error) {
34-
if err := daemon.Mount(container); err != nil {
41+
func (daemon *Daemon) containerExport(container *container.Container) (arch archive.Archive, err error) {
42+
rwLayer, err := daemon.layerStore.GetRWLayer(container.ID)
43+
if err != nil {
44+
return nil, err
45+
}
46+
defer func() {
47+
if err != nil {
48+
daemon.layerStore.ReleaseRWLayer(rwLayer)
49+
}
50+
}()
51+
52+
_, err = rwLayer.Mount(container.GetMountLabel())
53+
if err != nil {
3554
return nil, err
3655
}
3756

@@ -42,12 +61,13 @@ func (daemon *Daemon) containerExport(container *container.Container) (archive.A
4261
GIDMaps: gidMaps,
4362
})
4463
if err != nil {
45-
daemon.Unmount(container)
64+
rwLayer.Unmount()
4665
return nil, err
4766
}
48-
arch := ioutils.NewReadCloserWrapper(archive, func() error {
67+
arch = ioutils.NewReadCloserWrapper(archive, func() error {
4968
err := archive.Close()
50-
daemon.Unmount(container)
69+
rwLayer.Unmount()
70+
daemon.layerStore.ReleaseRWLayer(rwLayer)
5171
return err
5272
})
5373
daemon.LogContainerEvent(container, "export")

0 commit comments

Comments
 (0)