forked from daos-stack/daos
/
superblock.go
235 lines (197 loc) · 6.1 KB
/
superblock.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
//
// (C) Copyright 2019-2020 Intel Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// GOVERNMENT LICENSE RIGHTS-OPEN SOURCE SOFTWARE
// The Government's rights to use, modify, reproduce, release, perform, display,
// or disclose this software are subject to the terms of the Apache License as
// provided in Contract No. 8F-30005.
// Any reproduction of computer software, computer software documentation, or
// portions thereof marked with this legend must also reproduce the markings.
//
package server
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"syscall"
"github.com/dustin/go-humanize"
uuid "github.com/google/uuid"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"github.com/daos-stack/daos/src/control/common"
"github.com/daos-stack/daos/src/control/server/ioserver"
)
const (
defaultStoragePath = "/mnt/daos"
defaultGroupName = "daos_io_server"
superblockVersion = 0
)
// Superblock is the per-Instance superblock
type Superblock struct {
Version uint8
UUID string
System string
Rank *ioserver.Rank
ValidRank bool
MS bool
CreateMS bool
BootstrapMS bool
}
// TODO: Marshal/Unmarshal using a binary representation?
// Marshal transforms the Superblock into a storable representation
func (sb *Superblock) Marshal() ([]byte, error) {
data, err := yaml.Marshal(sb)
if err != nil {
return nil, errors.Wrapf(err, "Failed to marshal %+v", sb)
}
return data, nil
}
// Unmarshal reconstitutes a Superblock from a Marshaled representation
func (sb *Superblock) Unmarshal(raw []byte) error {
return yaml.Unmarshal(raw, sb)
}
func (srv *IOServerInstance) superblockPath() string {
scmConfig := srv.scmConfig()
storagePath := scmConfig.MountPoint
if storagePath == "" {
storagePath = defaultStoragePath
}
return filepath.Join(srv.fsRoot, storagePath, "superblock")
}
func (srv *IOServerInstance) setSuperblock(sb *Superblock) {
srv.Lock()
defer srv.Unlock()
srv._superblock = sb
}
func (srv *IOServerInstance) getSuperblock() *Superblock {
srv.RLock()
defer srv.RUnlock()
return srv._superblock
}
func (srv *IOServerInstance) hasSuperblock() bool {
return srv.getSuperblock() != nil
}
// NeedsSuperblock indicates whether or not the instance appears
// to need a superblock to be created in order to start.
func (srv *IOServerInstance) NeedsSuperblock() (bool, error) {
if srv.hasSuperblock() {
return false, nil
}
scmCfg := srv.scmConfig()
srv.log.Debugf("%s: checking superblock", scmCfg.MountPoint)
err := srv.ReadSuperblock()
if os.IsNotExist(errors.Cause(err)) {
srv.log.Debugf("%s: needs superblock (doesn't exist)", scmCfg.MountPoint)
return true, nil
}
if err != nil {
return true, errors.Wrap(err, "failed to read existing superblock")
}
return false, nil
}
// CreateSuperblock creates the superblock for this instance.
func (srv *IOServerInstance) CreateSuperblock(msInfo *mgmtInfo) error {
if err := srv.MountScmDevice(); err != nil {
return err
}
u, err := uuid.NewRandom()
if err != nil {
return errors.Wrap(err, "Failed to generate instance UUID")
}
cfg := srv.runner.GetConfig()
systemName := cfg.SystemName
if systemName == "" {
systemName = defaultGroupName
}
superblock := &Superblock{
Version: superblockVersion,
UUID: u.String(),
System: systemName,
ValidRank: msInfo.isReplica && msInfo.shouldBootstrap,
MS: msInfo.isReplica,
CreateMS: msInfo.isReplica,
BootstrapMS: msInfo.shouldBootstrap,
}
if cfg.Rank != nil || msInfo.isReplica && msInfo.shouldBootstrap {
superblock.Rank = new(ioserver.Rank)
if cfg.Rank != nil {
*superblock.Rank = *cfg.Rank
}
}
srv.setSuperblock(superblock)
srv.log.Debugf("creating %s: (rank: %s, uuid: %s)",
srv.superblockPath(), superblock.Rank, superblock.UUID)
return srv.WriteSuperblock()
}
func (srv *IOServerInstance) logScmStorage() error {
scmMount := path.Dir(srv.superblockPath())
stBuf := new(syscall.Statfs_t)
if err := syscall.Statfs(scmMount, stBuf); err != nil {
return err
}
frSize := uint64(stBuf.Frsize)
totalBytes := frSize * stBuf.Blocks
availBytes := frSize * stBuf.Bavail
srv.log.Infof("SCM @ %s: %s Total/%s Avail", scmMount,
humanize.Bytes(totalBytes), humanize.Bytes(availBytes))
return nil
}
// WriteSuperblock writes the instance's superblock
// to storage.
func (srv *IOServerInstance) WriteSuperblock() error {
return WriteSuperblock(srv.superblockPath(), srv.getSuperblock())
}
// ReadSuperblock reads the instance's superblock
// from storage.
func (srv *IOServerInstance) ReadSuperblock() error {
needsFormat, err := srv.NeedsScmFormat()
if err != nil {
return errors.Wrap(err, "failed to check storage formatting")
}
if needsFormat {
return errors.New("can't read superblock from unformatted storage")
}
if err := srv.MountScmDevice(); err != nil {
return errors.Wrap(err, "failed to mount SCM device")
}
sb, err := ReadSuperblock(srv.superblockPath())
if err != nil {
return errors.Wrap(err, "failed to read instance superblock")
}
srv.setSuperblock(sb)
return nil
}
// WriteSuperblock writes a Superblock to storage.
func WriteSuperblock(sbPath string, sb *Superblock) error {
data, err := sb.Marshal()
if err != nil {
return err
}
return errors.Wrapf(common.WriteFileAtomic(sbPath, data, 0600),
"Failed to write Superblock to %s", sbPath)
}
// ReadSuperblock reads a Superblock from storage.
func ReadSuperblock(sbPath string) (*Superblock, error) {
data, err := ioutil.ReadFile(sbPath)
if err != nil {
return nil, errors.Wrapf(err, "Failed to read Superblock from %s", sbPath)
}
sb := &Superblock{}
if err := sb.Unmarshal(data); err != nil {
return nil, err
}
return sb, nil
}