btrfs.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. // +build linux
  2. package btrfs // import "github.com/docker/docker/daemon/graphdriver/btrfs"
  3. /*
  4. #include <stdlib.h>
  5. #include <dirent.h>
  6. #include <btrfs/ioctl.h>
  7. #include <btrfs/ctree.h>
  8. static void set_name_btrfs_ioctl_vol_args_v2(struct btrfs_ioctl_vol_args_v2* btrfs_struct, const char* value) {
  9. snprintf(btrfs_struct->name, BTRFS_SUBVOL_NAME_MAX, "%s", value);
  10. }
  11. */
  12. import "C"
  13. import (
  14. "fmt"
  15. "io/ioutil"
  16. "math"
  17. "os"
  18. "path"
  19. "path/filepath"
  20. "strconv"
  21. "strings"
  22. "sync"
  23. "unsafe"
  24. "github.com/docker/docker/daemon/graphdriver"
  25. "github.com/docker/docker/pkg/containerfs"
  26. "github.com/docker/docker/pkg/idtools"
  27. "github.com/docker/docker/pkg/parsers"
  28. "github.com/docker/docker/pkg/system"
  29. "github.com/docker/go-units"
  30. "github.com/opencontainers/selinux/go-selinux/label"
  31. "github.com/sirupsen/logrus"
  32. "golang.org/x/sys/unix"
  33. )
  34. func init() {
  35. graphdriver.Register("btrfs", Init)
  36. }
  37. type btrfsOptions struct {
  38. minSpace uint64
  39. size uint64
  40. }
  41. // Init returns a new BTRFS driver.
  42. // An error is returned if BTRFS is not supported.
  43. func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
  44. // Perform feature detection on /var/lib/docker/btrfs if it's an existing directory.
  45. // This covers situations where /var/lib/docker/btrfs is a mount, and on a different
  46. // filesystem than /var/lib/docker.
  47. // If the path does not exist, fall back to using /var/lib/docker for feature detection.
  48. testdir := home
  49. if _, err := os.Stat(testdir); os.IsNotExist(err) {
  50. testdir = filepath.Dir(testdir)
  51. }
  52. fsMagic, err := graphdriver.GetFSMagic(testdir)
  53. if err != nil {
  54. return nil, err
  55. }
  56. if fsMagic != graphdriver.FsMagicBtrfs {
  57. return nil, graphdriver.ErrPrerequisites
  58. }
  59. rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps)
  60. if err != nil {
  61. return nil, err
  62. }
  63. if err := idtools.MkdirAllAndChown(home, 0700, idtools.IDPair{UID: rootUID, GID: rootGID}); err != nil {
  64. return nil, err
  65. }
  66. opt, userDiskQuota, err := parseOptions(options)
  67. if err != nil {
  68. return nil, err
  69. }
  70. driver := &Driver{
  71. home: home,
  72. uidMaps: uidMaps,
  73. gidMaps: gidMaps,
  74. options: opt,
  75. }
  76. if userDiskQuota {
  77. if err := driver.subvolEnableQuota(); err != nil {
  78. return nil, err
  79. }
  80. }
  81. return graphdriver.NewNaiveDiffDriver(driver, uidMaps, gidMaps), nil
  82. }
  83. func parseOptions(opt []string) (btrfsOptions, bool, error) {
  84. var options btrfsOptions
  85. userDiskQuota := false
  86. for _, option := range opt {
  87. key, val, err := parsers.ParseKeyValueOpt(option)
  88. if err != nil {
  89. return options, userDiskQuota, err
  90. }
  91. key = strings.ToLower(key)
  92. switch key {
  93. case "btrfs.min_space":
  94. minSpace, err := units.RAMInBytes(val)
  95. if err != nil {
  96. return options, userDiskQuota, err
  97. }
  98. userDiskQuota = true
  99. options.minSpace = uint64(minSpace)
  100. default:
  101. return options, userDiskQuota, fmt.Errorf("Unknown option %s", key)
  102. }
  103. }
  104. return options, userDiskQuota, nil
  105. }
  106. // Driver contains information about the filesystem mounted.
  107. type Driver struct {
  108. //root of the file system
  109. home string
  110. uidMaps []idtools.IDMap
  111. gidMaps []idtools.IDMap
  112. options btrfsOptions
  113. quotaEnabled bool
  114. once sync.Once
  115. }
  116. // String prints the name of the driver (btrfs).
  117. func (d *Driver) String() string {
  118. return "btrfs"
  119. }
  120. // Status returns current driver information in a two dimensional string array.
  121. // Output contains "Build Version" and "Library Version" of the btrfs libraries used.
  122. // Version information can be used to check compatibility with your kernel.
  123. func (d *Driver) Status() [][2]string {
  124. status := [][2]string{}
  125. if bv := btrfsBuildVersion(); bv != "-" {
  126. status = append(status, [2]string{"Build Version", bv})
  127. }
  128. if lv := btrfsLibVersion(); lv != -1 {
  129. status = append(status, [2]string{"Library Version", fmt.Sprintf("%d", lv)})
  130. }
  131. return status
  132. }
  133. // GetMetadata returns empty metadata for this driver.
  134. func (d *Driver) GetMetadata(id string) (map[string]string, error) {
  135. return nil, nil
  136. }
  137. // Cleanup unmounts the home directory.
  138. func (d *Driver) Cleanup() error {
  139. if err := d.subvolDisableQuota(); err != nil {
  140. return err
  141. }
  142. return nil
  143. }
  144. func free(p *C.char) {
  145. C.free(unsafe.Pointer(p))
  146. }
  147. func openDir(path string) (*C.DIR, error) {
  148. Cpath := C.CString(path)
  149. defer free(Cpath)
  150. dir := C.opendir(Cpath)
  151. if dir == nil {
  152. return nil, fmt.Errorf("Can't open dir")
  153. }
  154. return dir, nil
  155. }
  156. func closeDir(dir *C.DIR) {
  157. if dir != nil {
  158. C.closedir(dir)
  159. }
  160. }
  161. func getDirFd(dir *C.DIR) uintptr {
  162. return uintptr(C.dirfd(dir))
  163. }
  164. func subvolCreate(path, name string) error {
  165. dir, err := openDir(path)
  166. if err != nil {
  167. return err
  168. }
  169. defer closeDir(dir)
  170. var args C.struct_btrfs_ioctl_vol_args
  171. for i, c := range []byte(name) {
  172. args.name[i] = C.char(c)
  173. }
  174. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_SUBVOL_CREATE,
  175. uintptr(unsafe.Pointer(&args)))
  176. if errno != 0 {
  177. return fmt.Errorf("Failed to create btrfs subvolume: %v", errno.Error())
  178. }
  179. return nil
  180. }
  181. func subvolSnapshot(src, dest, name string) error {
  182. srcDir, err := openDir(src)
  183. if err != nil {
  184. return err
  185. }
  186. defer closeDir(srcDir)
  187. destDir, err := openDir(dest)
  188. if err != nil {
  189. return err
  190. }
  191. defer closeDir(destDir)
  192. var args C.struct_btrfs_ioctl_vol_args_v2
  193. args.fd = C.__s64(getDirFd(srcDir))
  194. var cs = C.CString(name)
  195. C.set_name_btrfs_ioctl_vol_args_v2(&args, cs)
  196. C.free(unsafe.Pointer(cs))
  197. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(destDir), C.BTRFS_IOC_SNAP_CREATE_V2,
  198. uintptr(unsafe.Pointer(&args)))
  199. if errno != 0 {
  200. return fmt.Errorf("Failed to create btrfs snapshot: %v", errno.Error())
  201. }
  202. return nil
  203. }
  204. func isSubvolume(p string) (bool, error) {
  205. var bufStat unix.Stat_t
  206. if err := unix.Lstat(p, &bufStat); err != nil {
  207. return false, err
  208. }
  209. // return true if it is a btrfs subvolume
  210. return bufStat.Ino == C.BTRFS_FIRST_FREE_OBJECTID, nil
  211. }
  212. func subvolDelete(dirpath, name string, quotaEnabled bool) error {
  213. dir, err := openDir(dirpath)
  214. if err != nil {
  215. return err
  216. }
  217. defer closeDir(dir)
  218. fullPath := path.Join(dirpath, name)
  219. var args C.struct_btrfs_ioctl_vol_args
  220. // walk the btrfs subvolumes
  221. walkSubvolumes := func(p string, f os.FileInfo, err error) error {
  222. if err != nil {
  223. if os.IsNotExist(err) && p != fullPath {
  224. // missing most likely because the path was a subvolume that got removed in the previous iteration
  225. // since it's gone anyway, we don't care
  226. return nil
  227. }
  228. return fmt.Errorf("error walking subvolumes: %v", err)
  229. }
  230. // we want to check children only so skip itself
  231. // it will be removed after the filepath walk anyways
  232. if f.IsDir() && p != fullPath {
  233. sv, err := isSubvolume(p)
  234. if err != nil {
  235. return fmt.Errorf("Failed to test if %s is a btrfs subvolume: %v", p, err)
  236. }
  237. if sv {
  238. if err := subvolDelete(path.Dir(p), f.Name(), quotaEnabled); err != nil {
  239. return fmt.Errorf("Failed to destroy btrfs child subvolume (%s) of parent (%s): %v", p, dirpath, err)
  240. }
  241. }
  242. }
  243. return nil
  244. }
  245. if err := filepath.Walk(path.Join(dirpath, name), walkSubvolumes); err != nil {
  246. return fmt.Errorf("Recursively walking subvolumes for %s failed: %v", dirpath, err)
  247. }
  248. if quotaEnabled {
  249. if qgroupid, err := subvolLookupQgroup(fullPath); err == nil {
  250. var args C.struct_btrfs_ioctl_qgroup_create_args
  251. args.qgroupid = C.__u64(qgroupid)
  252. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QGROUP_CREATE,
  253. uintptr(unsafe.Pointer(&args)))
  254. if errno != 0 {
  255. logrus.Errorf("Failed to delete btrfs qgroup %v for %s: %v", qgroupid, fullPath, errno.Error())
  256. }
  257. } else {
  258. logrus.Errorf("Failed to lookup btrfs qgroup for %s: %v", fullPath, err.Error())
  259. }
  260. }
  261. // all subvolumes have been removed
  262. // now remove the one originally passed in
  263. for i, c := range []byte(name) {
  264. args.name[i] = C.char(c)
  265. }
  266. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_SNAP_DESTROY,
  267. uintptr(unsafe.Pointer(&args)))
  268. if errno != 0 {
  269. return fmt.Errorf("Failed to destroy btrfs snapshot %s for %s: %v", dirpath, name, errno.Error())
  270. }
  271. return nil
  272. }
  273. func (d *Driver) updateQuotaStatus() {
  274. d.once.Do(func() {
  275. if !d.quotaEnabled {
  276. // In case quotaEnabled is not set, check qgroup and update quotaEnabled as needed
  277. if err := subvolQgroupStatus(d.home); err != nil {
  278. // quota is still not enabled
  279. return
  280. }
  281. d.quotaEnabled = true
  282. }
  283. })
  284. }
  285. func (d *Driver) subvolEnableQuota() error {
  286. d.updateQuotaStatus()
  287. if d.quotaEnabled {
  288. return nil
  289. }
  290. dir, err := openDir(d.home)
  291. if err != nil {
  292. return err
  293. }
  294. defer closeDir(dir)
  295. var args C.struct_btrfs_ioctl_quota_ctl_args
  296. args.cmd = C.BTRFS_QUOTA_CTL_ENABLE
  297. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QUOTA_CTL,
  298. uintptr(unsafe.Pointer(&args)))
  299. if errno != 0 {
  300. return fmt.Errorf("Failed to enable btrfs quota for %s: %v", dir, errno.Error())
  301. }
  302. d.quotaEnabled = true
  303. return nil
  304. }
  305. func (d *Driver) subvolDisableQuota() error {
  306. d.updateQuotaStatus()
  307. if !d.quotaEnabled {
  308. return nil
  309. }
  310. dir, err := openDir(d.home)
  311. if err != nil {
  312. return err
  313. }
  314. defer closeDir(dir)
  315. var args C.struct_btrfs_ioctl_quota_ctl_args
  316. args.cmd = C.BTRFS_QUOTA_CTL_DISABLE
  317. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QUOTA_CTL,
  318. uintptr(unsafe.Pointer(&args)))
  319. if errno != 0 {
  320. return fmt.Errorf("Failed to disable btrfs quota for %s: %v", dir, errno.Error())
  321. }
  322. d.quotaEnabled = false
  323. return nil
  324. }
  325. func (d *Driver) subvolRescanQuota() error {
  326. d.updateQuotaStatus()
  327. if !d.quotaEnabled {
  328. return nil
  329. }
  330. dir, err := openDir(d.home)
  331. if err != nil {
  332. return err
  333. }
  334. defer closeDir(dir)
  335. var args C.struct_btrfs_ioctl_quota_rescan_args
  336. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QUOTA_RESCAN_WAIT,
  337. uintptr(unsafe.Pointer(&args)))
  338. if errno != 0 {
  339. return fmt.Errorf("Failed to rescan btrfs quota for %s: %v", dir, errno.Error())
  340. }
  341. return nil
  342. }
  343. func subvolLimitQgroup(path string, size uint64) error {
  344. dir, err := openDir(path)
  345. if err != nil {
  346. return err
  347. }
  348. defer closeDir(dir)
  349. var args C.struct_btrfs_ioctl_qgroup_limit_args
  350. args.lim.max_referenced = C.__u64(size)
  351. args.lim.flags = C.BTRFS_QGROUP_LIMIT_MAX_RFER
  352. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QGROUP_LIMIT,
  353. uintptr(unsafe.Pointer(&args)))
  354. if errno != 0 {
  355. return fmt.Errorf("Failed to limit qgroup for %s: %v", dir, errno.Error())
  356. }
  357. return nil
  358. }
  359. // subvolQgroupStatus performs a BTRFS_IOC_TREE_SEARCH on the root path
  360. // with search key of BTRFS_QGROUP_STATUS_KEY.
  361. // In case qgroup is enabled, the retuned key type will match BTRFS_QGROUP_STATUS_KEY.
  362. // For more details please see https://github.com/kdave/btrfs-progs/blob/v4.9/qgroup.c#L1035
  363. func subvolQgroupStatus(path string) error {
  364. dir, err := openDir(path)
  365. if err != nil {
  366. return err
  367. }
  368. defer closeDir(dir)
  369. var args C.struct_btrfs_ioctl_search_args
  370. args.key.tree_id = C.BTRFS_QUOTA_TREE_OBJECTID
  371. args.key.min_type = C.BTRFS_QGROUP_STATUS_KEY
  372. args.key.max_type = C.BTRFS_QGROUP_STATUS_KEY
  373. args.key.max_objectid = C.__u64(math.MaxUint64)
  374. args.key.max_offset = C.__u64(math.MaxUint64)
  375. args.key.max_transid = C.__u64(math.MaxUint64)
  376. args.key.nr_items = 4096
  377. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_TREE_SEARCH,
  378. uintptr(unsafe.Pointer(&args)))
  379. if errno != 0 {
  380. return fmt.Errorf("Failed to search qgroup for %s: %v", path, errno.Error())
  381. }
  382. sh := (*C.struct_btrfs_ioctl_search_header)(unsafe.Pointer(&args.buf))
  383. if sh._type != C.BTRFS_QGROUP_STATUS_KEY {
  384. return fmt.Errorf("Invalid qgroup search header type for %s: %v", path, sh._type)
  385. }
  386. return nil
  387. }
  388. func subvolLookupQgroup(path string) (uint64, error) {
  389. dir, err := openDir(path)
  390. if err != nil {
  391. return 0, err
  392. }
  393. defer closeDir(dir)
  394. var args C.struct_btrfs_ioctl_ino_lookup_args
  395. args.objectid = C.BTRFS_FIRST_FREE_OBJECTID
  396. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_INO_LOOKUP,
  397. uintptr(unsafe.Pointer(&args)))
  398. if errno != 0 {
  399. return 0, fmt.Errorf("Failed to lookup qgroup for %s: %v", dir, errno.Error())
  400. }
  401. if args.treeid == 0 {
  402. return 0, fmt.Errorf("Invalid qgroup id for %s: 0", dir)
  403. }
  404. return uint64(args.treeid), nil
  405. }
  406. func (d *Driver) subvolumesDir() string {
  407. return path.Join(d.home, "subvolumes")
  408. }
  409. func (d *Driver) subvolumesDirID(id string) string {
  410. return path.Join(d.subvolumesDir(), id)
  411. }
  412. func (d *Driver) quotasDir() string {
  413. return path.Join(d.home, "quotas")
  414. }
  415. func (d *Driver) quotasDirID(id string) string {
  416. return path.Join(d.quotasDir(), id)
  417. }
  418. // CreateReadWrite creates a layer that is writable for use as a container
  419. // file system.
  420. func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
  421. return d.Create(id, parent, opts)
  422. }
  423. // Create the filesystem with given id.
  424. func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
  425. quotas := path.Join(d.home, "quotas")
  426. subvolumes := path.Join(d.home, "subvolumes")
  427. rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
  428. if err != nil {
  429. return err
  430. }
  431. if err := idtools.MkdirAllAndChown(subvolumes, 0700, idtools.IDPair{UID: rootUID, GID: rootGID}); err != nil {
  432. return err
  433. }
  434. if parent == "" {
  435. if err := subvolCreate(subvolumes, id); err != nil {
  436. return err
  437. }
  438. } else {
  439. parentDir := d.subvolumesDirID(parent)
  440. st, err := os.Stat(parentDir)
  441. if err != nil {
  442. return err
  443. }
  444. if !st.IsDir() {
  445. return fmt.Errorf("%s: not a directory", parentDir)
  446. }
  447. if err := subvolSnapshot(parentDir, subvolumes, id); err != nil {
  448. return err
  449. }
  450. }
  451. var storageOpt map[string]string
  452. if opts != nil {
  453. storageOpt = opts.StorageOpt
  454. }
  455. if _, ok := storageOpt["size"]; ok {
  456. driver := &Driver{}
  457. if err := d.parseStorageOpt(storageOpt, driver); err != nil {
  458. return err
  459. }
  460. if err := d.setStorageSize(path.Join(subvolumes, id), driver); err != nil {
  461. return err
  462. }
  463. if err := idtools.MkdirAllAndChown(quotas, 0700, idtools.IDPair{UID: rootUID, GID: rootGID}); err != nil {
  464. return err
  465. }
  466. if err := ioutil.WriteFile(path.Join(quotas, id), []byte(fmt.Sprint(driver.options.size)), 0644); err != nil {
  467. return err
  468. }
  469. }
  470. // if we have a remapped root (user namespaces enabled), change the created snapshot
  471. // dir ownership to match
  472. if rootUID != 0 || rootGID != 0 {
  473. if err := os.Chown(path.Join(subvolumes, id), rootUID, rootGID); err != nil {
  474. return err
  475. }
  476. }
  477. mountLabel := ""
  478. if opts != nil {
  479. mountLabel = opts.MountLabel
  480. }
  481. return label.Relabel(path.Join(subvolumes, id), mountLabel, false)
  482. }
  483. // Parse btrfs storage options
  484. func (d *Driver) parseStorageOpt(storageOpt map[string]string, driver *Driver) error {
  485. // Read size to change the subvolume disk quota per container
  486. for key, val := range storageOpt {
  487. key := strings.ToLower(key)
  488. switch key {
  489. case "size":
  490. size, err := units.RAMInBytes(val)
  491. if err != nil {
  492. return err
  493. }
  494. driver.options.size = uint64(size)
  495. default:
  496. return fmt.Errorf("Unknown option %s", key)
  497. }
  498. }
  499. return nil
  500. }
  501. // Set btrfs storage size
  502. func (d *Driver) setStorageSize(dir string, driver *Driver) error {
  503. if driver.options.size <= 0 {
  504. return fmt.Errorf("btrfs: invalid storage size: %s", units.HumanSize(float64(driver.options.size)))
  505. }
  506. if d.options.minSpace > 0 && driver.options.size < d.options.minSpace {
  507. return fmt.Errorf("btrfs: storage size cannot be less than %s", units.HumanSize(float64(d.options.minSpace)))
  508. }
  509. if err := d.subvolEnableQuota(); err != nil {
  510. return err
  511. }
  512. return subvolLimitQgroup(dir, driver.options.size)
  513. }
  514. // Remove the filesystem with given id.
  515. func (d *Driver) Remove(id string) error {
  516. dir := d.subvolumesDirID(id)
  517. if _, err := os.Stat(dir); err != nil {
  518. return err
  519. }
  520. quotasDir := d.quotasDirID(id)
  521. if _, err := os.Stat(quotasDir); err == nil {
  522. if err := os.Remove(quotasDir); err != nil {
  523. return err
  524. }
  525. } else if !os.IsNotExist(err) {
  526. return err
  527. }
  528. // Call updateQuotaStatus() to invoke status update
  529. d.updateQuotaStatus()
  530. if err := subvolDelete(d.subvolumesDir(), id, d.quotaEnabled); err != nil {
  531. return err
  532. }
  533. if err := system.EnsureRemoveAll(dir); err != nil {
  534. return err
  535. }
  536. return d.subvolRescanQuota()
  537. }
  538. // Get the requested filesystem id.
  539. func (d *Driver) Get(id, mountLabel string) (containerfs.ContainerFS, error) {
  540. dir := d.subvolumesDirID(id)
  541. st, err := os.Stat(dir)
  542. if err != nil {
  543. return nil, err
  544. }
  545. if !st.IsDir() {
  546. return nil, fmt.Errorf("%s: not a directory", dir)
  547. }
  548. if quota, err := ioutil.ReadFile(d.quotasDirID(id)); err == nil {
  549. if size, err := strconv.ParseUint(string(quota), 10, 64); err == nil && size >= d.options.minSpace {
  550. if err := d.subvolEnableQuota(); err != nil {
  551. return nil, err
  552. }
  553. if err := subvolLimitQgroup(dir, size); err != nil {
  554. return nil, err
  555. }
  556. }
  557. }
  558. return containerfs.NewLocalContainerFS(dir), nil
  559. }
  560. // Put is not implemented for BTRFS as there is no cleanup required for the id.
  561. func (d *Driver) Put(id string) error {
  562. // Get() creates no runtime resources (like e.g. mounts)
  563. // so this doesn't need to do anything.
  564. return nil
  565. }
  566. // Exists checks if the id exists in the filesystem.
  567. func (d *Driver) Exists(id string) bool {
  568. dir := d.subvolumesDirID(id)
  569. _, err := os.Stat(dir)
  570. return err == nil
  571. }