File: block.go

package info (click to toggle)
golang-github-digitalocean-go-qemu 0.0~git20250212.ee9b066-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 860 kB
  • sloc: sh: 34; makefile: 3
file content (274 lines) | stat: -rw-r--r-- 8,634 bytes parent folder | download | duplicates (2)
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
// Copyright 2022 The go-qemu 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 qemu

import (
	"errors"
	"fmt"
	"io"
	"path/filepath"
	"time"

	"github.com/digitalocean/go-qemu/qmp"
)

const (
	blockJobCompleted = "BLOCK_JOB_COMPLETED"
	blockJobError     = "BLOCK_JOB_ERROR"
	blockJobReady     = "BLOCK_JOB_READY"
)

// BlockDevice represents a QEMU block device.
type BlockDevice struct {
	Device   string `json:"device"`
	Inserted struct {
		BackingFile      string `json:"backing_file"`
		BackingFileDepth int    `json:"backing_file_depth"`
		BPS              int    `json:"bps"`
		BPSRead          int    `json:"bps_rd"`
		BPSWrite         int    `json:"bps_wr"`
		Cache            struct {
			Direct    bool `json:"direct"`
			NoFlush   bool `json:"no-flush"`
			Writeback bool `json:"writeback"`
		} `json:"cache"`
		DetectZeroes         string `json:"detect_zeroes"`
		Driver               string `json:"drv"`
		Encrypted            bool   `json:"encrypted"`
		EncryptionKeyMissing bool   `json:"encryption_key_missing"`
		File                 string `json:"file"`
		Image                Image  `json:"image"`
		IOPs                 int    `json:"iops"`
		IOPsRead             int    `json:"iops_rd"`
		IOPsWrite            int    `json:"iops_wr"`
		NodeName             string `json:"node-name"`
		ReadOnly             bool   `json:"ro"`
		WriteThreshold       int    `json:"write_threshold"`
	} `json:"inserted"`
	IOStatus  string `json:"io-status"`
	Locked    bool   `json:"locked"`
	Removable bool   `json:"removable"`
	Type      string `json:"type"`
}

// BlockJob represents a QEMU blockjob.
type BlockJob struct {
	Busy     bool   `json:"busy"`
	Device   string `json:"device"`
	IOStatus string `json:"io-status"`
	Len      int    `json:"len"`
	Offset   int    `json:"offset"`
	Paused   bool   `json:"paused"`
	Ready    bool   `json:"ready"`
	Speed    int    `json:"speed"`
	Type     string `json:"type"`
}

// Image represents a BlockDevice backing image.
type Image struct {
	ActualSize            uint64 `json:"actual-size"`
	BackingFilename       string `json:"backing-filename"`
	BackingFilenameFormat string `json:"backing-filename-format"`
	BackingImage          struct {
		ActualSize  uint64 `json:"actual-size"`
		Dirty       bool   `json:"dirty-flag"`
		Filename    string `json:"filename"`
		Format      string `json:"format"`
		VirtualSize uint64 `json:"virtual-size"`
	} `json:"backing-image"`
	ClusterSize    int    `json:"cluster-size"`
	Dirty          bool   `json:"dirty-flag"`
	Filename       string `json:"filename"`
	Format         string `json:"format"`
	FormatSpecific struct {
		Data struct {
			Compat        string `json:"compat"`
			Corrupt       bool   `json:"corrupt"`
			LazyRefcounts bool   `json:"lazy-refcounts"`
			RefcountBits  int    `json:"refcount-bits"`
		} `json:"data"`
		Type string `json:"type"`
	} `json:"format-specific"`
	VirtualSize uint64 `json:"virtual-size"`
}

// BlockStats represents QEMU block device statistics.
type BlockStats struct {
	// Device does not actually appear QEMU's output, but is added
	// by this package.
	Device string `json:"-"`

	AccountFailed             bool   `json:"account_failed"`
	AccountInvalid            bool   `json:"account_invalid"`
	FailedFlushOperations     int    `json:"failed_flush_operations"`
	FailedReadOperations      int    `json:"failed_rd_operations"`
	FailedWriteOperations     int    `json:"failed_wr_operations"`
	FlushOperations           int    `json:"flush_operations"`
	FlushTotalTimeNanoseconds int64  `json:"flush_total_time_ns"`
	IdleTimeNanoseconds       int64  `json:"idle_time_ns"`
	InvalidFlushOperations    int    `json:"invalid_flush_operations"`
	InvalidReadOperations     int    `json:"invalid_rd_operations"`
	InvalidWriteOperations    int    `json:"invalid_wr_operations"`
	ReadBytes                 uint64 `json:"rd_bytes"`
	ReadMerged                int    `json:"rd_merged"`
	ReadOperations            int    `json:"rd_operations"`
	ReadTotalTimeNanoseconds  int    `json:"rd_total_time_ns"`
	WriteBytes                uint64 `json:"wr_bytes"`
	WriteHighestOffset        int64  `json:"wr_highest_offset"`
	WriteMerged               int    `json:"wr_merged"`
	WriteOperations           int    `json:"wr_operations"`
	WriteTotalTimeNanoseconds int64  `json:"wr_total_time_ns"`
}

