File: mount.go

package info (click to toggle)
golang-github-containers-buildah 1.19.6%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,020 kB
  • sloc: sh: 1,957; makefile: 199; perl: 173; awk: 12; ansic: 1
file content (53 lines) | stat: -rw-r--r-- 1,693 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package buildah

import (
	"github.com/pkg/errors"
)

// Mount mounts a container's root filesystem in a location which can be
// accessed from the host, and returns the location.
func (b *Builder) Mount(label string) (string, error) {
	mountpoint, err := b.store.Mount(b.ContainerID, label)
	if err != nil {
		return "", errors.Wrapf(err, "error mounting build container %q", b.ContainerID)
	}
	b.MountPoint = mountpoint

	err = b.Save()
	if err != nil {
		return "", errors.Wrapf(err, "error saving updated state for build container %q", b.ContainerID)
	}
	return mountpoint, nil
}

func (b *Builder) setMountPoint(mountPoint string) error {
	b.MountPoint = mountPoint
	if err := b.Save(); err != nil {
		return errors.Wrapf(err, "error saving updated state for build container %q", b.ContainerID)
	}
	return nil
}

// Mounted returns whether the container is mounted or not
func (b *Builder) Mounted() (bool, error) {
	mountCnt, err := b.store.Mounted(b.ContainerID)
	if err != nil {
		return false, errors.Wrapf(err, "error determining if mounting build container %q is mounted", b.ContainerID)
	}
	mounted := mountCnt > 0
	if mounted && b.MountPoint == "" {
		ctr, err := b.store.Container(b.ContainerID)
		if err != nil {
			return mountCnt > 0, errors.Wrapf(err, "error determining if mounting build container %q is mounted", b.ContainerID)
		}
		layer, err := b.store.Layer(ctr.LayerID)
		if err != nil {
			return mountCnt > 0, errors.Wrapf(err, "error determining if mounting build container %q is mounted", b.ContainerID)
		}
		return mounted, b.setMountPoint(layer.MountPoint)
	}
	if !mounted && b.MountPoint != "" {
		return mounted, b.setMountPoint("")
	}
	return mounted, nil
}