filesystem.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package docker
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. )
  7. type Filesystem struct {
  8. RootFS string
  9. RWPath string
  10. Layers []string
  11. }
  12. func (fs *Filesystem) createMountPoints() error {
  13. if err := os.Mkdir(fs.RootFS, 0700); err != nil && !os.IsExist(err) {
  14. return err
  15. }
  16. if err := os.Mkdir(fs.RWPath, 0700); err != nil && !os.IsExist(err) {
  17. return err
  18. }
  19. return nil
  20. }
  21. func (fs *Filesystem) Mount() error {
  22. if err := fs.createMountPoints(); err != nil {
  23. return err
  24. }
  25. rwBranch := fmt.Sprintf("%v=rw", fs.RWPath)
  26. roBranches := ""
  27. for _, layer := range fs.Layers {
  28. roBranches += fmt.Sprintf("%v=ro:", layer)
  29. }
  30. branches := fmt.Sprintf("br:%v:%v", rwBranch, roBranches)
  31. cmd := exec.Command("mount", "-t", "aufs", "-o", branches, "none", fs.RootFS)
  32. if err := cmd.Run(); err != nil {
  33. return err
  34. }
  35. return nil
  36. }
  37. func (fs *Filesystem) Umount() error {
  38. return exec.Command("umount", fs.RootFS).Run()
  39. }
  40. func newFilesystem(rootfs string, rwpath string, layers []string) *Filesystem {
  41. return &Filesystem{
  42. RootFS: rootfs,
  43. RWPath: rwpath,
  44. Layers: layers,
  45. }
  46. }