// Mirror copies a block device to the given destination.
// The destination path specified by dest must be absolute.
func (bd BlockDevice) Mirror(d *Domain, dest string, timeout time.Duration) error {
	if !filepath.IsAbs(dest) {
		return errors.New("destination path must be absolute")
	}

	return waitForSignal(d, blockJobReady, timeout, func() error {
		cmd := qmp.Command{
			Execute: "drive-mirror",
			Args: map[string]string{
				"device": bd.Device,
				"target": dest,
				"sync":   "full",
				"mode":   "absolute-paths",
				"format": "raw",
			},
		}

		_, err := d.Run(cmd)
		return err
	})
}

// Commit synchronously merges an overlay image onto a block device's
// root backing image. Once the operation is complete, CompleteJob()
// must be called to pivot the domain back to the original backing image.
func (bd BlockDevice) Commit(d *Domain, overlay, jobID string, timeout time.Duration) error {
	return waitForSignal(d, blockJobReady, timeout, func() error {
		cmd := qmp.Command{
			Execute: "block-commit",
			Args: map[string]string{
				"device": bd.Inserted.NodeName,
				"top":    overlay,
				"job-id": jobID,
			},
		}

		_, err := d.Run(cmd)
		return err
	})
}

// Cancel a running block job.
// For block-mirror operations, this cancels the block job.
func (job BlockJob) Cancel(d *Domain, timeout time.Duration) error {
	return waitForSignal(d, blockJobCompleted, timeout, func() error {
		cmd := qmp.Command{
			Execute: "block-job-cancel",
			Args: map[string]string{
				"device": job.Device,
			},
		}

		_, err := d.Run(cmd)
		return err
	})
}

// Complete a running block job.
// For blockcommit backups, this performs the "pivot" back to the original
// backing image.
func (job BlockJob) Complete(d *Domain, timeout time.Duration) error {
	return waitForSignal(d, blockJobCompleted, timeout, func() error {
		cmd := qmp.Command{
			Execute: "block-job-complete",
			Args: map[string]string{
				"device": job.Device,
			},
		}

		_, err := d.Run(cmd)
		return err
	})
}

// Snapshot creates a point in time snapshot.
// The disk's image is given a new QCOW2 overlay, leaving the underlying image
// in a state that is considered safe for copying.
func (bd BlockDevice) Snapshot(d *Domain, overlay, nodeName string) error {
	cmd := qmp.Command{
		Execute: "blockdev-snapshot-sync",
		Args: map[string]string{
			"node-name":          bd.Inserted.NodeName,
			"snapshot-node-name": nodeName,
			"snapshot-file":      overlay,
		},
	}

	_, err := d.Run(cmd)
	return err
}

// waitForSignal opens a domain's QMP event stream and invokes an input
// closure to send commands which would create results on that event stream.
func waitForSignal(d *Domain, signal string, timeout time.Duration, fn func() error) error {
	// "done" signal must always be sent to avoid leaking goroutines.
	events, done, err := d.Events()
	if err != nil {
		return err
	}
	defer close(done)

	jobErr := make(chan error)
	go func() {
		jobErr <- waitForJob(events, signal, timeout)
	}()

	if err := fn(); err != nil {
		return err
	}

	return <-jobErr
}

// waitForJob monitors the domain's QMP event stream, waiting for the provided
// signal. An error is returned when a BLOCK_JOB_ERROR is seen, a timeout
// occurs, or the underlying channel is closed.
func waitForJob(events <-chan qmp.Event, signal string, timeout time.Duration) error {
	// Consider events stalled after timeout for X amount of time total,
	// rather than X amount of time without an incoming event
	stalled := time.After(timeout)

	for {
		select {
		case e, ok := <-events:
			if !ok {
				return io.EOF
			}
			switch e.Event {
			case signal:
				return nil
			case blockJobError:
				return fmt.Errorf("block job error: %v", e.Data)
			}
		case <-stalled:
			return errors.New("block job timeout")
		}
	}
}