btrfs.go 17 KB

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