btrfs.go 17 KB

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