btrfs.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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/mount"
  19. )
  20. func init() {
  21. graphdriver.Register("btrfs", Init)
  22. }
  23. func is_subvolume(dirpath string) (bool, error) {
  24. rootdir := path.Dir(dirpath)
  25. var bufStat syscall.Stat_t
  26. if err := syscall.Lstat(rootdir, &bufStat); err != nil {
  27. return false, err
  28. }
  29. if bufStat.Ino != C.BTRFS_FIRST_FREE_OBJECTID {
  30. return false, nil
  31. }
  32. return true, nil
  33. }
  34. // Init returns a new BTRFS driver.
  35. // An error is returned if BTRFS is not supported.
  36. func Init(home string, options []string) (graphdriver.Driver, error) {
  37. rootdir := path.Dir(home)
  38. var buf syscall.Statfs_t
  39. if err := syscall.Statfs(rootdir, &buf); err != nil {
  40. return nil, err
  41. }
  42. if graphdriver.FsMagic(buf.Type) != graphdriver.FsMagicBtrfs {
  43. return nil, graphdriver.ErrPrerequisites
  44. }
  45. if err := os.MkdirAll(home, 0700); err != nil {
  46. return nil, err
  47. }
  48. if err := mount.MakePrivate(home); err != nil {
  49. return nil, err
  50. }
  51. driver := &Driver{
  52. home: home,
  53. }
  54. return graphdriver.NaiveDiffDriver(driver), nil
  55. }
  56. // Driver contains information about the filesystem mounted.
  57. type Driver struct {
  58. //root of the file system
  59. home string
  60. }
  61. // String prints the name of the driver (btrfs).
  62. func (d *Driver) String() string {
  63. return "btrfs"
  64. }
  65. // Status returns current driver information in a two dimensional string array.
  66. // Output contains "Build Version" and "Library Version" of the btrfs libraries used.
  67. // Version information can be used to check compatibility with your kernel.
  68. func (d *Driver) Status() [][2]string {
  69. status := [][2]string{}
  70. if bv := btrfsBuildVersion(); bv != "-" {
  71. status = append(status, [2]string{"Build Version", bv})
  72. }
  73. if lv := btrfsLibVersion(); lv != -1 {
  74. status = append(status, [2]string{"Library Version", fmt.Sprintf("%d", lv)})
  75. }
  76. return status
  77. }
  78. // GetMetadata returns empty metadata for this driver.
  79. func (d *Driver) GetMetadata(id string) (map[string]string, error) {
  80. return nil, nil
  81. }
  82. // Cleanup unmounts the home directory.
  83. func (d *Driver) Cleanup() error {
  84. return mount.Unmount(d.home)
  85. }
  86. func free(p *C.char) {
  87. C.free(unsafe.Pointer(p))
  88. }
  89. func openDir(path string) (*C.DIR, error) {
  90. Cpath := C.CString(path)
  91. defer free(Cpath)
  92. dir := C.opendir(Cpath)
  93. if dir == nil {
  94. return nil, fmt.Errorf("Can't open dir")
  95. }
  96. return dir, nil
  97. }
  98. func closeDir(dir *C.DIR) {
  99. if dir != nil {
  100. C.closedir(dir)
  101. }
  102. }
  103. func getDirFd(dir *C.DIR) uintptr {
  104. return uintptr(C.dirfd(dir))
  105. }
  106. func subvolCreate(path, name string) error {
  107. dir, err := openDir(path)
  108. if err != nil {
  109. return err
  110. }
  111. defer closeDir(dir)
  112. var args C.struct_btrfs_ioctl_vol_args
  113. for i, c := range []byte(name) {
  114. args.name[i] = C.char(c)
  115. }
  116. _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_SUBVOL_CREATE,
  117. uintptr(unsafe.Pointer(&args)))
  118. if errno != 0 {
  119. return fmt.Errorf("Failed to create btrfs subvolume: %v", errno.Error())
  120. }
  121. return nil
  122. }
  123. func subvolSnapshot(src, dest, name string) error {
  124. srcDir, err := openDir(src)
  125. if err != nil {
  126. return err
  127. }
  128. defer closeDir(srcDir)
  129. destDir, err := openDir(dest)
  130. if err != nil {
  131. return err
  132. }
  133. defer closeDir(destDir)
  134. var args C.struct_btrfs_ioctl_vol_args_v2
  135. args.fd = C.__s64(getDirFd(srcDir))
  136. for i, c := range []byte(name) {
  137. args.name[i] = C.char(c)
  138. }
  139. _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, getDirFd(destDir), C.BTRFS_IOC_SNAP_CREATE_V2,
  140. uintptr(unsafe.Pointer(&args)))
  141. if errno != 0 {
  142. return fmt.Errorf("Failed to create btrfs snapshot: %v", errno.Error())
  143. }
  144. return nil
  145. }
  146. func subvolDelete(dirpath, name string) error {
  147. dir, err := openDir(dirpath)
  148. if err != nil {
  149. return err
  150. }
  151. defer closeDir(dir)
  152. var args C.struct_btrfs_ioctl_vol_args
  153. filepath.Walk(dirpath,
  154. func(dirpath string, f os.FileInfo, err error) error {
  155. if f.IsDir() {
  156. isSubvolumes, err := is_subvolume(path.Join(dirpath, f.Name()))
  157. if err != nil {
  158. return err
  159. }
  160. if isSubvolumes {
  161. for i, c := range []byte(f.Name()) {
  162. args.name[i] = C.char(c)
  163. }
  164. _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_SNAP_DESTROY,
  165. uintptr(unsafe.Pointer(&args)))
  166. if errno != 0 {
  167. return fmt.Errorf("Failed to destroy btrfs snapshot: %v", errno.Error())
  168. }
  169. }
  170. return nil
  171. }
  172. return nil
  173. })
  174. for i, c := range []byte(name) {
  175. args.name[i] = C.char(c)
  176. }
  177. _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_SNAP_DESTROY,
  178. uintptr(unsafe.Pointer(&args)))
  179. if errno != 0 {
  180. return fmt.Errorf("Failed to destroy btrfs snapshot: %v", errno.Error())
  181. }
  182. return nil
  183. }
  184. func (d *Driver) subvolumesDir() string {
  185. return path.Join(d.home, "subvolumes")
  186. }
  187. func (d *Driver) subvolumesDirID(id string) string {
  188. return path.Join(d.subvolumesDir(), id)
  189. }
  190. // Create the filesystem with given id.
  191. func (d *Driver) Create(id string, parent string) error {
  192. subvolumes := path.Join(d.home, "subvolumes")
  193. if err := os.MkdirAll(subvolumes, 0700); err != nil {
  194. return err
  195. }
  196. if parent == "" {
  197. if err := subvolCreate(subvolumes, id); err != nil {
  198. return err
  199. }
  200. } else {
  201. parentDir, err := d.Get(parent, "")
  202. if err != nil {
  203. return err
  204. }
  205. if err := subvolSnapshot(parentDir, subvolumes, id); err != nil {
  206. return err
  207. }
  208. }
  209. return nil
  210. }
  211. // Remove the filesystem with given id.
  212. func (d *Driver) Remove(id string) error {
  213. dir := d.subvolumesDirID(id)
  214. if _, err := os.Stat(dir); err != nil {
  215. return err
  216. }
  217. if err := subvolDelete(d.subvolumesDir(), id); err != nil {
  218. return err
  219. }
  220. return os.RemoveAll(dir)
  221. }
  222. // Get the requested filesystem id.
  223. func (d *Driver) Get(id, mountLabel string) (string, error) {
  224. dir := d.subvolumesDirID(id)
  225. st, err := os.Stat(dir)
  226. if err != nil {
  227. return "", err
  228. }
  229. if !st.IsDir() {
  230. return "", fmt.Errorf("%s: not a directory", dir)
  231. }
  232. return dir, nil
  233. }
  234. // Put is not implemented for BTRFS as there is no cleanup required for the id.
  235. func (d *Driver) Put(id string) error {
  236. // Get() creates no runtime resources (like e.g. mounts)
  237. // so this doesn't need to do anything.
  238. return nil
  239. }
  240. // Exists checks if the id exists in the filesystem.
  241. func (d *Driver) Exists(id string) bool {
  242. dir := d.subvolumesDirID(id)
  243. _, err := os.Stat(dir)
  244. return err == nil
  245. }