btrfs.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. return d.subvolDisableQuota()
  140. }
  141. func free(p *C.char) {
  142. C.free(unsafe.Pointer(p))
  143. }
  144. func openDir(path string) (*C.DIR, error) {
  145. Cpath := C.CString(path)
  146. defer free(Cpath)
  147. dir := C.opendir(Cpath)
  148. if dir == nil {
  149. return nil, fmt.Errorf("Can't open dir")
  150. }
  151. return dir, nil
  152. }
  153. func closeDir(dir *C.DIR) {
  154. if dir != nil {
  155. C.closedir(dir)
  156. }
  157. }
  158. func getDirFd(dir *C.DIR) uintptr {
  159. return uintptr(C.dirfd(dir))
  160. }
  161. func subvolCreate(path, name string) error {
  162. dir, err := openDir(path)
  163. if err != nil {
  164. return err
  165. }
  166. defer closeDir(dir)
  167. var args C.struct_btrfs_ioctl_vol_args
  168. for i, c := range []byte(name) {
  169. args.name[i] = C.char(c)
  170. }
  171. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_SUBVOL_CREATE,
  172. uintptr(unsafe.Pointer(&args)))
  173. if errno != 0 {
  174. return fmt.Errorf("Failed to create btrfs subvolume: %v", errno.Error())
  175. }
  176. return nil
  177. }
  178. func subvolSnapshot(src, dest, name string) error {
  179. srcDir, err := openDir(src)
  180. if err != nil {
  181. return err
  182. }
  183. defer closeDir(srcDir)
  184. destDir, err := openDir(dest)
  185. if err != nil {
  186. return err
  187. }
  188. defer closeDir(destDir)
  189. var args C.struct_btrfs_ioctl_vol_args_v2
  190. args.fd = C.__s64(getDirFd(srcDir))
  191. var cs = C.CString(name)
  192. C.set_name_btrfs_ioctl_vol_args_v2(&args, cs)
  193. C.free(unsafe.Pointer(cs))
  194. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(destDir), C.BTRFS_IOC_SNAP_CREATE_V2,
  195. uintptr(unsafe.Pointer(&args)))
  196. if errno != 0 {
  197. return fmt.Errorf("Failed to create btrfs snapshot: %v", errno.Error())
  198. }
  199. return nil
  200. }
  201. func isSubvolume(p string) (bool, error) {
  202. var bufStat unix.Stat_t
  203. if err := unix.Lstat(p, &bufStat); err != nil {
  204. return false, err
  205. }
  206. // return true if it is a btrfs subvolume
  207. return bufStat.Ino == C.BTRFS_FIRST_FREE_OBJECTID, nil
  208. }
  209. func subvolDelete(dirpath, name string, quotaEnabled bool) error {
  210. dir, err := openDir(dirpath)
  211. if err != nil {
  212. return err
  213. }
  214. defer closeDir(dir)
  215. fullPath := path.Join(dirpath, name)
  216. var args C.struct_btrfs_ioctl_vol_args
  217. // walk the btrfs subvolumes
  218. walkSubvolumes := func(p string, f os.FileInfo, err error) error {
  219. if err != nil {
  220. if os.IsNotExist(err) && p != fullPath {
  221. // missing most likely because the path was a subvolume that got removed in the previous iteration
  222. // since it's gone anyway, we don't care
  223. return nil
  224. }
  225. return fmt.Errorf("error walking subvolumes: %v", err)
  226. }
  227. // we want to check children only so skip itself
  228. // it will be removed after the filepath walk anyways
  229. if f.IsDir() && p != fullPath {
  230. sv, err := isSubvolume(p)
  231. if err != nil {
  232. return fmt.Errorf("Failed to test if %s is a btrfs subvolume: %v", p, err)
  233. }
  234. if sv {
  235. if err := subvolDelete(path.Dir(p), f.Name(), quotaEnabled); err != nil {
  236. return fmt.Errorf("Failed to destroy btrfs child subvolume (%s) of parent (%s): %v", p, dirpath, err)
  237. }
  238. }
  239. }
  240. return nil
  241. }
  242. if err := filepath.Walk(path.Join(dirpath, name), walkSubvolumes); err != nil {
  243. return fmt.Errorf("Recursively walking subvolumes for %s failed: %v", dirpath, err)
  244. }
  245. if quotaEnabled {
  246. if qgroupid, err := subvolLookupQgroup(fullPath); err == nil {
  247. var args C.struct_btrfs_ioctl_qgroup_create_args
  248. args.qgroupid = C.__u64(qgroupid)
  249. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QGROUP_CREATE,
  250. uintptr(unsafe.Pointer(&args)))
  251. if errno != 0 {
  252. logrus.WithField("storage-driver", "btrfs").Errorf("Failed to delete btrfs qgroup %v for %s: %v", qgroupid, fullPath, errno.Error())
  253. }
  254. } else {
  255. logrus.WithField("storage-driver", "btrfs").Errorf("Failed to lookup btrfs qgroup for %s: %v", fullPath, err.Error())
  256. }
  257. }
  258. // all subvolumes have been removed
  259. // now remove the one originally passed in
  260. for i, c := range []byte(name) {
  261. args.name[i] = C.char(c)
  262. }
  263. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_SNAP_DESTROY,
  264. uintptr(unsafe.Pointer(&args)))
  265. if errno != 0 {
  266. return fmt.Errorf("Failed to destroy btrfs snapshot %s for %s: %v", dirpath, name, errno.Error())
  267. }
  268. return nil
  269. }
  270. func (d *Driver) updateQuotaStatus() {
  271. d.once.Do(func() {
  272. if !d.quotaEnabled {
  273. // In case quotaEnabled is not set, check qgroup and update quotaEnabled as needed
  274. if err := subvolQgroupStatus(d.home); err != nil {
  275. // quota is still not enabled
  276. return
  277. }
  278. d.quotaEnabled = true
  279. }
  280. })
  281. }
  282. func (d *Driver) subvolEnableQuota() error {
  283. d.updateQuotaStatus()
  284. if d.quotaEnabled {
  285. return nil
  286. }
  287. dir, err := openDir(d.home)
  288. if err != nil {
  289. return err
  290. }
  291. defer closeDir(dir)
  292. var args C.struct_btrfs_ioctl_quota_ctl_args
  293. args.cmd = C.BTRFS_QUOTA_CTL_ENABLE
  294. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QUOTA_CTL,
  295. uintptr(unsafe.Pointer(&args)))
  296. if errno != 0 {
  297. return fmt.Errorf("Failed to enable btrfs quota for %s: %v", dir, errno.Error())
  298. }
  299. d.quotaEnabled = true
  300. return nil
  301. }
  302. func (d *Driver) subvolDisableQuota() error {
  303. d.updateQuotaStatus()
  304. if !d.quotaEnabled {
  305. return nil
  306. }
  307. dir, err := openDir(d.home)
  308. if err != nil {
  309. return err
  310. }
  311. defer closeDir(dir)
  312. var args C.struct_btrfs_ioctl_quota_ctl_args
  313. args.cmd = C.BTRFS_QUOTA_CTL_DISABLE
  314. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QUOTA_CTL,
  315. uintptr(unsafe.Pointer(&args)))
  316. if errno != 0 {
  317. return fmt.Errorf("Failed to disable btrfs quota for %s: %v", dir, errno.Error())
  318. }
  319. d.quotaEnabled = false
  320. return nil
  321. }
  322. func (d *Driver) subvolRescanQuota() error {
  323. d.updateQuotaStatus()
  324. if !d.quotaEnabled {
  325. return nil
  326. }
  327. dir, err := openDir(d.home)
  328. if err != nil {
  329. return err
  330. }
  331. defer closeDir(dir)
  332. var args C.struct_btrfs_ioctl_quota_rescan_args
  333. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QUOTA_RESCAN_WAIT,
  334. uintptr(unsafe.Pointer(&args)))
  335. if errno != 0 {
  336. return fmt.Errorf("Failed to rescan btrfs quota for %s: %v", dir, errno.Error())
  337. }
  338. return nil
  339. }
  340. func subvolLimitQgroup(path string, size uint64) error {
  341. dir, err := openDir(path)
  342. if err != nil {
  343. return err
  344. }
  345. defer closeDir(dir)
  346. var args C.struct_btrfs_ioctl_qgroup_limit_args
  347. args.lim.max_referenced = C.__u64(size)
  348. args.lim.flags = C.BTRFS_QGROUP_LIMIT_MAX_RFER
  349. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QGROUP_LIMIT,
  350. uintptr(unsafe.Pointer(&args)))
  351. if errno != 0 {
  352. return fmt.Errorf("Failed to limit qgroup for %s: %v", dir, errno.Error())
  353. }
  354. return nil
  355. }
  356. // subvolQgroupStatus performs a BTRFS_IOC_TREE_SEARCH on the root path
  357. // with search key of BTRFS_QGROUP_STATUS_KEY.
  358. // In case qgroup is enabled, the retuned key type will match BTRFS_QGROUP_STATUS_KEY.
  359. // For more details please see https://github.com/kdave/btrfs-progs/blob/v4.9/qgroup.c#L1035
  360. func subvolQgroupStatus(path string) error {
  361. dir, err := openDir(path)
  362. if err != nil {
  363. return err
  364. }
  365. defer closeDir(dir)
  366. var args C.struct_btrfs_ioctl_search_args
  367. args.key.tree_id = C.BTRFS_QUOTA_TREE_OBJECTID
  368. args.key.min_type = C.BTRFS_QGROUP_STATUS_KEY
  369. args.key.max_type = C.BTRFS_QGROUP_STATUS_KEY
  370. args.key.max_objectid = C.__u64(math.MaxUint64)
  371. args.key.max_offset = C.__u64(math.MaxUint64)
  372. args.key.max_transid = C.__u64(math.MaxUint64)
  373. args.key.nr_items = 4096
  374. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_TREE_SEARCH,
  375. uintptr(unsafe.Pointer(&args)))
  376. if errno != 0 {
  377. return fmt.Errorf("Failed to search qgroup for %s: %v", path, errno.Error())
  378. }
  379. sh := (*C.struct_btrfs_ioctl_search_header)(unsafe.Pointer(&args.buf))
  380. if sh._type != C.BTRFS_QGROUP_STATUS_KEY {
  381. return fmt.Errorf("Invalid qgroup search header type for %s: %v", path, sh._type)
  382. }
  383. return nil
  384. }
  385. func subvolLookupQgroup(path string) (uint64, error) {
  386. dir, err := openDir(path)
  387. if err != nil {
  388. return 0, err
  389. }
  390. defer closeDir(dir)
  391. var args C.struct_btrfs_ioctl_ino_lookup_args
  392. args.objectid = C.BTRFS_FIRST_FREE_OBJECTID
  393. _, _, errno := unix.Syscall(unix.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_INO_LOOKUP,
  394. uintptr(unsafe.Pointer(&args)))
  395. if errno != 0 {
  396. return 0, fmt.Errorf("Failed to lookup qgroup for %s: %v", dir, errno.Error())
  397. }
  398. if args.treeid == 0 {
  399. return 0, fmt.Errorf("Invalid qgroup id for %s: 0", dir)
  400. }
  401. return uint64(args.treeid), nil
  402. }
  403. func (d *Driver) subvolumesDir() string {
  404. return path.Join(d.home, "subvolumes")
  405. }
  406. func (d *Driver) subvolumesDirID(id string) string {
  407. return path.Join(d.subvolumesDir(), id)
  408. }
  409. func (d *Driver) quotasDir() string {
  410. return path.Join(d.home, "quotas")
  411. }
  412. func (d *Driver) quotasDirID(id string) string {
  413. return path.Join(d.quotasDir(), id)
  414. }
  415. // CreateReadWrite creates a layer that is writable for use as a container
  416. // file system.
  417. func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
  418. return d.Create(id, parent, opts)
  419. }
  420. // Create the filesystem with given id.
  421. func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
  422. quotas := path.Join(d.home, "quotas")
  423. subvolumes := path.Join(d.home, "subvolumes")
  424. rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
  425. if err != nil {
  426. return err
  427. }
  428. if err := idtools.MkdirAllAndChown(subvolumes, 0700, idtools.IDPair{UID: rootUID, GID: rootGID}); err != nil {
  429. return err
  430. }
  431. if parent == "" {
  432. if err := subvolCreate(subvolumes, id); err != nil {
  433. return err
  434. }
  435. } else {
  436. parentDir := d.subvolumesDirID(parent)
  437. st, err := os.Stat(parentDir)
  438. if err != nil {
  439. return err
  440. }
  441. if !st.IsDir() {
  442. return fmt.Errorf("%s: not a directory", parentDir)
  443. }
  444. if err := subvolSnapshot(parentDir, subvolumes, id); err != nil {
  445. return err
  446. }
  447. }
  448. var storageOpt map[string]string
  449. if opts != nil {
  450. storageOpt = opts.StorageOpt
  451. }
  452. if _, ok := storageOpt["size"]; ok {
  453. driver := &Driver{}
  454. if err := d.parseStorageOpt(storageOpt, driver); err != nil {
  455. return err
  456. }
  457. if err := d.setStorageSize(path.Join(subvolumes, id), driver); err != nil {
  458. return err
  459. }
  460. if err := idtools.MkdirAllAndChown(quotas, 0700, idtools.IDPair{UID: rootUID, GID: rootGID}); err != nil {
  461. return err
  462. }
  463. if err := ioutil.WriteFile(path.Join(quotas, id), []byte(fmt.Sprint(driver.options.size)), 0644); err != nil {
  464. return err
  465. }
  466. }
  467. // if we have a remapped root (user namespaces enabled), change the created snapshot
  468. // dir ownership to match
  469. if rootUID != 0 || rootGID != 0 {
  470. if err := os.Chown(path.Join(subvolumes, id), rootUID, rootGID); err != nil {
  471. return err
  472. }
  473. }
  474. mountLabel := ""
  475. if opts != nil {
  476. mountLabel = opts.MountLabel
  477. }
  478. return label.Relabel(path.Join(subvolumes, id), mountLabel, false)
  479. }
  480. // Parse btrfs storage options
  481. func (d *Driver) parseStorageOpt(storageOpt map[string]string, driver *Driver) error {
  482. // Read size to change the subvolume disk quota per container
  483. for key, val := range storageOpt {
  484. key := strings.ToLower(key)
  485. switch key {
  486. case "size":
  487. size, err := units.RAMInBytes(val)
  488. if err != nil {
  489. return err
  490. }
  491. driver.options.size = uint64(size)
  492. default:
  493. return fmt.Errorf("Unknown option %s", key)
  494. }
  495. }
  496. return nil
  497. }
  498. // Set btrfs storage size
  499. func (d *Driver) setStorageSize(dir string, driver *Driver) error {
  500. if driver.options.size <= 0 {
  501. return fmt.Errorf("btrfs: invalid storage size: %s", units.HumanSize(float64(driver.options.size)))
  502. }
  503. if d.options.minSpace > 0 && driver.options.size < d.options.minSpace {
  504. return fmt.Errorf("btrfs: storage size cannot be less than %s", units.HumanSize(float64(d.options.minSpace)))
  505. }
  506. if err := d.subvolEnableQuota(); err != nil {
  507. return err
  508. }
  509. return subvolLimitQgroup(dir, driver.options.size)
  510. }
  511. // Remove the filesystem with given id.
  512. func (d *Driver) Remove(id string) error {
  513. dir := d.subvolumesDirID(id)
  514. if _, err := os.Stat(dir); err != nil {
  515. return err
  516. }
  517. quotasDir := d.quotasDirID(id)
  518. if _, err := os.Stat(quotasDir); err == nil {
  519. if err := os.Remove(quotasDir); err != nil {
  520. return err
  521. }
  522. } else if !os.IsNotExist(err) {
  523. return err
  524. }
  525. // Call updateQuotaStatus() to invoke status update
  526. d.updateQuotaStatus()
  527. if err := subvolDelete(d.subvolumesDir(), id, d.quotaEnabled); err != nil {
  528. return err
  529. }
  530. if err := system.EnsureRemoveAll(dir); err != nil {
  531. return err
  532. }
  533. return d.subvolRescanQuota()
  534. }
  535. // Get the requested filesystem id.
  536. func (d *Driver) Get(id, mountLabel string) (containerfs.ContainerFS, error) {
  537. dir := d.subvolumesDirID(id)
  538. st, err := os.Stat(dir)
  539. if err != nil {
  540. return nil, err
  541. }
  542. if !st.IsDir() {
  543. return nil, fmt.Errorf("%s: not a directory", dir)
  544. }
  545. if quota, err := ioutil.ReadFile(d.quotasDirID(id)); err == nil {
  546. if size, err := strconv.ParseUint(string(quota), 10, 64); err == nil && size >= d.options.minSpace {
  547. if err := d.subvolEnableQuota(); err != nil {
  548. return nil, err
  549. }
  550. if err := subvolLimitQgroup(dir, size); err != nil {
  551. return nil, err
  552. }
  553. }
  554. }
  555. return containerfs.NewLocalContainerFS(dir), nil
  556. }
  557. // Put is not implemented for BTRFS as there is no cleanup required for the id.
  558. func (d *Driver) Put(id string) error {
  559. // Get() creates no runtime resources (like e.g. mounts)
  560. // so this doesn't need to do anything.
  561. return nil
  562. }
  563. // Exists checks if the id exists in the filesystem.
  564. func (d *Driver) Exists(id string) bool {
  565. dir := d.subvolumesDirID(id)
  566. _, err := os.Stat(dir)
  567. return err == nil
  568. }