btrfs.go 18 KB

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