github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/pkg/loop/loop_linux.go (about)

     1  // Copyright 2018 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package loop
     6  
     7  import (
     8  	"syscall"
     9  
    10  	"golang.org/x/sys/unix"
    11  )
    12  
    13  const forceUnmount = syscall.MNT_FORCE | syscall.MNT_DETACH
    14  
    15  // Unmount unmounts and frees a loop. If it is mounted, it will try to unmount it.
    16  // If the unmount fails, we try to free it anyway, after trying a more
    17  // forceful unmount. We don't log errors, but we do return a concatentation
    18  // of whatever errors occur.
    19  func (l *Loop) Unmount(flags int) error {
    20  	if l.Mounted {
    21  		if err := unix.Unmount(l.Dir, flags); err != nil {
    22  			unix.Unmount(l.Dir, flags|forceUnmount)
    23  		}
    24  	}
    25  	return ClearFdFile(l.Dev)
    26  }