mount: make setupLoop() work with with Autoclear

setupLoop()'s Autoclear (LO_FLAGS_AUTOCLEAR) will destruct the
loopback device when all associated file descriptors are closed.

However this behavior didn't work before since setupLoop() was
returning a file name. The looppack device was destructed at
the end of the function when LoopParams had Autoclear = true.

Fixes #4969.

Signed-off-by: Kazuyoshi Kato <katokazu@amazon.com>
This commit is contained in:
Kazuyoshi Kato
2021-02-02 14:57:34 -08:00
parent ccde82da2b
commit 05a2e280ac
3 changed files with 39 additions and 22 deletions

View File

@@ -79,14 +79,16 @@ func (m *Mount) Mount(target string) (err error) {
// or remount with changed data
source := m.Source
if losetup {
devFile, err := setupLoop(m.Source, LoopParams{
loFile, err := setupLoop(m.Source, LoopParams{
Readonly: oflags&unix.MS_RDONLY == unix.MS_RDONLY,
Autoclear: true})
if err != nil {
return err
}
defer loFile.Close()
// Mount the loop device instead
source = devFile
source = loFile.Name()
}
if err := mountAt(chdir, source, target, m.Type, uintptr(oflags), data); err != nil {
return err