btrfs.go 18 KB

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