forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 2
/
losetup.go
70 lines (60 loc) · 1.32 KB
/
losetup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2012-2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Setup loop devices.
//
// Synopsis:
// losetup [-Ad] FILE
// losetup [-Ad] DEV FILE
//
// Options:
// -A: pick any device
// -d: detach the device
package main
import (
"flag"
"log"
"os"
"github.com/u-root/u-root/pkg/loop"
)
var (
anyLoop = flag.Bool("A", true, "Pick any device")
detach = flag.Bool("d", false, "Detach the device")
)
func main() {
var (
filename, devicename string
err error
)
flag.Parse()
args := flag.Args()
if *detach {
if len(args) == 1 {
if err := loop.ClearFdFile(args[0]); err != nil {
log.Fatal("Error clearing device: ", err)
}
log.Println("Detached", args[0])
os.Exit(0)
}
flag.Usage()
log.Fatal("Syntax Error")
}
if len(args) == 1 {
devicename, err = loop.FindDevice()
if err != nil {
log.Fatalf("can't find a loop: %v", err)
os.Exit(1)
}
filename = args[0]
} else if len(args) == 2 {
devicename = args[0]
filename = args[1]
} else {
flag.Usage()
log.Fatal("Syntax Error")
}
if err := loop.SetFdFiles(devicename, filename); err != nil {
log.Fatal("Could not set loop device:", err)
}
log.Printf("Attached %s to %s", devicename, filename)
}