forked from purpleidea/mgmt
/
mount.go
732 lines (648 loc) · 21.4 KB
/
mount.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
// Mgmt
// Copyright (C) 2013-2018+ James Shubin and the project contributors
// Written by James Shubin <james@shubin.ca> and the project contributors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package resources
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"unsafe"
"github.com/purpleidea/mgmt/engine"
"github.com/purpleidea/mgmt/engine/traits"
engineUtil "github.com/purpleidea/mgmt/engine/util"
"github.com/purpleidea/mgmt/recwatch"
"github.com/purpleidea/mgmt/util"
sdbus "github.com/coreos/go-systemd/dbus"
"github.com/coreos/go-systemd/unit"
systemdUtil "github.com/coreos/go-systemd/util"
fstab "github.com/deniswernert/go-fstab"
"github.com/godbus/dbus"
errwrap "github.com/pkg/errors"
"golang.org/x/sys/unix"
)
func init() {
engine.RegisterResource("mount", func() engine.Res { return &MountRes{} })
}
const (
// procFilesystems is a file that lists all the valid filesystem types.
procFilesystems = "/proc/filesystems"
// procPath is the path to /proc/mounts which contains all active mounts.
procPath = "/proc/mounts"
// fstabPath is the path to the fstab file which defines mounts.
fstabPath = "/etc/fstab"
// fstabUmask is the umask (permissions) used to edit /etc/fstab.
fstabUmask = 0644
// getStatus64 is an ioctl command to get the status of file backed
// loopback devices (i.e. iso file mounts.)
getStatus64 = 0x4C05
// loopFileUmask is the umask (permissions) used to read the loop file.
loopFileUmask = 0660
// devDisk is the path where disks and partitions can be found, organized
// by uuid/label/path.
devDisk = "/dev/disk/"
// diskByUUID is the location of symlinks for devices by UUID.
diskByUUID = devDisk + "by-uuid/"
// diskByLabel is the location of symlinks for devices by label.
diskByLabel = devDisk + "by-label/"
// diskByUUID is the location of symlinks for partitions by UUID.
diskByPartUUID = devDisk + "by-partuuid/"
// diskByLabel is the location of symlinks for partitions by label.
diskByPartLabel = devDisk + "by-partlabel/"
// dbusSystemd1Interface is the base systemd1 path.
dbusSystemd1Path = "/org/freedesktop/systemd1"
// dbusUnitPath is the dbus path where mount unit files are found.
dbusUnitPath = dbusSystemd1Path + "/unit/"
// dbusSystemd1Interface is the base systemd1 interface.
dbusSystemd1Interface = "org.freedesktop.systemd1"
// dbusMountInterface is used as an argument to filter dbus messages.
dbusMountInterface = dbusSystemd1Interface + ".Mount"
// dbusManagerInterface is the systemd manager interface used for
// interfacing with systemd units.
dbusManagerInterface = dbusSystemd1Interface + ".Manager"
// dbusRestartUnit is the dbus method for restarting systemd units.
dbusRestartUnit = dbusManagerInterface + ".RestartUnit"
// restartTimeout is the delay before restartUnit is assumed to have
// failed.
dbusRestartCtxTimeout = 10
// dbusSignalJobRemoved is the name of the dbus signal that produces a
// message when a dbus job is done (or has errored.)
dbusSignalJobRemoved = "JobRemoved"
)
// MountRes is a systemd mount resource that adds/removes entries from
// /etc/fstab, and makes sure the defined device is mounted or unmounted
// accordingly. The mount point is set according to the resource's name.
type MountRes struct {
traits.Base
init *engine.Init
// State must be exists ot absent. If absent, remaining fields are ignored.
State string `yaml:"state"`
Device string `yaml:"device"` // location of the device or image
Type string `yaml:"type"` // the type of filesystem
Options map[string]string `yaml:"options"` // mount options
Freq int `yaml:"freq"` // dump frequency
PassNo int `yaml:"passno"` // verification order
mount *fstab.Mount // struct representing the mount
}
// Default returns some sensible defaults for this resource.
func (obj *MountRes) Default() engine.Res {
return &MountRes{
Options: defaultMntOps(),
}
}
// Validate if the params passed in are valid data.
func (obj *MountRes) Validate() error {
var err error
// validate state
if obj.State != "exists" && obj.State != "absent" {
return fmt.Errorf("state must be 'exists', or 'absent'")
}
// validate type
fs, err := ioutil.ReadFile(procFilesystems)
if err != nil {
return errwrap.Wrapf(err, "error reading %s", procFilesystems)
}
fsSlice := strings.Fields(string(fs))
for i, x := range fsSlice {
if x == "nodev" {
fsSlice = append(fsSlice[:i], fsSlice[i+1:]...)
}
}
if obj.State != "absent" && !util.StrInList(obj.Type, fsSlice) {
return fmt.Errorf("type must be a valid filesystem type (see /proc/filesystems)")
}
// validate mountpoint
if strings.Contains(obj.Name(), "//") {
return fmt.Errorf("double slashes are not allowed in resource name")
}
if err := unix.Access(obj.Name(), unix.R_OK); err != nil {
return errwrap.Wrapf(err, "error validating mount point: %s", obj.Name())
}
// validate device
device, err := evalSpec(obj.Device) // eval symlink
if err != nil {
return errwrap.Wrapf(err, "error evaluating spec: %s", obj.Device)
}
if err := unix.Access(device, unix.R_OK); err != nil {
return errwrap.Wrapf(err, "error validating device: %s", device)
}
return nil
}
// Init runs some startup code for this resource.
func (obj *MountRes) Init(init *engine.Init) error {
obj.init = init //save for later
obj.mount = &fstab.Mount{
Spec: obj.Device,
File: obj.Name(),
VfsType: obj.Type,
MntOps: obj.Options,
Freq: obj.Freq,
PassNo: obj.PassNo,
}
return nil
}
// Close is run by the engine to clean up after the resource is done.
func (obj *MountRes) Close() error {
return nil
}
// Watch listens for signals from the mount unit associated with the resource.
// It also watch for changes to /etc/fstab, where mounts are defined.
func (obj *MountRes) Watch() error {
// make sure systemd is running
if !systemdUtil.IsRunningSystemd() {
return fmt.Errorf("systemd is not running")
}
// establish a godbus connection
conn, err := util.SystemBusPrivateUsable()
if err != nil {
return errwrap.Wrapf(err, "error establishing dbus connection")
}
defer conn.Close()
// add a dbus rule to watch signals from the mount unit.
args := fmt.Sprintf("type='signal', path='%s', arg0='%s'",
dbusUnitPath+sdbus.PathBusEscape(unit.UnitNamePathEscape((obj.Name()+".mount"))),
dbusMountInterface,
)
if call := conn.BusObject().Call(engineUtil.DBusAddMatch, 0, args); call.Err != nil {
return errwrap.Wrapf(call.Err, "error creating dbus call")
}
defer conn.BusObject().Call(engineUtil.DBusRemoveMatch, 0, args) // ignore the error
ch := make(chan *dbus.Signal)
defer close(ch)
conn.Signal(ch)
defer conn.RemoveSignal(ch)
// watch the fstab file
recWatcher, err := recwatch.NewRecWatcher(fstabPath, false)
if err != nil {
return err
}
// close the recwatcher when we're done
defer recWatcher.Close()
// notify engine that we're running
if err := obj.init.Running(); err != nil {
return err // bubble up a NACK...
}
var send bool
var done bool
for {
select {
case event, ok := <-recWatcher.Events():
if !ok {
if done {
return nil
}
done = true
continue
}
if err := event.Error; err != nil {
return errwrap.Wrapf(err, "unknown recwatcher error")
}
if obj.init.Debug {
obj.init.Logf("event(%s): %v", event.Body.Name, event.Body.Op)
}
obj.init.Dirty()
send = true
case event, ok := <-ch:
if !ok {
if done {
return nil
}
done = true
continue
}
if obj.init.Debug {
obj.init.Logf("event: %+v", event)
}
obj.init.Dirty()
send = true
case event, ok := <-obj.init.Events:
if !ok {
return nil
}
if err := obj.init.Read(event); err != nil {
return err
}
}
// do all our event sending all together to avoid duplicate msgs
if send {
send = false
if err := obj.init.Event(); err != nil {
return err // exit if requested
}
}
}
}
// fstabCheckApply checks /etc/fstab for entries corresponding to the resource
// definition, and adds or deletes the entry as needed.
func (obj *MountRes) fstabCheckApply(apply bool) (checkOK bool, err error) {
exists, err := fstabEntryExists(fstabPath, obj.mount)
if err != nil {
return false, errwrap.Wrapf(err, "error checking if fstab entry exists")
}
// if everything is as it should be, we're done
if (exists && obj.State == "exists") || (!exists && obj.State == "absent") {
return true, nil
}
if !apply {
return false, nil
}
obj.init.Logf("fstabCheckApply(%t)", apply)
if obj.State == "exists" {
if err := obj.fstabEntryAdd(fstabPath, obj.mount); err != nil {
return false, errwrap.Wrapf(err, "error adding fstab entry: %+v", obj.mount)
}
return false, nil
}
if err := obj.fstabEntryRemove(fstabPath, obj.mount); err != nil {
return false, errwrap.Wrapf(err, "error removing fstab entry: %+v", obj.mount)
}
return false, nil
}
// mountCheckApply checks if the defined resource is mounted, and mounts or
// unmounts it according to the defined state.
func (obj *MountRes) mountCheckApply(apply bool) (bool, error) {
exists, err := mountExists(procPath, obj.mount)
if err != nil {
return false, errwrap.Wrapf(err, "error checking if mount exists")
}
// if everything is as it should be, we're done
if (exists && obj.State == "exists") || (!exists && obj.State == "absent") {
return true, nil
}
if !apply {
return false, nil
}
obj.init.Logf("mountCheckApply(%t)", apply)
if obj.State == "exists" {
// Reload mounts from /etc/fstab by performing a `daemon-reload` and
// restarting `local-fs.target` and `remote-fs.target` units.
if err := mountReload(); err != nil {
return false, errwrap.Wrapf(err, "error reloading /etc/fstab")
}
return false, nil // we're done
}
// unmount the device
if err := unix.Unmount(obj.Name(), 0); err != nil { // 0 means no flags
return false, errwrap.Wrapf(err, "error unmounting %s", obj.Name())
}
return false, nil
}
// CheckApply is run to check the state and, if apply is true, to apply the
// necessary changes to reach the desired state. This is run before Watch and
// again if Watch finds a change occurring to the state.
func (obj *MountRes) CheckApply(apply bool) (checkOK bool, err error) {
checkOK = true
if c, err := obj.fstabCheckApply(apply); err != nil {
return false, err
} else if !c {
checkOK = false
}
if c, err := obj.mountCheckApply(apply); err != nil {
return false, err
} else if !c {
checkOK = false
}
return checkOK, nil
}
// Cmp compares two resources and return if they are equivalent.
func (obj *MountRes) Cmp(r engine.Res) error {
// we can only compare MountRes to others of the same resource kind
res, ok := r.(*MountRes)
if !ok {
return fmt.Errorf("not a %s", obj.Kind())
}
if obj.State != res.State {
return fmt.Errorf("the State differs")
}
if obj.Type != res.Type {
return fmt.Errorf("the Type differs")
}
if !strMapEq(obj.Options, res.Options) {
return fmt.Errorf("the Options differ")
}
if obj.Freq != res.Freq {
return fmt.Errorf("the Type differs")
}
if obj.PassNo != res.PassNo {
return fmt.Errorf("the PassNo differs")
}
return nil
}
// MountUID is a unique resource identifier.
type MountUID struct {
engine.BaseUID
name string
}
// IFF aka if and only if they are equivalent, return true. If not, false.
func (obj *MountUID) IFF(uid engine.ResUID) bool {
res, ok := uid.(*MountUID)
if !ok {
return false
}
return obj.name == res.name
}
// UIDs includes all params to make a unique identification of this object.
// Most resources only return one although some resources can return multiple.
func (obj *MountRes) UIDs() []engine.ResUID {
x := &MountUID{
BaseUID: engine.BaseUID{Name: obj.Name(), Kind: obj.Kind()},
name: obj.Name(),
}
return []engine.ResUID{x}
}
// UnmarshalYAML is the custom unmarshal handler for this struct.
// It is primarily useful for setting the defaults.
func (obj *MountRes) UnmarshalYAML(unmarshal func(interface{}) error) error {
type rawRes MountRes // indirection to avoid infinite recursion
def := obj.Default() // get the default
res, ok := def.(*MountRes) // put in the right format
if !ok {
return fmt.Errorf("could not convert to MountRes")
}
raw := rawRes(*res) // convert; the defaults go here
if err := unmarshal(&raw); err != nil {
return err
}
*obj = MountRes(raw) // restore from indirection with type conversion!
return nil
}
// defaultMntOps returns a map that sets the default mount options for fstab
// mounts.
func defaultMntOps() map[string]string {
return map[string]string{"defaults": ""}
}
// strMapEq returns true, if and only if the two provided maps are identical.
func strMapEq(x, y map[string]string) bool {
if len(x) != len(y) {
return false
}
for k, v := range x {
if val, ok := x[k]; !ok || v != val {
return false
}
}
return true
}
// fstabEntryExists checks whether or not a given mount exists in the provided
// fstab file.
func fstabEntryExists(file string, mount *fstab.Mount) (bool, error) {
mounts, err := fstab.ParseFile(file)
if err != nil {
return false, errwrap.Wrapf(err, "error parsing file: %s", file)
}
for _, m := range mounts {
if m.Equals(mount) {
return true, nil
}
}
return false, nil
}
// fstabEntryAdd adds the given mount to the provided fstab file.
func (obj *MountRes) fstabEntryAdd(file string, mount *fstab.Mount) error {
mounts, err := fstab.ParseFile(file)
if err != nil {
return errwrap.Wrapf(err, "error parsing file: %s", file)
}
for _, m := range mounts {
// if the entry exists, we're done
if m.Equals(mount) {
return nil
}
}
// mount does not exist so we need to add it
mounts = append(mounts, mount)
return obj.fstabWrite(file, mounts)
}
// fstabEntryRemove removes the given mount from the provided fstab file.
func (obj *MountRes) fstabEntryRemove(file string, mount *fstab.Mount) error {
mounts, err := fstab.ParseFile(file)
if err != nil {
return errwrap.Wrapf(err, "error parsing file: %s", file)
}
for i, m := range mounts {
// remove any entry with the defined mountpoint
if m.File == mount.File {
mounts = append(mounts[:i], mounts[i+1:]...)
}
}
return obj.fstabWrite(file, mounts)
}
// fstabWrite generates an fstab file with the given mounts, and writes them
// to the provided fstab file.
func (obj *MountRes) fstabWrite(file string, mounts fstab.Mounts) error {
// build the file contents
contents := fmt.Sprintf("# Generated by %s at %d", obj.init.Program, time.Now().UnixNano()) + "\n"
contents = contents + mounts.String() + "\n"
// write the file
if err := ioutil.WriteFile(file, []byte(contents), fstabUmask); err != nil {
return errwrap.Wrapf(err, "error writing fstab file: %s", file)
}
return nil
}
// mountExists returns true, if a given mount exists in the given file
// (typically /proc/mounts.)
func mountExists(file string, mount *fstab.Mount) (bool, error) {
var err error
m := *mount // make a copy so we don't change the definition
// resolve the device's symlink if there is one
if m.Spec, err = evalSpec(mount.Spec); err != nil {
return false, errwrap.Wrapf(err, "error evaluating spec: %s", mount.Spec)
}
// get all mounts
mounts, err := fstab.ParseFile(file)
if err != nil {
return false, errwrap.Wrapf(err, "error parsing file: %s", file)
}
// check for the defined mount
for _, p := range mounts {
found, err := mountCompare(&m, p)
if err != nil {
return false, errwrap.Wrapf(err, "mounts could not be compared: %s and %s", mount.String(), p.String())
}
if found {
return true, nil
}
}
return false, nil
}
// mountCompare compares two mounts. It is assumed that the first comes from
// a resource definition, and the second comes from /proc/mounts. It compares
// the two after resolving the loopback device's file path (if necessary,) and
// ignores freq and passno, as they may differ between the definition and
// /proc/mounts.
func mountCompare(def, proc *fstab.Mount) (bool, error) {
if def.Equals(proc) {
return true, nil
}
if def.File != proc.File {
return false, nil
}
if def.Spec != "" {
procSpec, err := loopFilePath(proc.Spec)
if err != nil {
return false, err
}
if def.Spec != procSpec {
return false, nil
}
}
if !strMapEq(def.MntOps, defaultMntOps()) && !strMapEq(def.MntOps, proc.MntOps) {
return false, nil
}
if def.VfsType != "" && def.VfsType != proc.VfsType {
return false, nil
}
return true, nil
}
// mountReload performs a daemon-reload and restarts fs-local.target and
// fs-remote.target, to let systemd mount any new entries in /etc/fstab.
func mountReload() error {
// establish a godbus connection
conn, err := util.SystemBusPrivateUsable()
if err != nil {
return errwrap.Wrapf(err, "error establishing dbus connection")
}
defer conn.Close()
// systemctl daemon-reload
conn.BusObject().Call("Reload", 0)
// systemctl restart local-fs.target
if err := restartUnit(conn, "local-fs.target"); err != nil {
return errwrap.Wrapf(err, "error restarting unit")
}
// systemctl restart remote-fs.target
if err := restartUnit(conn, "local-fs.target"); err != nil {
return errwrap.Wrapf(err, "error restarting unit")
}
return nil
}
// restartUnit restarts the given dbus unit and waits for it to finish
// starting up. If restartTimeout is exceeded, it will return an error.
func restartUnit(conn *dbus.Conn, unit string) error {
// timeout if we don't get the JobRemoved event
ctx, cancel := context.WithTimeout(context.TODO(), dbusRestartCtxTimeout*time.Second)
defer cancel()
// Add a dbus rule to watch the systemd1 JobRemoved signal used to wait
// until the restart job completes.
args := fmt.Sprintf("type='signal', path='%s', interface='%s', member='%s', arg2='%s'",
dbusSystemd1Path,
dbusManagerInterface,
dbusSignalJobRemoved,
unit,
)
if call := conn.BusObject().Call(engineUtil.DBusAddMatch, 0, args); call.Err != nil {
return errwrap.Wrapf(call.Err, "error creating dbus call")
}
defer conn.BusObject().Call(engineUtil.DBusRemoveMatch, 0, args) // ignore the error
// channel for godbus connection
ch := make(chan *dbus.Signal)
defer close(ch)
conn.Signal(ch)
defer conn.RemoveSignal(ch)
// restart the unit
sd1 := conn.Object(dbusSystemd1Interface, dbus.ObjectPath(dbusSystemd1Path))
if call := sd1.Call(dbusRestartUnit, 0, unit, "fail"); call.Err != nil {
return errwrap.Wrapf(call.Err, "error restarting unit: %s", unit)
}
// wait for the job to be removed, indicating completion
select {
case event, ok := <-ch:
if !ok {
return fmt.Errorf("channel closed unexpectedly")
}
if event.Body[3] != "done" {
return fmt.Errorf("unexpected job status: %s", event.Body[3])
}
case <-ctx.Done():
return fmt.Errorf("restarting %s failed due to context timeout", unit)
}
return nil
}
// evalSpec resolves the device from the supplied spec, i.e. it follows the
// symlink, if any, from the provided uuid, label, or path.
func evalSpec(spec string) (string, error) {
var path string
m := &fstab.Mount{}
m.Spec = spec
switch m.SpecType() {
case fstab.UUID:
path = diskByUUID + m.SpecValue()
case fstab.Label:
path = diskByLabel + m.SpecValue()
case fstab.PartUUID:
path = diskByPartUUID + m.SpecValue()
case fstab.PartLabel:
path = diskByPartLabel + m.SpecValue()
case fstab.Path:
path = m.SpecValue()
default:
return "", fmt.Errorf("unexpected spec type: %v", m.SpecType())
}
return filepath.EvalSymlinks(path)
}
// loopFilePath returns the file path of the mounted filesystem image, backing
// the given loopback device.
func loopFilePath(spec string) (string, error) {
// if it's not a loopback device, return the input
if !strings.Contains(spec, "/dev/loop") {
return spec, nil
}
info, err := getLoopInfo(spec)
if err != nil {
return "", errwrap.Wrapf(err, "error getting loop info")
}
// trim the extra null chars off the end of the filename
return string(bytes.Trim(info.FileName[:], "\x00")), nil
}
// loopInfo is a datastructure that holds relevant information about a file
// backed loopback device. Code is based on freddierice/go-losetup.
type loopInfo struct {
Device uint64
INode uint64
RDevice uint64
Offset uint64
SizeLimit uint64
Number uint32
EncryptType uint32
EncryptKeySize uint32
Flags uint32
FileName [64]byte
CryptName [64]byte
EncryptKey [32]byte
Init [2]uint64
}
// getLoopInfo returns a loopInfo struct containing information about the
// provided file backed loopback device.
func getLoopInfo(loop string) (*loopInfo, error) {
// open the loop file
f, err := os.OpenFile(loop, 0, loopFileUmask)
if err != nil {
return nil, fmt.Errorf("error opening %s: %s", loop, err)
}
defer f.Close()
// deserialize the contents
retInfo := &loopInfo{}
_, _, errno := unix.Syscall(unix.SYS_IOCTL, f.Fd(), getStatus64, uintptr(unsafe.Pointer(retInfo)))
if errno == unix.ENXIO {
return nil, fmt.Errorf("device not backed by a file")
} else if errno != 0 {
return nil, fmt.Errorf("error getting info about %s (errno: %d)", loop, errno)
}
return retInfo, nil
}