mirror of https://github.com/xSmurf/oz.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.1 KiB
103 lines
2.1 KiB
10 years ago
|
// +build linux,!gccgo
|
||
|
package mount
|
||
|
|
||
|
// extern int enter_mount_namespace(void);
|
||
|
/*
|
||
|
#include <stdlib.h>
|
||
|
__attribute__((constructor)) void init(void) {
|
||
|
if (enter_mount_namespace() < 0) {
|
||
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
}
|
||
|
*/
|
||
|
import "C"
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
"path"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/subgraph/oz"
|
||
|
"github.com/subgraph/oz/fs"
|
||
|
|
||
|
"github.com/op/go-logging"
|
||
|
)
|
||
|
|
||
|
const (
|
||
|
MOUNT = 1 << iota
|
||
|
UMOUNT
|
||
|
)
|
||
|
|
||
|
func Main(mode int) {
|
||
|
log := createLogger()
|
||
|
config, err := loadConfig()
|
||
|
if err != nil {
|
||
|
log.Error("Could not load configuration: %s\n", oz.DefaultConfigPath, err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
|
||
|
fsys := fs.NewFilesystem(config, log)
|
||
|
for fii, fpath := range os.Args {
|
||
|
if fii == 0 {
|
||
|
continue
|
||
|
}
|
||
|
if !strings.HasPrefix(fpath, "/home/") {
|
||
|
log.Warning("Ignored `%s`, only files inside of home are permitted!", fpath)
|
||
|
continue
|
||
|
}
|
||
|
switch mode {
|
||
|
case MOUNT:
|
||
|
mount(fpath, fsys, log)
|
||
|
case UMOUNT:
|
||
|
unmount(fpath, fsys, log)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
os.Exit(0)
|
||
|
}
|
||
|
|
||
|
func mount(fpath string, fsys *fs.Filesystem, log *logging.Logger) {
|
||
|
if _, err := os.Stat(fpath); err == nil {
|
||
|
//log.Notice("Adding file `%s`.", fpath)
|
||
|
if err := fsys.BindPath(fpath, fs.BindCanCreate, nil); err != nil {
|
||
|
log.Error("%v while adding `%s`!", err, fpath)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func unmount(fpath string, fsys *fs.Filesystem, log *logging.Logger) {
|
||
|
sbpath := path.Join(fsys.Root(), fpath)
|
||
|
if _, err := os.Stat(sbpath); err == nil {
|
||
|
//log.Notice("Removing file `%s`.", fpath)
|
||
|
if err := fsys.UnbindPath(fpath); err != nil {
|
||
|
log.Error("%v while removing `%s`!", err, fpath)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
} else {
|
||
|
log.Error("%v error while removing `%s`!", err, fpath)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func createLogger() *logging.Logger {
|
||
|
l := logging.MustGetLogger("oz-init")
|
||
|
be := logging.NewLogBackend(os.Stderr, "", 0)
|
||
|
f := logging.MustStringFormatter("%{level:.1s} %{message}")
|
||
|
fbe := logging.NewBackendFormatter(be, f)
|
||
|
logging.SetBackend(fbe)
|
||
|
return l
|
||
|
}
|
||
|
|
||
|
func loadConfig() (*oz.Config, error) {
|
||
|
config, err := oz.LoadConfig(oz.DefaultConfigPath)
|
||
|
if err != nil {
|
||
|
if os.IsNotExist(err) {
|
||
|
config = oz.NewDefaultConfig()
|
||
|
} else {
|
||
|
return nil, err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return config, nil
|
||
|
}
|