Skip to content

Instantly share code, notes, and snippets.

@erikh
Created October 18, 2016 03:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erikh/073334db7f91fdbf55d81523435ffafe to your computer and use it in GitHub Desktop.
Save erikh/073334db7f91fdbf55d81523435ffafe to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"io/ioutil"
"os"
"golang.org/x/sys/unix"
)
// Layer encapsulates a specific mount
type Layer struct {
lower string
upper string
rootfs string
workdir string
}
// FS is a representation of a fully mounted system
type FS struct {
Mountpoint string
Layers []*Layer
}
func main() {
if len(os.Args) < 3 {
panic("i pity the fool who doesn't pass arguments")
}
args := os.Args[1:]
fs := &FS{Layers: []*Layer{}}
for i := 0; i < len(args); i++ {
var upper, lower, workdir string
var err error
if i != 0 {
lower = args[i-1]
}
upper, err = ioutil.TempDir("", "layer-")
if err != nil {
panic(err)
}
workdir, err = ioutil.TempDir("", "workdir-")
if err != nil {
panic(err)
}
fs.Layers = append(fs.Layers, &Layer{
lower: lower,
upper: upper,
workdir: workdir,
rootfs: args[i],
})
}
fs.Layers[1].lower = fs.Layers[0].rootfs
for i, layer := range fs.Layers {
os.Mkdir(layer.rootfs, 0777)
if i == 0 {
continue
}
err := unix.Mount("overlay", layer.rootfs, "overlay", 0, fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", layer.lower, layer.upper, layer.workdir))
if err != nil {
panic(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment