btrfs.go 17 KB

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