btrfs.go 14 KB

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