forked from filcloud/lvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloop.go
77 lines (66 loc) · 1.91 KB
/
loop.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
71
72
73
74
75
76
77
package lvm
import (
"io/ioutil"
"os"
losetup "gopkg.in/freddierice/go-losetup.v1"
)
// This file abstracts operations regarding LVM on loopback devices.
// See http://www.anthonyldechiaro.com/blog/2010/12/19/lvm-loopback-how-to/
// LoopDevice represents a loop device such as `/dev/loop0` backed by a file.
type LoopDevice struct {
lodev losetup.Device
backingFilePath string
}
// CreateLoopDevice returns a file-backed loop device. The caller is
// responsible for calling `Close()` on the `*LoopDevice` when done
// with it.
//
// CreateLoopDevice may panic if an error occurs during error recovery.
func CreateLoopDevice(size uint64) (device *LoopDevice, err error) {
var cleanup CleanupSteps
defer func() {
if err != nil {
cleanup.Unwind()
}
}()
// Create a tempfile to use as the target of our loop device.
file, err := ioutil.TempFile("", "test-dev")
if err != nil {
return nil, err
}
// If anything goes wrong, remove the tempfile.
cleanup.Add(func() error { return os.Remove(file.Name()) })
// Close the file as we're not going to manipulate its
// contents manually.
if err = file.Close(); err != nil {
return nil, err
}
if err = os.Truncate(file.Name(), int64(size)); err != nil {
return nil, err
}
// Attach a loop device
const (
offset = 0
ro = false
)
lodev, err := losetup.Attach(file.Name(), offset, ro)
if err != nil {
return nil, err
}
cleanup.Add(func() error { return lodev.Detach() })
// https://www.howtogeek.com/howto/40702/how-to-manage-and-use-lvm-logical-volume-management-in-ubuntu/
return &LoopDevice{lodev, file.Name()}, nil
}
func (d *LoopDevice) Path() string {
return d.lodev.Path()
}
func (d *LoopDevice) String() string {
return d.lodev.Path()
}
// Close detaches the loop device and removes the backing file.
func (d *LoopDevice) Close() error {
if err := d.lodev.Detach(); err != nil {
return err
}
return os.Remove(d.backingFilePath)
}