btrfs.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. // +build linux
  2. package btrfs
  3. /*
  4. #include <stdlib.h>
  5. #include <dirent.h>
  6. #include <btrfs/ioctl.h>
  7. #include <btrfs/ctree.h>
  8. */
  9. import "C"
  10. import (
  11. "fmt"
  12. "os"
  13. "path"
  14. "path/filepath"
  15. "syscall"
  16. "unsafe"
  17. "github.com/docker/docker/daemon/graphdriver"
  18. "github.com/docker/docker/pkg/idtools"
  19. "github.com/docker/docker/pkg/mount"
  20. "github.com/opencontainers/runc/libcontainer/label"
  21. )
  22. func init() {
  23. graphdriver.Register("btrfs", Init)
  24. }
  25. // Init returns a new BTRFS driver.
  26. // An error is returned if BTRFS is not supported.
  27. func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
  28. rootdir := path.Dir(home)
  29. var buf syscall.Statfs_t
  30. if err := syscall.Statfs(rootdir, &buf); err != nil {
  31. return nil, err
  32. }
  33. if graphdriver.FsMagic(buf.Type) != graphdriver.FsMagicBtrfs {
  34. return nil, graphdriver.ErrPrerequisites
  35. }
  36. rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps)
  37. if err != nil {
  38. return nil, err
  39. }
  40. if err := idtools.MkdirAllAs(home, 0700, rootUID, rootGID); err != nil {
  41. return nil, err
  42. }
  43. if err := mount.MakePrivate(home); err != nil {
  44. return nil, err
  45. }
  46. driver := &Driver{
  47. home: home,
  48. uidMaps: uidMaps,
  49. gidMaps: gidMaps,
  50. }
  51. return graphdriver.NewNaiveDiffDriver(driver, uidMaps, gidMaps), nil
  52. }
  53. // Driver contains information about the filesystem mounted.
  54. type Driver struct {
  55. //root of the file system
  56. home string
  57. uidMaps []idtools.IDMap
  58. gidMaps []idtools.IDMap
  59. }
  60. // String prints the name of the driver (btrfs).
  61. func (d *Driver) String() string {
  62. return "btrfs"
  63. }
  64. // Status returns current driver information in a two dimensional string array.
  65. // Output contains "Build Version" and "Library Version" of the btrfs libraries used.
  66. // Version information can be used to check compatibility with your kernel.
  67. func (d *Driver) Status() [][2]string {
  68. status := [][2]string{}
  69. if bv := btrfsBuildVersion(); bv != "-" {
  70. status = append(status, [2]string{"Build Version", bv})
  71. }
  72. if lv := btrfsLibVersion(); lv != -1 {
  73. status = append(status, [2]string{"Library Version", fmt.Sprintf("%d", lv)})
  74. }
  75. return status
  76. }
  77. // GetMetadata returns empty metadata for this driver.
  78. func (d *Driver) GetMetadata(id string) (map[string]string, error) {
  79. return nil, nil
  80. }
  81. // Cleanup unmounts the home directory.
  82. func (d *Driver) Cleanup() error {
  83. return mount.Unmount(d.home)
  84. }
  85. func free(p *C.char) {
  86. C.free(unsafe.Pointer(p))
  87. }
  88. func openDir(path string) (*C.DIR, error) {
  89. Cpath := C.CString(path)
  90. defer free(Cpath)
  91. dir := C.opendir(Cpath)
  92. if dir == nil {
  93. return nil, fmt.Errorf("Can't open dir")
  94. }
  95. return dir, nil
  96. }
  97. func closeDir(dir *C.DIR) {
  98. if dir != nil {
  99. C.closedir(dir)
  100. }
  101. }
  102. func getDirFd(dir *C.DIR) uintptr {
  103. return uintptr(C.dirfd(dir))
  104. }
  105. func subvolCreate(path, name string) error {
  106. dir, err := openDir(path)
  107. if err != nil {
  108. return err
  109. }
  110. defer closeDir(dir)
  111. var args C.struct_btrfs_ioctl_vol_args
  112. for i, c := range []byte(name) {
  113. args.name[i] = C.char(c)
  114. }
  115. _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_SUBVOL_CREATE,
  116. uintptr(unsafe.Pointer(&args)))
  117. if errno != 0 {
  118. return fmt.Errorf("Failed to create btrfs subvolume: %v", errno.Error())
  119. }
  120. return nil
  121. }
  122. func subvolSnapshot(src, dest, name string) error {
  123. srcDir, err := openDir(src)
  124. if err != nil {
  125. return err
  126. }
  127. defer closeDir(srcDir)
  128. destDir, err := openDir(dest)
  129. if err != nil {
  130. return err
  131. }
  132. defer closeDir(destDir)
  133. var args C.struct_btrfs_ioctl_vol_args_v2
  134. args.fd = C.__s64(getDirFd(srcDir))
  135. for i, c := range []byte(name) {
  136. args.name[i] = C.char(c)
  137. }
  138. _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, getDirFd(destDir), C.BTRFS_IOC_SNAP_CREATE_V2,
  139. uintptr(unsafe.Pointer(&args)))
  140. if errno != 0 {
  141. return fmt.Errorf("Failed to create btrfs snapshot: %v", errno.Error())
  142. }
  143. return nil
  144. }
  145. func isSubvolume(p string) (bool, error) {
  146. var bufStat syscall.Stat_t
  147. if err := syscall.Lstat(p, &bufStat); err != nil {
  148. return false, err
  149. }
  150. // return true if it is a btrfs subvolume
  151. return bufStat.Ino == C.BTRFS_FIRST_FREE_OBJECTID, nil
  152. }
  153. func subvolDelete(dirpath, name string) error {
  154. dir, err := openDir(dirpath)
  155. if err != nil {
  156. return err
  157. }
  158. defer closeDir(dir)
  159. fullPath := path.Join(dirpath, name)
  160. var args C.struct_btrfs_ioctl_vol_args
  161. // walk the btrfs subvolumes
  162. walkSubvolumes := func(p string, f os.FileInfo, err error) error {
  163. if err != nil {
  164. if os.IsNotExist(err) && p != fullPath {
  165. // missing most likely because the path was a subvolume that got removed in the previous iteration
  166. // since it's gone anyway, we don't care
  167. return nil
  168. }
  169. return fmt.Errorf("error walking subvolumes: %v", err)
  170. }
  171. // we want to check children only so skip itself
  172. // it will be removed after the filepath walk anyways
  173. if f.IsDir() && p != fullPath {
  174. sv, err := isSubvolume(p)
  175. if err != nil {
  176. return fmt.Errorf("Failed to test if %s is a btrfs subvolume: %v", p, err)
  177. }
  178. if sv {
  179. if err := subvolDelete(path.Dir(p), f.Name()); err != nil {
  180. return fmt.Errorf("Failed to destroy btrfs child subvolume (%s) of parent (%s): %v", p, dirpath, err)
  181. }
  182. }
  183. }
  184. return nil
  185. }
  186. if err := filepath.Walk(path.Join(dirpath, name), walkSubvolumes); err != nil {
  187. return fmt.Errorf("Recursively walking subvolumes for %s failed: %v", dirpath, err)
  188. }
  189. // all subvolumes have been removed
  190. // now remove the one originally passed in
  191. for i, c := range []byte(name) {
  192. args.name[i] = C.char(c)
  193. }
  194. _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_SNAP_DESTROY,
  195. uintptr(unsafe.Pointer(&args)))
  196. if errno != 0 {
  197. return fmt.Errorf("Failed to destroy btrfs snapshot %s for %s: %v", dirpath, name, errno.Error())
  198. }
  199. return nil
  200. }
  201. func (d *Driver) subvolumesDir() string {
  202. return path.Join(d.home, "subvolumes")
  203. }
  204. func (d *Driver) subvolumesDirID(id string) string {
  205. return path.Join(d.subvolumesDir(), id)
  206. }
  207. // Create the filesystem with given id.
  208. func (d *Driver) Create(id, parent, mountLabel string) error {
  209. subvolumes := path.Join(d.home, "subvolumes")
  210. rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
  211. if err != nil {
  212. return err
  213. }
  214. if err := idtools.MkdirAllAs(subvolumes, 0700, rootUID, rootGID); err != nil {
  215. return err
  216. }
  217. if parent == "" {
  218. if err := subvolCreate(subvolumes, id); err != nil {
  219. return err
  220. }
  221. } else {
  222. parentDir, err := d.Get(parent, "")
  223. if err != nil {
  224. return err
  225. }
  226. if err := subvolSnapshot(parentDir, subvolumes, id); err != nil {
  227. return err
  228. }
  229. }
  230. // if we have a remapped root (user namespaces enabled), change the created snapshot
  231. // dir ownership to match
  232. if rootUID != 0 || rootGID != 0 {
  233. if err := os.Chown(path.Join(subvolumes, id), rootUID, rootGID); err != nil {
  234. return err
  235. }
  236. }
  237. return label.Relabel(path.Join(subvolumes, id), mountLabel, false)
  238. }
  239. // Remove the filesystem with given id.
  240. func (d *Driver) Remove(id string) error {
  241. dir := d.subvolumesDirID(id)
  242. if _, err := os.Stat(dir); err != nil {
  243. return err
  244. }
  245. if err := subvolDelete(d.subvolumesDir(), id); err != nil {
  246. return err
  247. }
  248. if err := os.RemoveAll(dir); err != nil && !os.IsNotExist(err) {
  249. return err
  250. }
  251. return nil
  252. }
  253. // Get the requested filesystem id.
  254. func (d *Driver) Get(id, mountLabel string) (string, error) {
  255. dir := d.subvolumesDirID(id)
  256. st, err := os.Stat(dir)
  257. if err != nil {
  258. return "", err
  259. }
  260. if !st.IsDir() {
  261. return "", fmt.Errorf("%s: not a directory", dir)
  262. }
  263. return dir, nil
  264. }
  265. // Put is not implemented for BTRFS as there is no cleanup required for the id.
  266. func (d *Driver) Put(id string) error {
  267. // Get() creates no runtime resources (like e.g. mounts)
  268. // so this doesn't need to do anything.
  269. return nil
  270. }
  271. // Exists checks if the id exists in the filesystem.
  272. func (d *Driver) Exists(id string) bool {
  273. dir := d.subvolumesDirID(id)
  274. _, err := os.Stat(dir)
  275. return err == nil
  276. }