mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Add remount for bind mounts in ro
Docker-DCO-1.1-Signed-off-by: Michael Crosby <michael@crosbymichael.com> (github: crosbymichael)
This commit is contained in:
parent
bfc256f6c9
commit
f231539e99
3 changed files with 54 additions and 3 deletions
|
@ -716,7 +716,7 @@ func (container *Container) Start() (err error) {
|
|||
|
||||
for r, v := range container.Volumes {
|
||||
mountAs := "ro"
|
||||
if container.VolumesRW[v] {
|
||||
if container.VolumesRW[r] {
|
||||
mountAs = "rw"
|
||||
}
|
||||
|
||||
|
|
|
@ -48,7 +48,7 @@ func TestMounted(t *testing.T) {
|
|||
}
|
||||
f.Close()
|
||||
|
||||
if err := Mount(sourcePath, targetPath, "none", "bind,ro"); err != nil {
|
||||
if err := Mount(sourcePath, targetPath, "none", "bind,rw"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
|
@ -64,4 +64,47 @@ func TestMounted(t *testing.T) {
|
|||
if !mounted {
|
||||
t.Fatalf("Expected %s to be mounted", targetPath)
|
||||
}
|
||||
if _, err := os.Stat(targetPath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMountReadonly(t *testing.T) {
|
||||
tmp := path.Join(os.TempDir(), "mount-tests")
|
||||
if err := os.MkdirAll(tmp, 0777); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(tmp)
|
||||
|
||||
var (
|
||||
sourcePath = path.Join(tmp, "sourcefile.txt")
|
||||
targetPath = path.Join(tmp, "targetfile.txt")
|
||||
)
|
||||
|
||||
f, err := os.Create(sourcePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.WriteString("hello")
|
||||
f.Close()
|
||||
|
||||
f, err = os.Create(targetPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.Close()
|
||||
|
||||
if err := Mount(sourcePath, targetPath, "none", "bind,ro"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer func() {
|
||||
if err := Unmount(targetPath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}()
|
||||
|
||||
f, err = os.OpenFile(targetPath, os.O_RDWR, 0777)
|
||||
if err == nil {
|
||||
t.Fatal("Should not be able to open a ro file as rw")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,15 @@ import (
|
|||
)
|
||||
|
||||
func mount(device, target, mType string, flag uintptr, data string) error {
|
||||
return syscall.Mount(device, target, mType, flag, data)
|
||||
if err := syscall.Mount(device, target, mType, flag, data); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// If we have a bind mount or remount, remount...
|
||||
if flag&syscall.MS_BIND == syscall.MS_BIND && flag&syscall.MS_RDONLY == syscall.MS_RDONLY {
|
||||
return syscall.Mount(device, target, mType, flag|syscall.MS_REMOUNT, data)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func unmount(target string, flag int) error {
|
||||
|
|
Loading…
Reference in a new issue