lxd/container_lxc.go
func (c *containerLXC) insertMount(source, target, fstype string, flags int) error {
	var err error

	// Get the init PID
	pid := c.InitPID()
	if pid == -1 {
		// Container isn't running
		return fmt.Errorf("Can't insert mount into stopped container")
	}

	if lxc.HasApiExtension("mount_injection_file") {
		cname := projectPrefix(c.Project(), c.Name())
		configPath := filepath.Join(c.LogPath(), "lxc.conf")
		if fstype == "" {
			fstype = "none"
		}

		if !strings.HasPrefix(target, "/") {
			target = "/" + target
		}

		_, err := shared.RunCommand(c.state.OS.ExecPath, "forkmount", "lxc-mount", cname, c.state.OS.LxcPath, configPath, source, target, fstype, fmt.Sprintf("%d", flags))
		if err != nil {
			return err
		}
	} else {
		// Create the temporary mount target
		var tmpMount string
		if shared.IsDir(source) {
			tmpMount, err = ioutil.TempDir(c.ShmountsPath(), "lxdmount_")
			if err != nil {
				return fmt.Errorf("Failed to create shmounts path: %s", err)
			}
		} else {
			f, err := ioutil.TempFile(c.ShmountsPath(), "lxdmount_")
			if err != nil {
				return fmt.Errorf("Failed to create shmounts path: %s", err)
			}

			tmpMount = f.Name()
			f.Close()
		}
		defer os.Remove(tmpMount)

		// Mount the filesystem
		err = syscall.Mount(source, tmpMount, fstype, uintptr(flags), "")
		if err != nil {
			return fmt.Errorf("Failed to setup temporary mount: %s", err)
		}
		defer syscall.Unmount(tmpMount, syscall.MNT_DETACH)

		// Move the mount inside the container
		mntsrc := filepath.Join("/dev/.lxd-mounts", filepath.Base(tmpMount))
		pidStr := fmt.Sprintf("%d", pid)

		out, err := shared.RunCommand(c.state.OS.ExecPath, "forkmount", "lxd-mount", pidStr, mntsrc, target)

		if out != "" {
			for _, line := range strings.Split(strings.TrimRight(out, "\n"), "\n") {
				logger.Debugf("forkmount: %s", line)
			}
		}
		if err != nil {
			return err
		}
	}

	return nil
}
func (c *containerLXC) insertMount(source, target, fstype string, flags int) error {
	var err error

	// Get the init PID
	pid := c.InitPID()
	if pid == -1 {
		// Container isn't running
		return fmt.Errorf("Can't insert mount into stopped container")
	}

	if lxc.HasApiExtension("mount_injection_file") {
		cname := projectPrefix(c.Project(), c.Name())
		configPath := filepath.Join(c.LogPath(), "lxc.conf")
		if fstype == "" {
			fstype = "none"
		}

		if !strings.HasPrefix(target, "/") {
			target = "/" + target
		}

		_, err := shared.RunCommand(c.state.OS.ExecPath, "forkmount", "lxc-mount", cname, c.state.OS.LxcPath, configPath, source, target, fstype, fmt.Sprintf("%d", flags))
		if err != nil {
			return err
		}
	} else {
		// Create the temporary mount target
		var tmpMount string
		if shared.IsDir(source) {
			tmpMount, err = ioutil.TempDir(c.ShmountsPath(), "lxdmount_")
			if err != nil {
				return fmt.Errorf("Failed to create shmounts path: %s", err)
			}
		} else {
			f, err := ioutil.TempFile(c.ShmountsPath(), "lxdmount_")
			if err != nil {
				return fmt.Errorf("Failed to create shmounts path: %s", err)
			}

			tmpMount = f.Name()
			f.Close()
		}
		defer os.Remove(tmpMount)

		// Mount the filesystem
		err = syscall.Mount(source, tmpMount, fstype, uintptr(flags), "")
		if err != nil {
			return fmt.Errorf("Failed to setup temporary mount: %s", err)
		}
		defer syscall.Unmount(tmpMount, syscall.MNT_DETACH)

		// Move the mount inside the container
		mntsrc := filepath.Join("/dev/.lxd-mounts", filepath.Base(tmpMount))
		pidStr := fmt.Sprintf("%d", pid)

		out, err := shared.RunCommand(c.state.OS.ExecPath, "forkmount", "lxd-mount", pidStr, mntsrc, target)

		if out != "" {
			for _, line := range strings.Split(strings.TrimRight(out, "\n"), "\n") {
				logger.Debugf("forkmount: %s", line)
			}
		}
		if err != nil {
			return err
		}
	}

	return nil
}
// Mount handling