-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add support for raw file as rootdisk
In addition to regular block devices raw files can now be used as rootdisk. Simple example: fallocate -l 1G disk.raw mkfs.ext4 disk.raw docker run --runtime runq --volume $PWD/disk.raw:/dev/runq/01/none/ext4 -e RUNQ_ROOTDISK=01 -ti alpine sh Signed-off-by: Peter Morjan <peter.morjan@de.ibm.com>
- Loading branch information
Showing
8 changed files
with
208 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package loopback | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/gotoz/runq/pkg/util" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
// Loopback defines a loopback device | ||
type Loopback struct { | ||
Name string | ||
device *os.File | ||
} | ||
|
||
// New finds the first unused loopback device | ||
func New() (*Loopback, error) { | ||
f, err := os.OpenFile("/dev/loop-control", os.O_RDWR, 0600) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
|
||
nr, _, e1 := unix.Syscall(unix.SYS_IOCTL, f.Fd(), unix.LOOP_CTL_GET_FREE, 0) | ||
if e1 != 0 { | ||
return nil, fmt.Errorf("Get next free loop device file: %v", e1.Error()) | ||
} | ||
name := fmt.Sprintf("/dev/loop%d", nr) | ||
|
||
if _, err := os.Stat(name); err != nil { | ||
if err := util.Mknod(name, "b", 0600, 7, int(nr)); err != nil { | ||
return nil, fmt.Errorf("create %q failed: %v", name, err) | ||
} | ||
} | ||
|
||
return &Loopback{ | ||
Name: name, | ||
}, nil | ||
} | ||
|
||
// Attach attaches a given raw file to an loop back device | ||
func (l *Loopback) Attach(file *os.File) error { | ||
f, err := os.OpenFile(l.Name, os.O_RDWR, 0600) | ||
if err != nil { | ||
return err | ||
} | ||
l.device = f | ||
if _, _, e1 := unix.Syscall(unix.SYS_IOCTL, f.Fd(), unix.LOOP_SET_FD, file.Fd()); e1 != 0 { | ||
return fmt.Errorf("Attach file to %q failed: %v", l.Name, e1.Error()) | ||
} | ||
return nil | ||
} | ||
|
||
// Detach detaches the device | ||
func (l *Loopback) Detach() error { | ||
defer l.device.Close() | ||
if _, _, e1 := unix.Syscall(unix.SYS_IOCTL, l.device.Fd(), unix.LOOP_CLR_FD, 0); e1 != 0 { | ||
return fmt.Errorf("Detach file from %q failed: %v", l.Name, e1.Error()) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.