Skip to content

Commit

Permalink
mount: allow bind-mounting a file to a file
Browse files Browse the repository at this point in the history
A user may want to bring /etc to a remote machine except
/etc/resolv.conf, since it may contains `nameserver 127.0.0.1`
if the user has used dnsmasq. In that case, the user can add the
following line to fstab:
/tmp/local/etc/resolv.conf /etc/resolv.conf none defaults,bind 0 0
to use the original dns config on the remote machine.

Signed-off-by: Changyuan Lyu <changyuanl@google.com>
  • Loading branch information
Lencerf committed Dec 22, 2022
1 parent 243d03a commit 3993009
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions mount/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"bufio"
"fmt"
"os"
"path"
"strings"

"github.com/hashicorp/go-multierror"
Expand Down Expand Up @@ -61,11 +62,32 @@ func mount(m mounter, fstab string) error {
dev, where, fstype, opts := f[0], f[1], f[2], f[3]
// The man page implies that the Linux kernel handles flags of "defaults"
// we do no further manipulation of opts.
if e := os.MkdirAll(where, 0666); e != nil && !os.IsExist(e) {
err = multierror.Append(err, e)
continue
}
flags, data := parse(opts)
if src, e := os.Stat(dev); e == nil && !src.IsDir() && flags&unix.MS_BIND != 0 {
// Source dev is a file and we are going to do a bind mount.
if target, e := os.Stat(where); e != nil {
// Destination does not exist, so we are going to create an empty file.
if e := os.MkdirAll(path.Dir(where), 0666); e != nil {
// Creation failed.
err = multierror.Append(err, fmt.Errorf("cannot create dir %s: %s", path.Dir(where), e))
continue
}
if err := os.WriteFile(where, []byte{}, 0666); err != nil {
// Creation failed.
err = multierror.Append(err, fmt.Errorf("cannot create target file %s: %s", where, e))
continue
}
} else if target.IsDir() {
// Destination exists, but it is a directory.
err = multierror.Append(err, fmt.Errorf("cannot bind file %s to a dir %s", dev, where))
continue
}
} else {
if e := os.MkdirAll(where, 0666); e != nil && !os.IsExist(e) {
err = multierror.Append(err, e)
continue
}
}
if e := m(dev, where, fstype, flags, data); e != nil {
err = multierror.Append(err, fmt.Errorf("Mount(%q, %q, %q, %q=>(%#x, %q)): %v", dev, where, fstype, opts, flags, data, e))
}
Expand Down

0 comments on commit 3993009

Please sign in to comment.