/
sparsefilegenerator.go
227 lines (187 loc) · 7.7 KB
/
sparsefilegenerator.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
/*
Copyright 2018 OpenEBS Authors.
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.
*/
package controller
import (
"io/ioutil"
"strings"
"github.com/openebs/node-disk-manager/blockdevice"
"github.com/openebs/node-disk-manager/pkg/util"
"k8s.io/klog"
"fmt"
"os"
"path"
"strconv"
)
/*
Sparse File help simulate disk objects that can be used for testing and
proto typing solutions built using node-disk-manager(NDM). Sparse files
will be created if NDM is provided with the location where sparse files
should be located.
On Startup, if a sparse directory (EnvSparseFileDir) is specified as a
environment variable, a Sparse file with specified size (EnvSparseFileSize) will
be created and an associated BlockDevice CR will be added to Kubernetes. By default
only one sparse file will be created which can be changed by passing the desired
number of sparse files required via the environment variable EnvSparseFileCount.
On Shutdown, the status of the sparse file BlockDevice CR will be marked as Unknown.
*/
const (
// EnvSparseFileDir - defines a sparse directory.
// if it is specified as a environment variable,
// a sparse file with specified size (EnvSparseFileSize) will
// be created inside specified directory (EnvSparseFileDir)
// and an associated BlockDevice CR will be added to Kubernetes.
EnvSparseFileDir = "SPARSE_FILE_DIR"
//EnvSparseFileSize define the size of created sparse file
EnvSparseFileSize = "SPARSE_FILE_SIZE"
//EnvSparseFileCount defines the number of sparse files to be created
EnvSparseFileCount = "SPARSE_FILE_COUNT"
//SparseFileName is a name of Sparse file
SparseFileName = "ndm-sparse.img"
//SparseFileDefaultSize defines the default sparse file default size
SparseFileDefaultSize = int64(1073741824)
//SparseFileMinSize defines the minimum size for sparse file
SparseFileMinSize = int64(1073741824)
//SparseFileDefaultCount defines the default sparse count files
SparseFileDefaultCount = "1"
//SparseBlockDevicePrefix defines the prefix for the sparse device
SparseBlockDevicePrefix = "sparse-"
)
// GetSparseFileDir returns the full path to the sparse
// file directory on the node.
func GetSparseFileDir() string {
sparseFileDir := os.Getenv(EnvSparseFileDir)
if len(sparseFileDir) < 1 {
return ""
}
info, err := os.Stat(sparseFileDir)
if os.IsNotExist(err) || !info.Mode().IsDir() {
klog.Info("Specified directory doesnt exist: ", sparseFileDir)
return ""
}
return sparseFileDir
}
// GetSparseFileCount returns the number of sparse files to be
// created by NDM. Returns 0, if invalid count is specified.
func GetSparseFileCount() int {
sparseFileCountStr := os.Getenv(EnvSparseFileCount)
if len(sparseFileCountStr) < 1 {
sparseFileCountStr = SparseFileDefaultCount
}
sparseFileCount, econv := strconv.Atoi(sparseFileCountStr)
if econv != nil {
klog.Info("Error converting sparse file count: ", sparseFileCountStr)
return 0
}
return sparseFileCount
}
// GetSparseFileSize returns the size of the sparse file to be
// created by NDM. Returns 0, if invalid size is specified.
func GetSparseFileSize() int64 {
sparseFileSizeStr := os.Getenv(EnvSparseFileSize)
if len(sparseFileSizeStr) < 1 {
klog.Info("No size was specified. Using default size: ", fmt.Sprint(SparseFileDefaultSize))
return SparseFileDefaultSize
}
fileSize, econv := strconv.ParseFloat(sparseFileSizeStr, 64)
if econv != nil {
klog.Error("Error converting sparse file size: ", econv)
return 0
}
sparseFileSize := int64(fileSize)
if sparseFileSize < SparseFileMinSize {
klog.Info(fmt.Sprint(sparseFileSizeStr), " is less than minimum required. Setting the size to: ", fmt.Sprint(SparseFileMinSize))
return SparseFileMinSize
}
return sparseFileSize
}
// InitializeSparseFiles will check if the sparse file exist or have to be
// created and will update or create the associated BlockDevice CR accordingly
func (c *Controller) InitializeSparseFiles() {
sparseFileDir := GetSparseFileDir()
sparseFileSize := GetSparseFileSize()
sparseFileCount := GetSparseFileCount()
if len(sparseFileDir) < 1 || sparseFileSize < 1 || sparseFileCount < 1 {
klog.Info("No sparse file path/size provided. Skip creating sparse files.")
return
}
for i := 0; i < sparseFileCount; i++ {
sparseFile := path.Join(sparseFileDir, fmt.Sprint(i)+"-"+SparseFileName)
err := CheckAndCreateSparseFile(sparseFile, sparseFileSize)
if err != nil {
klog.Info("Error creating sparse file: ", sparseFile, "Error: ", err)
continue
}
c.MarkSparseBlockDeviceStateActive(sparseFile, sparseFileSize)
}
}
// CheckAndCreateSparseFile will reuse the existing sparse file if it already exists,
// for handling cases where NDM is upgraded or restarted. If the file doesn't exist
// a new file will be created.
func CheckAndCreateSparseFile(sparseFile string, sparseFileSize int64) error {
sparseFileInfo, err := util.SparseFileInfo(sparseFile)
if err != nil {
klog.Info("Check for existing file returned error: ", err)
klog.Info("Creating a new Sparse file: ", sparseFile)
err = util.SparseFileCreate(sparseFile, sparseFileSize)
} else {
klog.Info("Sparse file already exists: ", sparseFileInfo.Name())
}
return err
}
// GetSparseBlockDeviceUUID returns a fixed UUID for the sparse
// disk on a given node.
func GetSparseBlockDeviceUUID(hostname, sparseFile string) string {
return SparseBlockDevicePrefix + util.Hash(hostname+sparseFile)
}
// GetActiveSparseBlockDevicesUUID returns UUIDs for the sparse
// disks present in a given node.
func GetActiveSparseBlockDevicesUUID(hostname string) []string {
sparseFileLocation := GetSparseFileDir()
sparseUuids := make([]string, 0)
files, err := ioutil.ReadDir(sparseFileLocation)
if err != nil {
klog.Error("Failed to read sparse file names : ", err)
return sparseUuids
}
for _, file := range files {
if strings.HasSuffix(file.Name(), SparseFileName) {
fileName := path.Join(sparseFileLocation, file.Name())
sparseUuids = append(sparseUuids, GetSparseBlockDeviceUUID(hostname, fileName))
}
}
return sparseUuids
}
// MarkSparseBlockDeviceStateActive will either create a BlockDevice CR if it doesn't exist, or it will
// update the state of the existing CR as Active. Note that, when the NDM is going being
// gracefully shutdown, all its BlockDevice CRs are marked with State as Unknown.
func (c *Controller) MarkSparseBlockDeviceStateActive(sparseFile string, sparseFileSize int64) {
// Fill in the details of the sparse disk
BlockDeviceDetails := NewDeviceInfo()
BlockDeviceDetails.UUID = GetSparseBlockDeviceUUID(c.NodeAttributes[HostNameKey], sparseFile)
BlockDeviceDetails.NodeAttributes = c.NodeAttributes
BlockDeviceDetails.DeviceType = blockdevice.SparseBlockDeviceType
BlockDeviceDetails.Path = sparseFile
sparseFileInfo, err := util.SparseFileInfo(sparseFile)
if err != nil {
klog.Info("Error fetching the size of sparse file: ", err)
klog.Error("Failed to create a block device CR for sparse file: ", sparseFile)
return
}
BlockDeviceDetails.Capacity = uint64(sparseFileInfo.Size())
//If a BlockDevice CR already exits, update it. If not create a new one.
klog.Info("Updating the BlockDevice CR for Sparse file: ", BlockDeviceDetails.UUID)
err = c.CreateBlockDevice(BlockDeviceDetails.ToDevice())
if err != nil {
klog.Error("Failed to create a block device resource in etcd, Error: ", err)
}
}