我需要编写程序,它将使用所有类型的fyle系统挂载(工作)。
在linux中,您可以安装不同的文件系统。并且每个文件系统都有参数,一些"文件夹"甚至是命令的执行顺序。 示例:
实质上,每个文件系统都有两种状态: - 安装 - 未安装
每个人都可以进行有限数量的操作:
PS: 我用Go语言编写代码。我写了一些代码http://play.golang.org/p/Ri5yAqsLu7,但它不适合。
package main
import (
"fmt"
"sort"
"sync"
"os/exec"
"strings"
)
type LayerForMount struct {
Upper string
Lower string
MountPoint string
MountOptions string
FSType string
}
type LayersOverlayFS struct {
layers map[int]LayerForMount
sync.RWMutex
}
func (l *LayersOverlayFS) Add(layer LayerForMount, key int) error {
l.Lock()
defer l.Unlock()
if _, ok := l.layers[key]; ok {
return fmt.Errorf("key %v exist", key)
}
l.layers[key] = layer
return nil
}
func (l *LayersOverlayFS) Remove(key int) {
l.Lock()
defer l.Unlock()
delete(l.layers, key)
}
/*
mount lower,upper dirs to mountpoint if mountpoint not mounted
TODO: cheack that lower,upper == to already mounted
*/
func (l *LayersOverlayFS) Mount() (err error) {
l.RLock()
defer l.RUnlock()
forceRemount := false
var keys []int
for k, _ := range l.layers {
keys = append(keys, k)
}
sort.Ints(keys)
for _, k := range keys {
err = MountLowerUpperLayer(l.layers[k].FSType, l.layers[k].Lower, l.layers[k].Upper, l.layers[k].MountPoint, l.layers[k].MountOptions, forceRemount)
if err != nil {
return fmt.Errorf("MountLowerUpperLayer(%v,%v,%v,%v,%v,%v) has error: %v", l.layers[k].FSType, l.layers[k].Lower, l.layers[k].Upper, l.layers[k].MountPoint, l.layers[k].MountOptions, forceRemount, err)
}
}
return nil
}
func (l *LayersOverlayFS) GetCFGofLastLayer() LayerForMount {
l.RLock()
defer l.RUnlock()
var keys []int
for k, _ := range l.layers {
keys = append(keys, k)
}
sort.Ints(keys)
lastLayer := keys[len(keys)]
return l.layers[lastLayer]
}
/* unmount only last layer */
func (l *LayersOverlayFS) Umount() (err error) {
l.RLock()
defer l.RUnlock()
forceRemount := false
var keys []int
for k, _ := range l.layers {
keys = append(keys, k)
}
sort.Ints(keys)
lastLayer := keys[len(keys)]
if IsMounted(l.layers[lastLayer].MountPoint, l.layers[lastLayer].FSType) == true {
UnmountLayer(l.layers[lastLayer].FSType, l.layers[lastLayer].MountPoint, forceRemount)
}
return nil
}
func IsMounted (MountPoint,FSType string) bool{
return true
}
func UnmountLayer(FSType,MountPoint string,forceRemount bool) {
}
func MountLowerUpperLayer(fstype, lower, upper, mountpoint, mount_options string, forceremount bool) (err error) {
cmdLayerMount:="mount"
cmd := " -n -t " + " " + fstype + " -o " + mount_options + ",lowerdir=" + lower + "/,upperdir=" + upper + "/ " + fstype + " " + mountpoint + "/"
cmdObject := exec.Command(cmdLayerMount, strings.Fields(cmd)...)
_, err = cmdObject.Output()
if err != nil {
return err
}
return nil
}
func main() {
fmt.Println("Hello, playground")
}
我想实现所有类型的文件系统,但现在这是OverlayFS