tmpfs.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package native
  2. import (
  3. "fmt"
  4. "os"
  5. "os/exec"
  6. "strings"
  7. "github.com/Sirupsen/logrus"
  8. "github.com/opencontainers/runc/libcontainer/configs"
  9. )
  10. func genTmpfsPremountCmd(tmpDir string, fullDest string, dest string) []configs.Command {
  11. var premount []configs.Command
  12. tarPath, err := exec.LookPath("tar")
  13. if err != nil {
  14. logrus.Warn("tar command is not available for tmpfs mount: %s", err)
  15. return premount
  16. }
  17. if _, err = exec.LookPath("rm"); err != nil {
  18. logrus.Warn("rm command is not available for tmpfs mount: %s", err)
  19. return premount
  20. }
  21. tarFile := fmt.Sprintf("%s/%s.tar", tmpDir, strings.Replace(dest, "/", "_", -1))
  22. if _, err := os.Stat(fullDest); err == nil {
  23. premount = append(premount, configs.Command{
  24. Path: tarPath,
  25. Args: []string{"-cf", tarFile, "-C", fullDest, "."},
  26. })
  27. }
  28. return premount
  29. }
  30. func genTmpfsPostmountCmd(tmpDir string, fullDest string, dest string) []configs.Command {
  31. var postmount []configs.Command
  32. tarPath, err := exec.LookPath("tar")
  33. if err != nil {
  34. return postmount
  35. }
  36. rmPath, err := exec.LookPath("rm")
  37. if err != nil {
  38. return postmount
  39. }
  40. if _, err := os.Stat(fullDest); os.IsNotExist(err) {
  41. return postmount
  42. }
  43. tarFile := fmt.Sprintf("%s/%s.tar", tmpDir, strings.Replace(dest, "/", "_", -1))
  44. postmount = append(postmount, configs.Command{
  45. Path: tarPath,
  46. Args: []string{"-xf", tarFile, "-C", fullDest, "."},
  47. })
  48. return append(postmount, configs.Command{
  49. Path: rmPath,
  50. Args: []string{"-f", tarFile},
  51. })
  52. }