btrfs.go 12 KB

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