forked from snapcore/snapd
/
ondisk.go
296 lines (256 loc) · 8.82 KB
/
ondisk.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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2019-2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package gadget
import (
"encoding/json"
"fmt"
"os/exec"
"strconv"
"strings"
"github.com/snapcore/snapd/gadget/quantity"
"github.com/snapcore/snapd/osutil"
)
// sfdiskDeviceDump represents the sfdisk --dump JSON output format.
type sfdiskDeviceDump struct {
PartitionTable sfdiskPartitionTable `json:"partitiontable"`
}
type sfdiskPartitionTable struct {
Label string `json:"label"`
ID string `json:"id"`
Device string `json:"device"`
Unit string `json:"unit"`
FirstLBA uint64 `json:"firstlba"`
LastLBA uint64 `json:"lastlba"`
Partitions []sfdiskPartition `json:"partitions"`
}
type sfdiskPartition struct {
Node string `json:"node"`
Start uint64 `json:"start"`
Size uint64 `json:"size"`
// List of GPT partition attributes in <attr>[ <attr>] format, numeric attributes
// are listed as GUID:<bit>[,<bit>]. Note that the even though the sfdisk(8) manpage
// says --part-attrs takes a space or comma separated list, the output from
// --json/--dump uses a different format.
Attrs string `json:"attrs"`
Type string `json:"type"`
UUID string `json:"uuid"`
Name string `json:"name"`
}
// TODO: consider looking into merging LaidOutVolume/Structure OnDiskVolume/Structure
// OnDiskStructure represents a gadget structure laid on a block device.
type OnDiskStructure struct {
LaidOutStructure
// Node identifies the device node of the block device.
Node string
// Size of the on disk structure, which is at least equal to the
// LaidOutStructure.Size but may be bigger if the partition was
// expanded.
Size quantity.Size
}
// OnDiskVolume holds information about the disk device including its partitioning
// schema, the partition table, and the structure layout it contains.
type OnDiskVolume struct {
Structure []OnDiskStructure
ID string
Device string
Schema string
// size in bytes
Size quantity.Size
// sector size in bytes
SectorSize quantity.Size
}
// OnDiskVolumeFromDevice obtains the partitioning and filesystem information from
// the block device.
func OnDiskVolumeFromDevice(device string) (*OnDiskVolume, error) {
output, err := exec.Command("sfdisk", "--json", device).Output()
if err != nil {
return nil, osutil.OutputErr(output, err)
}
var dump sfdiskDeviceDump
if err := json.Unmarshal(output, &dump); err != nil {
return nil, fmt.Errorf("cannot parse sfdisk output: %v", err)
}
dl, err := onDiskVolumeFromPartitionTable(dump.PartitionTable)
if err != nil {
return nil, err
}
dl.Device = device
return dl, nil
}
func fromSfdiskPartitionType(st string, sfdiskLabel string) (string, error) {
switch sfdiskLabel {
case "dos":
// sometimes sfdisk reports what is "0C" in gadget.yaml as "c",
// normalize the values
v, err := strconv.ParseUint(st, 16, 8)
if err != nil {
return "", fmt.Errorf("cannot convert MBR partition type %q", st)
}
return fmt.Sprintf("%02X", v), nil
case "gpt":
return st, nil
default:
return "", fmt.Errorf("unsupported partitioning schema %q", sfdiskLabel)
}
}
func blockdevSizeCmd(cmd, devpath string) (quantity.Size, error) {
out, err := exec.Command("blockdev", cmd, devpath).CombinedOutput()
if err != nil {
return 0, osutil.OutputErr(out, err)
}
nospace := strings.TrimSpace(string(out))
sz, err := strconv.Atoi(nospace)
if err != nil {
return 0, fmt.Errorf("cannot parse blockdev %s result size %q: %v", cmd, nospace, err)
}
return quantity.Size(sz), nil
}
func blockDeviceSizeInSectors(devpath string) (quantity.Size, error) {
// the size is always reported in 512-byte sectors, even if the device does
// not have a physical sector size of 512
// XXX: consider using /sys/block/<dev>/size directly
return blockdevSizeCmd("--getsz", devpath)
}
func blockDeviceSectorSize(devpath string) (quantity.Size, error) {
// the size is reported in raw bytes
sz, err := blockdevSizeCmd("--getss", devpath)
if err != nil {
return 0, err
}
// ensure that the sector size is a multiple of 512, since we rely on that
// when we calculate the size in sectors, as blockdev --getsz always returns
// the size in 512-byte sectors
if sz%512 != 0 {
return 0, fmt.Errorf("cannot calculate structure size: sector size (%s) is not a multiple of 512", sz.String())
}
if sz == 0 {
// extra paranoia
return 0, fmt.Errorf("internal error: sector size returned as 0")
}
return sz, nil
}
// onDiskVolumeFromPartitionTable takes an sfdisk dump partition table and returns
// the partitioning information as an on-disk volume.
func onDiskVolumeFromPartitionTable(ptable sfdiskPartitionTable) (*OnDiskVolume, error) {
if ptable.Unit != "sectors" {
return nil, fmt.Errorf("cannot position partitions: unknown unit %q", ptable.Unit)
}
structure := make([]VolumeStructure, len(ptable.Partitions))
ds := make([]OnDiskStructure, len(ptable.Partitions))
sectorSize, err := blockDeviceSectorSize(ptable.Device)
if err != nil {
return nil, err
}
for i, p := range ptable.Partitions {
info, err := filesystemInfo(p.Node)
if err != nil {
return nil, fmt.Errorf("cannot obtain filesystem information: %v", err)
}
switch {
case len(info.BlockDevices) == 0:
continue
case len(info.BlockDevices) > 1:
return nil, fmt.Errorf("unexpected number of blockdevices for node %q: %v", p.Node, info.BlockDevices)
}
bd := info.BlockDevices[0]
vsType, err := fromSfdiskPartitionType(p.Type, ptable.Label)
if err != nil {
return nil, fmt.Errorf("cannot convert sfdisk partition type %q: %v", p.Type, err)
}
structure[i] = VolumeStructure{
Name: p.Name,
Size: quantity.Size(p.Size) * sectorSize,
Label: bd.Label,
Type: vsType,
Filesystem: bd.FSType,
}
ds[i] = OnDiskStructure{
LaidOutStructure: LaidOutStructure{
VolumeStructure: &structure[i],
StartOffset: quantity.Offset(p.Start) * quantity.Offset(sectorSize),
Index: i + 1,
},
Node: p.Node,
}
}
var numSectors quantity.Size
if ptable.LastLBA != 0 {
// sfdisk reports the last usable LBA for GPT disks only
numSectors = quantity.Size(ptable.LastLBA + 1)
} else {
// sfdisk does not report any information about the size of a
// MBR partitioned disk, find out the size of the device by
// other means
sz, err := blockDeviceSizeInSectors(ptable.Device)
if err != nil {
return nil, fmt.Errorf("cannot obtain the size of device %q: %v", ptable.Device, err)
}
// since blockdev always reports the size in 512-byte sectors, if for
// some reason we are on a disk that does not 512-byte sectors, we will
// get confused, so in this case, multiply the number of 512-byte
// sectors by 512, then divide by the actual sector size to get the
// number of sectors
// this will never have a divisor, since we verified that sector size is
// a multiple of 512 above
numSectors = sz * 512 / sectorSize
}
dl := &OnDiskVolume{
Structure: ds,
ID: ptable.ID,
Device: ptable.Device,
Schema: ptable.Label,
Size: numSectors * sectorSize,
SectorSize: sectorSize,
}
return dl, nil
}
// UpdatePartitionList re-reads the partitioning data from the device and
// updates the volume structures in the specified volume.
func UpdatePartitionList(dl *OnDiskVolume) error {
layout, err := OnDiskVolumeFromDevice(dl.Device)
if err != nil {
return fmt.Errorf("cannot read disk layout: %v", err)
}
if dl.ID != layout.ID {
return fmt.Errorf("partition table IDs don't match")
}
dl.Structure = layout.Structure
return nil
}
// lsblkFilesystemInfo represents the lsblk --fs JSON output format.
type lsblkFilesystemInfo struct {
BlockDevices []lsblkBlockDevice `json:"blockdevices"`
}
type lsblkBlockDevice struct {
Name string `json:"name"`
FSType string `json:"fstype"`
Label string `json:"label"`
UUID string `json:"uuid"`
Mountpoint string `json:"mountpoint"`
}
func filesystemInfo(node string) (*lsblkFilesystemInfo, error) {
output, err := exec.Command("lsblk", "--fs", "--json", node).CombinedOutput()
if err != nil {
return nil, osutil.OutputErr(output, err)
}
var info lsblkFilesystemInfo
if err := json.Unmarshal(output, &info); err != nil {
return nil, fmt.Errorf("cannot parse lsblk output: %v", err)
}
return &info, nil
}