btrfs.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. // +build linux
  2. package 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. "os"
  16. "path"
  17. "path/filepath"
  18. "strings"
  19. "syscall"
  20. "unsafe"
  21. "github.com/docker/docker/daemon/graphdriver"
  22. "github.com/docker/docker/pkg/idtools"
  23. "github.com/docker/docker/pkg/mount"
  24. "github.com/docker/docker/pkg/parsers"
  25. "github.com/docker/docker/pkg/system"
  26. "github.com/docker/go-units"
  27. "github.com/opencontainers/selinux/go-selinux/label"
  28. )
  29. func init() {
  30. graphdriver.Register("btrfs", Init)
  31. }
  32. type btrfsOptions struct {
  33. minSpace uint64
  34. size uint64
  35. }
  36. // Init returns a new BTRFS driver.
  37. // An error is returned if BTRFS is not supported.
  38. func Init(home string, options []string, uidMaps, gidMaps []idtools.IDMap) (graphdriver.Driver, error) {
  39. fsMagic, err := graphdriver.GetFSMagic(home)
  40. if err != nil {
  41. return nil, err
  42. }
  43. if fsMagic != graphdriver.FsMagicBtrfs {
  44. return nil, graphdriver.ErrPrerequisites
  45. }
  46. rootUID, rootGID, err := idtools.GetRootUIDGID(uidMaps, gidMaps)
  47. if err != nil {
  48. return nil, err
  49. }
  50. if err := idtools.MkdirAllAs(home, 0700, rootUID, rootGID); err != nil {
  51. return nil, err
  52. }
  53. if err := mount.MakePrivate(home); err != nil {
  54. return nil, err
  55. }
  56. opt, userDiskQuota, err := parseOptions(options)
  57. if err != nil {
  58. return nil, err
  59. }
  60. driver := &Driver{
  61. home: home,
  62. uidMaps: uidMaps,
  63. gidMaps: gidMaps,
  64. options: opt,
  65. }
  66. if userDiskQuota {
  67. if err := driver.subvolEnableQuota(); err != nil {
  68. return nil, err
  69. }
  70. }
  71. return graphdriver.NewNaiveDiffDriver(driver, uidMaps, gidMaps), nil
  72. }
  73. func parseOptions(opt []string) (btrfsOptions, bool, error) {
  74. var options btrfsOptions
  75. userDiskQuota := false
  76. for _, option := range opt {
  77. key, val, err := parsers.ParseKeyValueOpt(option)
  78. if err != nil {
  79. return options, userDiskQuota, err
  80. }
  81. key = strings.ToLower(key)
  82. switch key {
  83. case "btrfs.min_space":
  84. minSpace, err := units.RAMInBytes(val)
  85. if err != nil {
  86. return options, userDiskQuota, err
  87. }
  88. userDiskQuota = true
  89. options.minSpace = uint64(minSpace)
  90. default:
  91. return options, userDiskQuota, fmt.Errorf("Unknown option %s", key)
  92. }
  93. }
  94. return options, userDiskQuota, nil
  95. }
  96. // Driver contains information about the filesystem mounted.
  97. type Driver struct {
  98. //root of the file system
  99. home string
  100. uidMaps []idtools.IDMap
  101. gidMaps []idtools.IDMap
  102. options btrfsOptions
  103. quotaEnabled bool
  104. }
  105. // String prints the name of the driver (btrfs).
  106. func (d *Driver) String() string {
  107. return "btrfs"
  108. }
  109. // Status returns current driver information in a two dimensional string array.
  110. // Output contains "Build Version" and "Library Version" of the btrfs libraries used.
  111. // Version information can be used to check compatibility with your kernel.
  112. func (d *Driver) Status() [][2]string {
  113. status := [][2]string{}
  114. if bv := btrfsBuildVersion(); bv != "-" {
  115. status = append(status, [2]string{"Build Version", bv})
  116. }
  117. if lv := btrfsLibVersion(); lv != -1 {
  118. status = append(status, [2]string{"Library Version", fmt.Sprintf("%d", lv)})
  119. }
  120. return status
  121. }
  122. // GetMetadata returns empty metadata for this driver.
  123. func (d *Driver) GetMetadata(id string) (map[string]string, error) {
  124. return nil, nil
  125. }
  126. // Cleanup unmounts the home directory.
  127. func (d *Driver) Cleanup() error {
  128. if err := d.subvolDisableQuota(); err != nil {
  129. return err
  130. }
  131. return mount.Unmount(d.home)
  132. }
  133. func free(p *C.char) {
  134. C.free(unsafe.Pointer(p))
  135. }
  136. func openDir(path string) (*C.DIR, error) {
  137. Cpath := C.CString(path)
  138. defer free(Cpath)
  139. dir := C.opendir(Cpath)
  140. if dir == nil {
  141. return nil, fmt.Errorf("Can't open dir")
  142. }
  143. return dir, nil
  144. }
  145. func closeDir(dir *C.DIR) {
  146. if dir != nil {
  147. C.closedir(dir)
  148. }
  149. }
  150. func getDirFd(dir *C.DIR) uintptr {
  151. return uintptr(C.dirfd(dir))
  152. }
  153. func subvolCreate(path, name string) error {
  154. dir, err := openDir(path)
  155. if err != nil {
  156. return err
  157. }
  158. defer closeDir(dir)
  159. var args C.struct_btrfs_ioctl_vol_args
  160. for i, c := range []byte(name) {
  161. args.name[i] = C.char(c)
  162. }
  163. _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_SUBVOL_CREATE,
  164. uintptr(unsafe.Pointer(&args)))
  165. if errno != 0 {
  166. return fmt.Errorf("Failed to create btrfs subvolume: %v", errno.Error())
  167. }
  168. return nil
  169. }
  170. func subvolSnapshot(src, dest, name string) error {
  171. srcDir, err := openDir(src)
  172. if err != nil {
  173. return err
  174. }
  175. defer closeDir(srcDir)
  176. destDir, err := openDir(dest)
  177. if err != nil {
  178. return err
  179. }
  180. defer closeDir(destDir)
  181. var args C.struct_btrfs_ioctl_vol_args_v2
  182. args.fd = C.__s64(getDirFd(srcDir))
  183. var cs = C.CString(name)
  184. C.set_name_btrfs_ioctl_vol_args_v2(&args, cs)
  185. C.free(unsafe.Pointer(cs))
  186. _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, getDirFd(destDir), C.BTRFS_IOC_SNAP_CREATE_V2,
  187. uintptr(unsafe.Pointer(&args)))
  188. if errno != 0 {
  189. return fmt.Errorf("Failed to create btrfs snapshot: %v", errno.Error())
  190. }
  191. return nil
  192. }
  193. func isSubvolume(p string) (bool, error) {
  194. var bufStat syscall.Stat_t
  195. if err := syscall.Lstat(p, &bufStat); err != nil {
  196. return false, err
  197. }
  198. // return true if it is a btrfs subvolume
  199. return bufStat.Ino == C.BTRFS_FIRST_FREE_OBJECTID, nil
  200. }
  201. func subvolDelete(dirpath, name string) error {
  202. dir, err := openDir(dirpath)
  203. if err != nil {
  204. return err
  205. }
  206. defer closeDir(dir)
  207. fullPath := path.Join(dirpath, name)
  208. var args C.struct_btrfs_ioctl_vol_args
  209. // walk the btrfs subvolumes
  210. walkSubvolumes := func(p string, f os.FileInfo, err error) error {
  211. if err != nil {
  212. if os.IsNotExist(err) && p != fullPath {
  213. // missing most likely because the path was a subvolume that got removed in the previous iteration
  214. // since it's gone anyway, we don't care
  215. return nil
  216. }
  217. return fmt.Errorf("error walking subvolumes: %v", err)
  218. }
  219. // we want to check children only so skip itself
  220. // it will be removed after the filepath walk anyways
  221. if f.IsDir() && p != fullPath {
  222. sv, err := isSubvolume(p)
  223. if err != nil {
  224. return fmt.Errorf("Failed to test if %s is a btrfs subvolume: %v", p, err)
  225. }
  226. if sv {
  227. if err := subvolDelete(path.Dir(p), f.Name()); err != nil {
  228. return fmt.Errorf("Failed to destroy btrfs child subvolume (%s) of parent (%s): %v", p, dirpath, err)
  229. }
  230. }
  231. }
  232. return nil
  233. }
  234. if err := filepath.Walk(path.Join(dirpath, name), walkSubvolumes); err != nil {
  235. return fmt.Errorf("Recursively walking subvolumes for %s failed: %v", dirpath, err)
  236. }
  237. // all subvolumes have been removed
  238. // now remove the one originally passed in
  239. for i, c := range []byte(name) {
  240. args.name[i] = C.char(c)
  241. }
  242. _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_SNAP_DESTROY,
  243. uintptr(unsafe.Pointer(&args)))
  244. if errno != 0 {
  245. return fmt.Errorf("Failed to destroy btrfs snapshot %s for %s: %v", dirpath, name, errno.Error())
  246. }
  247. return nil
  248. }
  249. func (d *Driver) subvolEnableQuota() error {
  250. if d.quotaEnabled {
  251. return nil
  252. }
  253. // In case quotaEnabled is not set, check qgroup and update quotaEnabled as needed
  254. if _, err := subvolLookupQgroup(d.home); err == nil {
  255. d.quotaEnabled = true
  256. return nil
  257. }
  258. dir, err := openDir(d.home)
  259. if err != nil {
  260. return err
  261. }
  262. defer closeDir(dir)
  263. var args C.struct_btrfs_ioctl_quota_ctl_args
  264. args.cmd = C.BTRFS_QUOTA_CTL_ENABLE
  265. _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QUOTA_CTL,
  266. uintptr(unsafe.Pointer(&args)))
  267. if errno != 0 {
  268. return fmt.Errorf("Failed to enable btrfs quota for %s: %v", dir, errno.Error())
  269. }
  270. d.quotaEnabled = true
  271. return nil
  272. }
  273. func (d *Driver) subvolDisableQuota() error {
  274. if !d.quotaEnabled {
  275. // In case quotaEnabled is not set, check qgroup and update quotaEnabled as needed
  276. if _, err := subvolLookupQgroup(d.home); err != nil {
  277. // quota is still not enabled
  278. return nil
  279. }
  280. d.quotaEnabled = true
  281. }
  282. dir, err := openDir(d.home)
  283. if err != nil {
  284. return err
  285. }
  286. defer closeDir(dir)
  287. var args C.struct_btrfs_ioctl_quota_ctl_args
  288. args.cmd = C.BTRFS_QUOTA_CTL_DISABLE
  289. _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QUOTA_CTL,
  290. uintptr(unsafe.Pointer(&args)))
  291. if errno != 0 {
  292. return fmt.Errorf("Failed to disable btrfs quota for %s: %v", dir, errno.Error())
  293. }
  294. d.quotaEnabled = false
  295. return nil
  296. }
  297. func (d *Driver) subvolRescanQuota() error {
  298. if !d.quotaEnabled {
  299. // In case quotaEnabled is not set, check qgroup and update quotaEnabled as needed
  300. if _, err := subvolLookupQgroup(d.home); err != nil {
  301. // quota is still not enabled
  302. return nil
  303. }
  304. d.quotaEnabled = true
  305. }
  306. dir, err := openDir(d.home)
  307. if err != nil {
  308. return err
  309. }
  310. defer closeDir(dir)
  311. var args C.struct_btrfs_ioctl_quota_rescan_args
  312. _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QUOTA_RESCAN_WAIT,
  313. uintptr(unsafe.Pointer(&args)))
  314. if errno != 0 {
  315. return fmt.Errorf("Failed to rescan btrfs quota for %s: %v", dir, errno.Error())
  316. }
  317. return nil
  318. }
  319. func subvolLimitQgroup(path string, size uint64) error {
  320. dir, err := openDir(path)
  321. if err != nil {
  322. return err
  323. }
  324. defer closeDir(dir)
  325. var args C.struct_btrfs_ioctl_qgroup_limit_args
  326. args.lim.max_referenced = C.__u64(size)
  327. args.lim.flags = C.BTRFS_QGROUP_LIMIT_MAX_RFER
  328. _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_QGROUP_LIMIT,
  329. uintptr(unsafe.Pointer(&args)))
  330. if errno != 0 {
  331. return fmt.Errorf("Failed to limit qgroup for %s: %v", dir, errno.Error())
  332. }
  333. return nil
  334. }
  335. func subvolLookupQgroup(path string) (uint64, error) {
  336. dir, err := openDir(path)
  337. if err != nil {
  338. return 0, err
  339. }
  340. defer closeDir(dir)
  341. var args C.struct_btrfs_ioctl_ino_lookup_args
  342. args.objectid = C.BTRFS_FIRST_FREE_OBJECTID
  343. _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, getDirFd(dir), C.BTRFS_IOC_INO_LOOKUP,
  344. uintptr(unsafe.Pointer(&args)))
  345. if errno != 0 {
  346. return 0, fmt.Errorf("Failed to lookup qgroup for %s: %v", dir, errno.Error())
  347. }
  348. if args.treeid == 0 {
  349. return 0, fmt.Errorf("Invalid qgroup id for %s: 0", dir)
  350. }
  351. return uint64(args.treeid), nil
  352. }
  353. func (d *Driver) subvolumesDir() string {
  354. return path.Join(d.home, "subvolumes")
  355. }
  356. func (d *Driver) subvolumesDirID(id string) string {
  357. return path.Join(d.subvolumesDir(), id)
  358. }
  359. // CreateReadWrite creates a layer that is writable for use as a container
  360. // file system.
  361. func (d *Driver) CreateReadWrite(id, parent string, opts *graphdriver.CreateOpts) error {
  362. return d.Create(id, parent, opts)
  363. }
  364. // Create the filesystem with given id.
  365. func (d *Driver) Create(id, parent string, opts *graphdriver.CreateOpts) error {
  366. subvolumes := path.Join(d.home, "subvolumes")
  367. rootUID, rootGID, err := idtools.GetRootUIDGID(d.uidMaps, d.gidMaps)
  368. if err != nil {
  369. return err
  370. }
  371. if err := idtools.MkdirAllAs(subvolumes, 0700, rootUID, rootGID); err != nil {
  372. return err
  373. }
  374. if parent == "" {
  375. if err := subvolCreate(subvolumes, id); err != nil {
  376. return err
  377. }
  378. } else {
  379. parentDir := d.subvolumesDirID(parent)
  380. st, err := os.Stat(parentDir)
  381. if err != nil {
  382. return err
  383. }
  384. if !st.IsDir() {
  385. return fmt.Errorf("%s: not a directory", parentDir)
  386. }
  387. if err := subvolSnapshot(parentDir, subvolumes, id); err != nil {
  388. return err
  389. }
  390. }
  391. var storageOpt map[string]string
  392. if opts != nil {
  393. storageOpt = opts.StorageOpt
  394. }
  395. if _, ok := storageOpt["size"]; ok {
  396. driver := &Driver{}
  397. if err := d.parseStorageOpt(storageOpt, driver); err != nil {
  398. return err
  399. }
  400. if err := d.setStorageSize(path.Join(subvolumes, id), driver); err != nil {
  401. return err
  402. }
  403. }
  404. // if we have a remapped root (user namespaces enabled), change the created snapshot
  405. // dir ownership to match
  406. if rootUID != 0 || rootGID != 0 {
  407. if err := os.Chown(path.Join(subvolumes, id), rootUID, rootGID); err != nil {
  408. return err
  409. }
  410. }
  411. mountLabel := ""
  412. if opts != nil {
  413. mountLabel = opts.MountLabel
  414. }
  415. return label.Relabel(path.Join(subvolumes, id), mountLabel, false)
  416. }
  417. // Parse btrfs storage options
  418. func (d *Driver) parseStorageOpt(storageOpt map[string]string, driver *Driver) error {
  419. // Read size to change the subvolume disk quota per container
  420. for key, val := range storageOpt {
  421. key := strings.ToLower(key)
  422. switch key {
  423. case "size":
  424. size, err := units.RAMInBytes(val)
  425. if err != nil {
  426. return err
  427. }
  428. driver.options.size = uint64(size)
  429. default:
  430. return fmt.Errorf("Unknown option %s", key)
  431. }
  432. }
  433. return nil
  434. }
  435. // Set btrfs storage size
  436. func (d *Driver) setStorageSize(dir string, driver *Driver) error {
  437. if driver.options.size <= 0 {
  438. return fmt.Errorf("btrfs: invalid storage size: %s", units.HumanSize(float64(driver.options.size)))
  439. }
  440. if d.options.minSpace > 0 && driver.options.size < d.options.minSpace {
  441. return fmt.Errorf("btrfs: storage size cannot be less than %s", units.HumanSize(float64(d.options.minSpace)))
  442. }
  443. if err := d.subvolEnableQuota(); err != nil {
  444. return err
  445. }
  446. if err := subvolLimitQgroup(dir, driver.options.size); err != nil {
  447. return err
  448. }
  449. return nil
  450. }
  451. // Remove the filesystem with given id.
  452. func (d *Driver) Remove(id string) error {
  453. dir := d.subvolumesDirID(id)
  454. if _, err := os.Stat(dir); err != nil {
  455. return err
  456. }
  457. if err := subvolDelete(d.subvolumesDir(), id); err != nil {
  458. return err
  459. }
  460. if err := system.EnsureRemoveAll(dir); err != nil {
  461. return err
  462. }
  463. if err := d.subvolRescanQuota(); err != nil {
  464. return err
  465. }
  466. return nil
  467. }
  468. // Get the requested filesystem id.
  469. func (d *Driver) Get(id, mountLabel string) (string, error) {
  470. dir := d.subvolumesDirID(id)
  471. st, err := os.Stat(dir)
  472. if err != nil {
  473. return "", err
  474. }
  475. if !st.IsDir() {
  476. return "", fmt.Errorf("%s: not a directory", dir)
  477. }
  478. return dir, nil
  479. }
  480. // Put is not implemented for BTRFS as there is no cleanup required for the id.
  481. func (d *Driver) Put(id string) error {
  482. // Get() creates no runtime resources (like e.g. mounts)
  483. // so this doesn't need to do anything.
  484. return nil
  485. }
  486. // Exists checks if the id exists in the filesystem.
  487. func (d *Driver) Exists(id string) bool {
  488. dir := d.subvolumesDirID(id)
  489. _, err := os.Stat(dir)
  490. return err == nil
  491. }