forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdevice.go
80 lines (66 loc) · 1.98 KB
/
device.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
// Copyright 2017-2018 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.
package diskboot
import (
"fmt"
"io/ioutil"
"path/filepath"
"github.com/u-root/u-root/pkg/mount"
"github.com/u-root/u-root/pkg/storage"
"golang.org/x/sys/unix"
)
// Device contains the path to a block filesystem along with its type
type Device struct {
DevPath string
MountPath string
Fstype string
Configs []*Config
}
// fstypes returns all block file system supported by the linuxboot kernel
// FindDevices searches for devices with bootable configs
func FindDevices(devicesGlob string) (devices []*Device) {
fstypes, err := storage.GetSupportedFilesystems()
if err != nil {
return nil
}
sysList, err := filepath.Glob(devicesGlob)
if err != nil {
return nil
}
// The Linux /sys file system is a bit, er, awkward. You can't find
// the device special in there; just everything else.
for _, sys := range sysList {
blk := filepath.Join("/dev", filepath.Base(sys))
dev, _ := mountDevice(blk, fstypes)
if dev != nil && len(dev.Configs) > 0 {
devices = append(devices, dev)
}
}
return devices
}
// FindDevice attempts to construct a boot device at the given path
func FindDevice(devPath string) (*Device, error) {
fstypes, err := storage.GetSupportedFilesystems()
if err != nil {
return nil, nil
}
return mountDevice(devPath, fstypes)
}
func mountDevice(devPath string, fstypes []string) (*Device, error) {
mountPath, err := ioutil.TempDir("/tmp", "boot-")
if err != nil {
return nil, fmt.Errorf("Failed to create tmp mount directory: %v", err)
}
for _, fstype := range fstypes {
if err := mount.Mount(devPath, mountPath, fstype, "", unix.MS_RDONLY); err != nil {
continue
}
configs := FindConfigs(mountPath)
if len(configs) == 0 {
continue
}
return &Device{devPath, mountPath, fstype, configs}, nil
}
return nil, fmt.Errorf("Failed to find a valid boot device with configs")
}