-
Notifications
You must be signed in to change notification settings - Fork 0
/
datadisk.go
146 lines (121 loc) · 3.29 KB
/
datadisk.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
package datadisk
import (
"errors"
"fmt"
"os"
"github.com/fntlnz/mountinfo"
"github.com/godbus/dbus/v5"
"github.com/godbus/dbus/v5/introspect"
"github.com/godbus/dbus/v5/prop"
"github.com/lavstudia/os-agent/udisks2"
logging "github.com/lavstudia/os-agent/utils/log"
)
const (
dataMount = "/mnt/data"
linuxDataPartitionUUID = "0FC63DAF-8483-4772-8E79-3D69D8477DE4"
)
func GetDataMount() (*mountinfo.Mountinfo, error) {
minfo, err := mountinfo.GetMountInfo("/proc/self/mountinfo")
if err != nil {
logging.Warning.Print(err)
return nil, err
}
for _, info := range minfo {
if dataMount == info.MountPoint {
return &info, nil
}
}
return nil, errors.New("Can't find a data mount!")
}
type datadisk struct {
conn *dbus.Conn
props *prop.Properties
}
func (d datadisk) ChangeDevice(newDevice string) (bool, *dbus.Error) {
logging.Info.Printf("Request to change data disk to %s.", newDevice)
udisks2helper := udisks2.NewUDisks2(d.conn)
dataDevice, err := udisks2helper.GetRootDeviceFromLabel("sasos-data")
if err != nil {
return false, dbus.MakeFailedError(err)
}
logging.Info.Printf("Data partition is currently on device %s.", *dataDevice)
if *dataDevice == newDevice {
return false, dbus.MakeFailedError(fmt.Errorf("Current data device \"%s\" the same as target device. Aborting.", *dataDevice))
}
err = udisks2helper.PartitionDeviceWithSinglePartition(newDevice, linuxDataPartitionUUID, "sasos-data-external")
if err != nil {
return false, dbus.MakeFailedError(err)
}
/* Move request marker for sasos-data.service */
fileName := "/mnt/overlay/move-data"
_, err = os.Stat(fileName)
if os.IsNotExist(err) {
file, err := os.Create(fileName)
if err != nil {
return false, dbus.MakeFailedError(err)
}
defer file.Close()
}
return true, nil
}
func (d datadisk) ReloadDevice() (bool, *dbus.Error) {
mountInfo, err := GetDataMount()
if err != nil {
return false, dbus.MakeFailedError(err)
}
d.props.SetMust(ifaceName, "CurrentDevice", mountInfo.MountSource)
return true, nil
}
const (
objectPath = "/org/sas/os/DataDisk"
ifaceName = "org.sas.os.DataDisk"
)
func InitializeDBus(conn *dbus.Conn) {
// Try to read the current data mount point
mountInfo, err := GetDataMount()
var currentDisk string = ""
if err != nil {
logging.Warning.Print(err)
} else {
currentDisk = mountInfo.MountSource
}
d := datadisk{
conn: conn,
}
propsSpec := map[string]map[string]*prop.Prop{
ifaceName: {
"CurrentDevice": {
Value: ¤tDisk,
Writable: false,
Emit: prop.EmitTrue,
Callback: nil,
},
},
}
props, err := prop.Export(conn, objectPath, propsSpec)
if err != nil {
logging.Critical.Panic(err)
}
d.props = props
err = conn.Export(d, objectPath, ifaceName)
if err != nil {
logging.Critical.Panic(err)
}
node := &introspect.Node{
Name: objectPath,
Interfaces: []introspect.Interface{
introspect.IntrospectData,
prop.IntrospectData,
{
Name: ifaceName,
Methods: introspect.Methods(d),
Properties: props.Introspection(ifaceName),
},
},
}
err = conn.Export(introspect.NewIntrospectable(node), objectPath, "org.freedesktop.DBus.Introspectable")
if err != nil {
logging.Critical.Panic(err)
}
logging.Info.Printf("Exposing object %s with interface %s ...", objectPath, ifaceName)
}