Skip to content

Commit

Permalink
Merge pull request #1712 from giuseppe/overlay-mount-erofs-RO
Browse files Browse the repository at this point in the history
overlay, composefs: mount loop device RO
  • Loading branch information
rhatdan authored Sep 14, 2023
2 parents 0c47206 + 2cfd72f commit 14f9d50
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
2 changes: 1 addition & 1 deletion drivers/overlay/composefs_supported.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func hasACL(path string) (bool, error) {

func mountComposefsBlob(dataDir, mountPoint string) error {
blobFile := getComposefsBlob(dataDir)
loop, err := loopback.AttachLoopDevice(blobFile)
loop, err := loopback.AttachLoopDeviceRO(blobFile)
if err != nil {
return err
}
Expand Down
18 changes: 17 additions & 1 deletion pkg/loopback/attach_loopback.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,16 @@ func openNextAvailableLoopback(index int, sparseName string, sparseFile *os.File
// AttachLoopDevice attaches the given sparse file to the next
// available loopback device. It returns an opened *os.File.
func AttachLoopDevice(sparseName string) (loop *os.File, err error) {
return attachLoopDevice(sparseName, false)
}

// AttachLoopDeviceRO attaches the given sparse file opened read-only to
// the next available loopback device. It returns an opened *os.File.
func AttachLoopDeviceRO(sparseName string) (loop *os.File, err error) {
return attachLoopDevice(sparseName, true)
}

func attachLoopDevice(sparseName string, readonly bool) (loop *os.File, err error) {
// Try to retrieve the next available loopback device via syscall.
// If it fails, we discard error and start looping for a
// loopback from index 0.
Expand All @@ -122,8 +132,14 @@ func AttachLoopDevice(sparseName string) (loop *os.File, err error) {
logrus.Debugf("Error retrieving the next available loopback: %s", err)
}

var sparseFile *os.File

// OpenFile adds O_CLOEXEC
sparseFile, err := os.OpenFile(sparseName, os.O_RDWR, 0o644)
if readonly {
sparseFile, err = os.OpenFile(sparseName, os.O_RDONLY, 0o644)
} else {
sparseFile, err = os.OpenFile(sparseName, os.O_RDWR, 0o644)
}
if err != nil {
logrus.Errorf("Opening sparse file: %v", err)
return nil, ErrAttachLoopbackDevice
Expand Down

0 comments on commit 14f9d50

Please sign in to comment.