github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/pkg/loop/loop.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  	"github.com/u-root/u-root/pkg/mount"
     9  	"golang.org/x/sys/unix"
    10  )
    11  
    12  // Loop implements mount.Mount
    13  type Loop struct {
    14  	Dev     string
    15  	Source  string
    16  	Dir     string
    17  	FStype  string
    18  	Flags   uintptr
    19  	Data    string
    20  	Mounted bool
    21  }
    22  
    23  // New initializes a Loop struct and allocates a loodevice to it.
    24  func New(source, target, fstype string, flags uintptr, data string) (mount.Mounter, error) {
    25  	devicename, err := FindDevice()
    26  	if err != nil {
    27  		return nil, err
    28  	}
    29  	if err := SetFdFiles(devicename, source); err != nil {
    30  		return nil, err
    31  	}
    32  	l := &Loop{Dev: devicename, Dir: target, Source: source, FStype: fstype, Flags: flags, Data: data}
    33  	return l, nil
    34  }
    35  
    36  // Mount mounts the provided source file, with type fstype, and flags and data options
    37  // (which are usually 0 and ""), using any available loop device.
    38  func (l *Loop) Mount() error {
    39  	if err := unix.Mount(l.Dev, l.Dir, l.FStype, l.Flags, l.Data); err != nil {
    40  		return err
    41  	}
    42  	l.Mounted = true
    43  	return nil
    44  }