-
Notifications
You must be signed in to change notification settings - Fork 3
/
ploop.go
451 lines (365 loc) · 10.7 KB
/
ploop.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package ploop
import "strings"
import "os/exec"
import "sync"
// #include <ploop/libploop.h>
import "C"
// Possible SetVerboseLevel arguments
const (
NoConsole = C.PLOOP_LOG_NOCONSOLE
NoStdout = C.PLOOP_LOG_NOSTDOUT
Timestamps = C.PLOOP_LOG_TIMESTAMPS
)
// SetVerboseLevel sets a level of verbosity when logging to stdout/stderr
func SetVerboseLevel(v int) {
C.ploop_set_verbose_level(C.int(v))
}
// SetLogFile enables logging to a file and sets log file name
func SetLogFile(file string) error {
cfile := C.CString(file)
defer cfree(cfile)
ret := C.ploop_set_log_file(cfile)
return mkerr(ret)
}
// SetLogLevel sets a level of verbosity when logging to a file
func SetLogLevel(v int) {
C.ploop_set_log_level(C.int(v))
}
// Ploop is a type containing DiskDescriptor.xml opened by the library
type Ploop struct {
d *C.struct_ploop_disk_images_data
}
var once sync.Once
// load ploop modules
func loadKmod() {
// try to load ploop modules
modules := []string{"ploop", "pfmt_ploop1", "pfmt_raw", "pio_direct", "pio_nfs", "pio_kaio"}
for _, m := range modules {
exec.Command("modprobe", m).Run()
}
}
// Open opens a ploop DiskDescriptor.xml, most ploop operations require it
func Open(file string) (Ploop, error) {
var d Ploop
once.Do(loadKmod)
cfile := C.CString(file)
defer cfree(cfile)
ret := C.ploop_open_dd(&d.d, cfile)
return d, mkerr(ret)
}
// Close closes a ploop disk descriptor when it is no longer needed
func (d Ploop) Close() {
C.ploop_close_dd(d.d)
}
// ImageMode is a type for CreateParam.Mode field
type ImageMode int
// Possible values for ImageMode
const (
Expanded ImageMode = C.PLOOP_EXPANDED_MODE
Preallocated ImageMode = C.PLOOP_EXPANDED_PREALLOCATED_MODE
Raw ImageMode = C.PLOOP_RAW_MODE
)
// ParseImageMode converts a string to ImageMode value
func ParseImageMode(s string) (ImageMode, error) {
switch strings.ToLower(s) {
case "expanded":
return Expanded, nil
case "preallocated":
return Preallocated, nil
case "raw":
return Raw, nil
default:
return Expanded, mkerr(E_PARAM)
}
}
// String converts an ImageMode value to string
func (m ImageMode) String() string {
switch m {
case Expanded:
return "Expanded"
case Preallocated:
return "Preallocated"
case Raw:
return "Raw"
}
return "<unknown>"
}
// CreateFlags is a type for CreateParam.Flags
type CreateFlags uint
// Possible values for CreateFlags
const (
NoLazy CreateFlags = C.PLOOP_CREATE_NOLAZY
)
// CreateParam is a set of parameters for a newly created ploop
type CreateParam struct {
Size uint64 // image size, in kilobytes (FS size is about 10% smaller)
Mode ImageMode // image mode
File string // path to and a file name for base delta image
CLog uint // cluster block size log (6 to 15, default 11)
Flags CreateFlags // flags
}
// Create creates a ploop image and its DiskDescriptor.xml
func Create(p *CreateParam) error {
var a C.struct_ploop_create_param
once.Do(loadKmod)
// default image file name
if p.File == "" {
p.File = "root.hdd"
}
a.size = convertSize(p.Size)
a.mode = C.int(p.Mode)
if p.CLog != 0 {
// ploop cluster block size, in 512-byte sectors
// default is 1M cluster block size (CLog=11)
// 2^11 = 2048 sectors, 2048*512 = 1M
a.blocksize = 1 << p.CLog
}
a.flags = C.uint(p.Flags)
a.image = C.CString(p.File)
defer cfree(a.image)
a.fstype = C.CString("ext4")
defer cfree(a.fstype)
ret := C.ploop_create_image(&a)
return mkerr(ret)
}
// MountParam is a set of parameters to pass to Mount()
type MountParam struct {
UUID string // snapshot uuid (empty for top delta)
Target string // mount point (empty if no mount is needed)
Flags int // bit mount flags such as MS_NOATIME
Data string // auxiliary mount options
Readonly bool // mount read-only
Fsck bool // do fsck before mounting inner FS
Quota bool // enable quota for inner FS
}
// Mount creates a ploop device and (optionally) mounts it
func (d Ploop) Mount(p *MountParam) (string, error) {
var a C.struct_ploop_mount_param
var device string
if p.UUID != "" {
a.guid = C.CString(p.UUID)
defer cfree(a.guid)
}
if p.Target != "" {
a.target = C.CString(p.Target)
defer cfree(a.target)
}
// mount_data should not be NULL
a.mount_data = C.CString(p.Data)
defer cfree(a.mount_data)
a.flags = C.int(p.Flags)
a.ro = boolToC(p.Readonly)
a.fsck = boolToC(p.Fsck)
a.quota = boolToC(p.Quota)
ret := C.ploop_mount_image(d.d, &a)
if ret == 0 {
device = C.GoString(&a.device[0])
// TODO? fsck_code = C.GoString(a.fsck_rc)
}
return device, mkerr(ret)
}
// Umount unmounts the ploop filesystem and dismantles the device
func (d Ploop) Umount() error {
ret := C.ploop_umount_image(d.d)
return mkerr(ret)
}
// UmountByDevice unmounts the ploop filesystem and dismantles the device.
// Unlike Umount(), this is a lower-level function meaning it can be less
// safe and should generally not be used.
func UmountByDevice(dev string) error {
cdev := C.CString(dev)
defer cfree(cdev)
ret := C.ploop_umount(cdev, nil)
return mkerr(ret)
}
// Resize changes the ploop size. Online resize is recommended.
func (d Ploop) Resize(size uint64, offline bool) error {
var p C.struct_ploop_resize_param
p.size = convertSize(size)
p.offline_resize = boolToC(offline)
ret := C.ploop_resize_image(d.d, &p)
return mkerr(ret)
}
// Snapshot creates a ploop snapshot, returning its uuid
func (d Ploop) Snapshot() (string, error) {
var p C.struct_ploop_snapshot_param
uuid, err := UUID()
if err != nil {
return "", err
}
p.guid = C.CString(uuid)
defer cfree(p.guid)
ret := C.ploop_create_snapshot(d.d, &p)
if ret == 0 {
uuid = C.GoString(p.guid)
}
return uuid, mkerr(ret)
}
// SwitchSnapshot switches to a specified snapshot,
// creates a new empty delta on top of it, and makes it a top one
// (i.e. the one new data will be written to).
// Old top delta (i.e. data modified since the last snapshot) is lost.
func (d Ploop) SwitchSnapshot(uuid string) error {
var p C.struct_ploop_snapshot_switch_param
p.guid = C.CString(uuid)
defer cfree(p.guid)
ret := C.ploop_switch_snapshot_ex(d.d, &p)
return mkerr(ret)
}
// SwitchFlag is a type for SwitchSnapshotExtended.Flags
type SwitchFlag uint
const (
// SkipDestroy flag, if set, modifies the behavior of
// SwitchSnapshotExtended to not delete the old top delta, but
// make it a snapshot and return its uuid. Without this flag,
// old top delta (i.e. data modified since the last snapshot)
// is lost.
SkipDestroy SwitchFlag = C.PLOOP_SNAP_SKIP_TOPDELTA_DESTROY
// SkipCreate flag, if set, modifies the behavior of
// SwitchSnapshotExtended to not create a new top delta,
// but rather transform the specified snapshot itself to be
// the new top delta), so all new changes will be written
// right to it. Snapshot UUID is lost in this case.
SkipCreate SwitchFlag = C.PLOOP_SNAP_SKIP_TOPDELTA_CREATE
)
// SwitchSnapshotExtended is same as SwitchSnapshot but with additional
// flags modifying its behavior. Please see individual flags description.
// Returns uuid of what was the old top delta if SkipDestroy flag is set.
func (d Ploop) SwitchSnapshotExtended(uuid string, flags SwitchFlag) (string, error) {
var p C.struct_ploop_snapshot_switch_param
oldUUID := ""
p.guid = C.CString(uuid)
defer cfree(p.guid)
p.flags = C.int(flags)
if flags&SkipDestroy != 0 {
oldUUID, err := UUID()
if err != nil {
return "", err
}
p.guid_old = C.CString(oldUUID)
defer cfree(p.guid_old)
}
ret := C.ploop_switch_snapshot_ex(d.d, &p)
return oldUUID, mkerr(ret)
}
// DeleteSnapshot deletes a snapshot (merging it down if necessary)
func (d Ploop) DeleteSnapshot(uuid string) error {
cuuid := C.CString(uuid)
defer cfree(cuuid)
ret := C.ploop_delete_snapshot(d.d, cuuid)
return mkerr(ret)
}
// ReplaceFlag is a type for ReplaceParam.Flags field
type ReplaceFlag int
// Possible values for ReplaceParam.Flags field
const (
// KeepName renames the new file to old file name after replace;
// note that if this option is used the old file is removed.
KeepName ReplaceFlag = C.PLOOP_REPLACE_KEEP_NAME
)
// ReplaceParam is a set of parameters to Replace()
type ReplaceParam struct {
File string // new image file name
// Image to be replaced is specified by either
// uuid, current file name, or level,
// in the above order of preference.
UUID string
CurFile string
Level int
Flags ReplaceFlag
}
// Replace replaces a ploop image to a different (but identical) one
func (d Ploop) Replace(p *ReplaceParam) error {
var a C.struct_ploop_replace_param
a.file = C.CString(p.File)
defer cfree(a.file)
if p.UUID != "" {
a.guid = C.CString(p.UUID)
defer cfree(a.guid)
} else if p.CurFile != "" {
a.cur_file = C.CString(p.CurFile)
defer cfree(a.cur_file)
} else {
a.level = C.int(p.Level)
}
a.flags = C.int(p.Flags)
ret := C.ploop_replace_image(d.d, &a)
return mkerr(ret)
}
// IsMounted returns true if ploop is mounted
func (d Ploop) IsMounted() (bool, error) {
ret := C.ploop_is_mounted(d.d)
if ret == 0 {
return false, nil
} else if ret == 1 {
return true, nil
} else {
// error, but no code, make our own
return false, mkerr(E_SYS)
}
}
// FSInfoData holds information about ploop inner file system
type FSInfoData struct {
BlockSize uint64
Blocks uint64
BlocksFree uint64
Inodes uint64
InodesFree uint64
}
// FSInfo gets info of ploop's inner file system
func FSInfo(file string) (FSInfoData, error) {
var cinfo C.struct_ploop_info
var info FSInfoData
cfile := C.CString(file)
defer cfree(cfile)
once.Do(loadKmod)
ret := C.ploop_get_info_by_descr(cfile, &cinfo)
if ret == 0 {
info.BlockSize = uint64(cinfo.fs_bsize)
info.Blocks = uint64(cinfo.fs_blocks)
info.BlocksFree = uint64(cinfo.fs_bfree)
info.Inodes = uint64(cinfo.fs_inodes)
info.InodesFree = uint64(cinfo.fs_ifree)
}
return info, mkerr(ret)
}
// ImageInfoData holds information about ploop image
type ImageInfoData struct {
Blocks uint64
BlockSize uint32
Version int
}
// ImageInfo gets information about a ploop image
func (d Ploop) ImageInfo() (ImageInfoData, error) {
var cinfo C.struct_ploop_spec
var info ImageInfoData
ret := C.ploop_get_spec(d.d, &cinfo)
if ret == 0 {
info.Blocks = uint64(cinfo.size)
info.BlockSize = uint32(cinfo.blocksize)
info.Version = int(cinfo.fmt_version)
}
return info, mkerr(ret)
}
// TopDeltaFile returns file name of top delta
func (d Ploop) TopDeltaFile() (string, error) {
const len = 4096 // PATH_MAX
var out [len]C.char
ret := C.ploop_get_top_delta_fname(d.d, &out[0], len)
if ret != 0 {
// error, but no code, make our own
return "", mkerr(E_SYS)
}
file := C.GoString(&out[0])
return file, nil
}
// UUID generates a ploop UUID
func UUID() (string, error) {
var cuuid [39]C.char
ret := C.ploop_uuid_generate(&cuuid[0], 39)
if ret != 0 {
return "", mkerr(ret)
}
uuid := C.GoString(&cuuid[0])
return uuid, nil
}