Skip to content

Instantly share code, notes, and snippets.

@dallasmarlow
Created October 17, 2014 12:27
Show Gist options
  • Save dallasmarlow/9e0edcba4650901ab3e3 to your computer and use it in GitHub Desktop.
Save dallasmarlow/9e0edcba4650901ab3e3 to your computer and use it in GitHub Desktop.
loop
if *loopId == 0 || *loopId > *loopMax-1 {
log.Fatalf("invalid loop device number: %d", *loopId)
}
loopDevNum := int((*loopId & 0xff) | ((7 & 0xfff) << 8))
loopDevPath := "/dev/loop" + strconv.Itoa(int(*loopId))
// ensure requested loop device is unique and read file
// permissions from existing loop device
devices, err := ioutil.ReadDir("/dev")
if err != nil {
log.Fatal("unable to list system devices")
}
// ensure `loopId` is unique and read the file permissions/ownership info
// of `-loopTemplate` for new device
var mode, uid, gid uint32
for _, dev := range devices {
switch dev.Name() {
case path.Base(loopDevPath):
log.Fatal("requested loop device path path must be unique:", loopDevPath)
case path.Base(*loopTemplate):
mode = dev.Mode()
uid = dev.Sys().(*syscall.Stat_t).Uid
gid = dev.Sys().(*syscall.Stat_t).Gid
}
// create loop device
if err := syscall.Mknod(loopDevPath, syscall.S_IFBLK|mode, loopDevNum)
// ensure loop device does not already exist
if _, err := os.Stat(loopDevPath); err == nil {
log.Fatal("loop device appears to already be present:", loopDevPath)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment