-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.go
133 lines (108 loc) · 3.32 KB
/
system.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
package system
import (
"context"
"fmt"
"io/ioutil"
"os"
"strings"
"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 (
objectPath = "/org/sas/os/System"
ifaceName = "org.sas.os.System"
labelDataFileSystem = "sasos-data"
labelOverlayFileSystem = "sasos-overlay"
kernelCommandLine = "/mnt/boot/cmdline.txt"
tmpKernelCommandLine = "/mnt/boot/.tmp.cmdline.txt"
)
type system struct {
conn *dbus.Conn
}
func getAndCheckBusObjectFromLabel(udisks2helper udisks2.UDisks2Helper, label string) (dbus.BusObject, error) {
dataBusObject, err := udisks2helper.GetBusObjectFromLabel(label)
if err != nil {
return nil, dbus.MakeFailedError(err)
}
dataFilesystem := udisks2.NewFilesystem(dataBusObject)
dataMountPoints, err := dataFilesystem.GetMountPointsString(context.Background())
if err != nil {
return nil, dbus.MakeFailedError(err)
}
if len(dataMountPoints) > 0 {
return nil, dbus.MakeFailedError(fmt.Errorf("Device with label \"%s\" is mounted at %s, aborting.", label, dataMountPoints))
}
return dataBusObject, nil
}
func (d system) WipeDevice() (bool, *dbus.Error) {
logging.Info.Printf("Wipe device data.")
udisks2helper := udisks2.NewUDisks2(d.conn)
dataBusObject, err := getAndCheckBusObjectFromLabel(udisks2helper, labelDataFileSystem)
if err != nil {
return false, dbus.MakeFailedError(err)
}
overlayBusObject, err := getAndCheckBusObjectFromLabel(udisks2helper, labelOverlayFileSystem)
if err != nil {
return false, dbus.MakeFailedError(err)
}
err = udisks2helper.FormatPartition(dataBusObject, "ext4", labelDataFileSystem)
if err != nil {
return false, dbus.MakeFailedError(err)
}
err = udisks2helper.FormatPartition(overlayBusObject, "ext4", labelOverlayFileSystem)
if err != nil {
return false, dbus.MakeFailedError(err)
}
logging.Info.Printf("Successfully wiped device data.")
return true, nil
}
func (d system) ScheduleWipeDevice() (bool, *dbus.Error) {
data, err := ioutil.ReadFile(kernelCommandLine)
if err != nil {
fmt.Println(err)
return false, dbus.MakeFailedError(err)
}
datastr := strings.TrimSpace(string(data))
datastr += " saos.wipe=1"
err = ioutil.WriteFile(tmpKernelCommandLine, []byte(datastr), 0644)
if err != nil {
fmt.Println(err)
return false, dbus.MakeFailedError(err)
}
// Boot is mounted sync on SmartAutomatic OS, so just rename should be fine.
err = os.Rename(tmpKernelCommandLine, kernelCommandLine)
if err != nil {
fmt.Println(err)
return false, dbus.MakeFailedError(err)
}
logging.Info.Printf("Device will get wiped on next reboot!")
return true, nil
}
func InitializeDBus(conn *dbus.Conn) {
d := system{
conn: conn,
}
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),
},
},
}
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)
}