-
Notifications
You must be signed in to change notification settings - Fork 906
/
main_activateifneeded.go
198 lines (164 loc) · 5.11 KB
/
main_activateifneeded.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
package main
import (
"context"
"database/sql"
"fmt"
"os"
sqlite3 "github.com/mattn/go-sqlite3"
"github.com/spf13/cobra"
"github.com/canonical/lxd/client"
"github.com/canonical/lxd/lxd/db"
"github.com/canonical/lxd/lxd/db/cluster"
"github.com/canonical/lxd/lxd/idmap"
"github.com/canonical/lxd/lxd/instance"
"github.com/canonical/lxd/lxd/instance/instancetype"
"github.com/canonical/lxd/lxd/node"
"github.com/canonical/lxd/shared"
"github.com/canonical/lxd/shared/logger"
)
func init() {
sql.Register("dqlite_direct_access", &sqlite3.SQLiteDriver{ConnectHook: sqliteDirectAccess})
}
type cmdActivateifneeded struct {
global *cmdGlobal
}
func (c *cmdActivateifneeded) Command() *cobra.Command {
cmd := &cobra.Command{}
cmd.Use = "activateifneeded"
cmd.Short = "Check if LXD should be started"
cmd.Long = `Description:
Check if LXD should be started
This command will check if LXD has any auto-started instances,
instances which were running prior to LXD's last shutdown or if it's
configured to listen on the network address.
If at least one of those is true, then a connection will be attempted to the
LXD socket which will cause a socket-activated LXD to be spawned.
`
cmd.RunE = c.Run
return cmd
}
func (c *cmdActivateifneeded) Run(cmd *cobra.Command, args []string) error {
// Only root should run this
if os.Geteuid() != 0 {
return fmt.Errorf("This must be run as root")
}
// Don't start a full daemon, we just need database access
d := defaultDaemon()
// Check if either the local database files exists.
path := d.os.LocalDatabasePath()
if !shared.PathExists(d.os.LocalDatabasePath()) {
logger.Debugf("No local database, so no need to start the daemon now")
return nil
}
// Open the database directly to avoid triggering any initialization
// code, in particular the data migration from node to cluster db.
sqldb, err := sql.Open("sqlite3", path)
if err != nil {
return err
}
d.db.Node = db.DirectAccess(sqldb)
// Load the configured address from the database
var localConfig *node.Config
err = d.db.Node.Transaction(context.TODO(), func(ctx context.Context, tx *db.NodeTx) error {
localConfig, err = node.ConfigLoad(ctx, tx)
return err
})
if err != nil {
return err
}
localHTTPAddress := localConfig.HTTPSAddress()
// Look for network socket
if localHTTPAddress != "" {
logger.Debugf("Daemon has core.https_address set, activating...")
_, err := lxd.ConnectLXDUnix("", nil)
return err
}
// Load the idmap for unprivileged instances
d.os.IdmapSet, err = idmap.DefaultIdmapSet("", "")
if err != nil {
return err
}
// Look for auto-started or previously started instances
path = d.os.GlobalDatabasePath()
if !shared.PathExists(path) {
logger.Debugf("No global database, so no need to start the daemon now")
return nil
}
sqldb, err = sql.Open("dqlite_direct_access", path+"?mode=ro")
if err != nil {
return err
}
defer func() { _ = sqldb.Close() }()
d.db.Cluster, err = db.ForLocalInspectionWithPreparedStmts(sqldb)
if err != nil {
return err
}
instances, err := instance.LoadNodeAll(d.State(), instancetype.Any)
if err != nil {
return err
}
for _, inst := range instances {
if instanceShouldAutoStart(inst) {
logger.Debugf("Daemon has auto-started instances, activating...")
_, err := lxd.ConnectLXDUnix("", nil)
return err
}
if inst.IsRunning() {
logger.Debugf("Daemon has running instances, activating...")
_, err := lxd.ConnectLXDUnix("", nil)
return err
}
// Check for scheduled instance snapshots
config := inst.ExpandedConfig()
if config["snapshots.schedule"] != "" {
logger.Debugf("Daemon has scheduled instance snapshots, activating...")
_, err := lxd.ConnectLXDUnix("", nil)
return err
}
}
// Check for scheduled volume snapshots
var volumes []db.StorageVolumeArgs
err = d.State().DB.Cluster.Transaction(context.TODO(), func(ctx context.Context, tx *db.ClusterTx) error {
volumes, err = tx.GetStoragePoolVolumesWithType(ctx, cluster.StoragePoolVolumeTypeCustom, false)
if err != nil {
return err
}
return nil
})
if err != nil {
return err
}
for _, vol := range volumes {
if vol.Config["snapshots.schedule"] != "" {
logger.Debugf("Daemon has scheduled volume snapshots, activating...")
_, err := lxd.ConnectLXDUnix("", nil)
return err
}
}
logger.Debugf("No need to start the daemon now")
return nil
}
// Configure the sqlite connection so that it's safe to access the
// dqlite-managed sqlite file, also without setting up raft.
func sqliteDirectAccess(conn *sqlite3.SQLiteConn) error {
// Ensure journal mode is set to WAL, as this is a requirement for
// replication.
_, err := conn.Exec("PRAGMA journal_mode=wal", nil)
if err != nil {
return err
}
// Ensure we don't truncate or checkpoint the WAL on exit, as this
// would bork replication which must be in full control of the WAL
// file.
_, err = conn.Exec("PRAGMA journal_size_limit=-1", nil)
if err != nil {
return err
}
// Ensure WAL autocheckpoint is disabled, since checkpoints are
// triggered explicitly by dqlite.
_, err = conn.Exec("PRAGMA wal_autocheckpoint=0", nil)
if err != nil {
return err
}
return nil
}