btrfs.go 7.8 KB

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