diff options
Diffstat (limited to 'libpod')
-rw-r--r-- | libpod/container_api.go | 21 | ||||
-rw-r--r-- | libpod/container_internal.go | 28 | ||||
-rw-r--r-- | libpod/image/image.go | 10 | ||||
-rw-r--r-- | libpod/image/image_test.go | 4 | ||||
-rw-r--r-- | libpod/image/pull.go | 23 | ||||
-rw-r--r-- | libpod/storage.go | 15 |
6 files changed, 78 insertions, 23 deletions
diff --git a/libpod/container_api.go b/libpod/container_api.go index a3756ac5f..bb9727ec1 100644 --- a/libpod/container_api.go +++ b/libpod/container_api.go @@ -437,11 +437,7 @@ func (c *Container) Mount() (string, error) { } } - if err := c.mountStorage(); err != nil { - return "", err - } - - return c.state.Mountpoint, nil + return c.mount() } // Unmount unmounts a container's filesystem on the host @@ -456,15 +452,24 @@ func (c *Container) Unmount() error { } if c.state.State == ContainerStateRunning || c.state.State == ContainerStatePaused { - return errors.Wrapf(ErrCtrStateInvalid, "cannot remove storage for container %s as it is running or paused", c.ID()) + return errors.Wrapf(ErrCtrStateInvalid, "cannot unmount storage for container %s as it is running or paused", c.ID()) } // Check if we have active exec sessions if len(c.state.ExecSessions) != 0 { - return errors.Wrapf(ErrCtrStateInvalid, "container %s has active exec sessions, refusing to clean up", c.ID()) + return errors.Wrapf(ErrCtrStateInvalid, "container %s has active exec sessions, refusing to unmount", c.ID()) } - return c.cleanupStorage() + if c.state.Mounted { + mounted, err := c.runtime.storageService.MountedContainerImage(c.ID()) + if err != nil { + return errors.Wrapf(err, "can't determine how many times %s is mounted, refusing to unmount", c.ID()) + } + if mounted == 1 { + return errors.Wrapf(err, "can't unmount %s last mount, it is still in use", c.ID()) + } + } + return c.unmount() } // Pause pauses a container diff --git a/libpod/container_internal.go b/libpod/container_internal.go index 38099c6ac..55fd7369d 100644 --- a/libpod/container_internal.go +++ b/libpod/container_internal.go @@ -753,9 +753,9 @@ func (c *Container) mountStorage() (err error) { mountPoint := c.config.Rootfs if mountPoint == "" { - mountPoint, err = c.runtime.storageService.MountContainerImage(c.ID()) + mountPoint, err = c.mount() if err != nil { - return errors.Wrapf(err, "error mounting storage for container %s", c.ID()) + return err } } c.state.Mounted = true @@ -796,8 +796,7 @@ func (c *Container) cleanupStorage() error { return nil } - // Also unmount storage - if _, err := c.runtime.storageService.UnmountContainerImage(c.ID()); err != nil { + if err := c.unmount(); err != nil { // If the container has already been removed, warn but don't // error // We still want to be able to kick the container out of the @@ -807,7 +806,7 @@ func (c *Container) cleanupStorage() error { return nil } - return errors.Wrapf(err, "error unmounting container %s root filesystem", c.ID()) + return err } c.state.Mountpoint = "" @@ -1285,3 +1284,22 @@ func (c *Container) setupOCIHooks(ctx context.Context, config *spec.Spec) (exten return manager.Hooks(config, c.Spec().Annotations, len(c.config.UserVolumes) > 0) } + +// mount mounts the container's root filesystem +func (c *Container) mount() (string, error) { + mountPoint, err := c.runtime.storageService.MountContainerImage(c.ID()) + if err != nil { + return "", errors.Wrapf(err, "error mounting storage for container %s", c.ID()) + } + return mountPoint, nil +} + +// unmount unmounts the container's root filesystem +func (c *Container) unmount() error { + // Also unmount storage + if _, err := c.runtime.storageService.UnmountContainerImage(c.ID()); err != nil { + return errors.Wrapf(err, "error unmounting container %s root filesystem", c.ID()) + } + + return nil +} diff --git a/libpod/image/image.go b/libpod/image/image.go index b5c4c537f..3863338b5 100644 --- a/libpod/image/image.go +++ b/libpod/image/image.go @@ -260,6 +260,12 @@ func (i *Image) getLocalImage() (*storage.Image, error) { if hasReg { return nil, errors.Errorf("%s", imageError) } + // if the image is saved with the repository localhost, searching with localhost prepended is necessary + // We don't need to strip the sha because we have already determined it is not an ID + img, err = i.imageruntime.getImage(DefaultLocalRepo + "/" + i.InputName) + if err == nil { + return img.image, err + } // grab all the local images images, err := i.imageruntime.GetImages() @@ -462,6 +468,10 @@ func (i *Image) TagImage(tag string) error { if !decomposedTag.isTagged { tag = fmt.Sprintf("%s:%s", tag, decomposedTag.tag) } + // If the input doesn't specify a registry, set the registry to localhost + if !decomposedTag.hasRegistry { + tag = fmt.Sprintf("%s/%s", DefaultLocalRepo, tag) + } tags := i.Names() if util.StringInSlice(tag, tags) { return nil diff --git a/libpod/image/image_test.go b/libpod/image/image_test.go index 71beed495..04877dbe5 100644 --- a/libpod/image/image_test.go +++ b/libpod/image/image_test.go @@ -170,12 +170,12 @@ func TestImage_MatchRepoTag(t *testing.T) { // foo should resolve to foo:latest repoTag, err := newImage.MatchRepoTag("foo") assert.NoError(t, err) - assert.Equal(t, "foo:latest", repoTag) + assert.Equal(t, "localhost/foo:latest", repoTag) // foo:bar should resolve to foo:bar repoTag, err = newImage.MatchRepoTag("foo:bar") assert.NoError(t, err) - assert.Equal(t, "foo:bar", repoTag) + assert.Equal(t, "localhost/foo:bar", repoTag) // Shutdown the runtime and remove the temporary storage cleanup(workdir, ir) } diff --git a/libpod/image/pull.go b/libpod/image/pull.go index a5a398eb1..3bfbc912c 100644 --- a/libpod/image/pull.go +++ b/libpod/image/pull.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "io" - "os" "strings" cp "github.com/containers/image/copy" @@ -46,6 +45,9 @@ var ( AtomicTransport = "atomic" // DefaultTransport is a prefix that we apply to an image name DefaultTransport = DockerTransport + // DefaultLocalRepo is the default local repository for local image operations + // Remote pulls will still use defined registries + DefaultLocalRepo = "localhost" ) type pullStruct struct { @@ -56,6 +58,14 @@ type pullStruct struct { } func (ir *Runtime) getPullStruct(srcRef types.ImageReference, destName string) (*pullStruct, error) { + imgPart, err := decompose(destName) + if err == nil && !imgPart.hasRegistry { + // If the image doesn't have a registry, set it as the default repo + imgPart.registry = DefaultLocalRepo + imgPart.hasRegistry = true + destName = imgPart.assemble() + } + reference := destName if srcRef.DockerReference() != nil { reference = srcRef.DockerReference().String() @@ -149,7 +159,9 @@ func (ir *Runtime) getPullListFromRef(ctx context.Context, srcRef types.ImageRef image := splitArr[1] // remove leading "/" if image[:1] == "/" { - image = image[1:] + // Instead of removing the leading /, set localhost as the registry + // so docker.io isn't prepended, and the path becomes the repository + image = DefaultLocalRepo + image } pullInfo, err := ir.getPullStruct(srcRef, image) if err != nil { @@ -277,12 +289,7 @@ func (i *Image) createNamesToPull() ([]*pullStruct, error) { pullNames = append(pullNames, &ps) } else { - registryConfigPath := "" - envOverride := os.Getenv("REGISTRIES_CONFIG_PATH") - if len(envOverride) > 0 { - registryConfigPath = envOverride - } - searchRegistries, err := sysregistries.GetRegistries(&types.SystemContext{SystemRegistriesConfPath: registryConfigPath}) + searchRegistries, err := registries.GetRegistries() if err != nil { return nil, err } diff --git a/libpod/storage.go b/libpod/storage.go index ff366edf2..76aa9efa4 100644 --- a/libpod/storage.go +++ b/libpod/storage.go @@ -248,6 +248,21 @@ func (r *storageService) UnmountContainerImage(idOrName string) (bool, error) { return mounted, nil } +func (r *storageService) MountedContainerImage(idOrName string) (int, error) { + if idOrName == "" { + return 0, ErrEmptyID + } + container, err := r.store.Container(idOrName) + if err != nil { + return 0, err + } + mounted, err := r.store.Mounted(container.ID) + if err != nil { + return 0, err + } + return mounted, nil +} + func (r *storageService) GetWorkDir(id string) (string, error) { container, err := r.store.Container(id) if err != nil { |