-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathresize_test.go
More file actions
377 lines (367 loc) · 10.6 KB
/
resize_test.go
File metadata and controls
377 lines (367 loc) · 10.6 KB
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
package partitionresizer
import (
"bytes"
"errors"
"io"
iofs "io/fs"
"os"
"path/filepath"
"testing"
"github.com/diskfs/go-diskfs"
"github.com/diskfs/go-diskfs/backend/file"
"github.com/diskfs/go-diskfs/partition/gpt"
)
func TestCreatePartitions(t *testing.T) {
// create a disk with GPT partitions, call createPartitions, verify partitions created correctly
workDir := t.TempDir()
f, err := os.CreateTemp(workDir, "disk.img")
if err != nil {
t.Fatalf("failed to create temp disk image: %v", err)
}
if err := os.Truncate(f.Name(), 1*GB); err != nil {
t.Fatalf("failed to truncate disk image: %v", err)
}
defer func() { _ = f.Close() }()
backend := file.New(f, false)
d, err := diskfs.OpenBackend(backend, diskfs.WithOpenMode(diskfs.ReadWrite))
if err != nil {
t.Fatalf("failed to open disk: %v", err)
}
// create a partition table, will use <512MB partitions, so we can
// cleanly run the test targeting second half of the disk
var offset uint64 = 2048
table := &gpt.Table{
Partitions: []*gpt.Partition{
{
Index: 1,
Start: offset,
Size: 36 * MB,
Type: gpt.LinuxFilesystem,
Name: "part1",
Attributes: 0,
},
{
Index: 2,
Start: offset + 36*MB,
Size: 200 * MB,
Type: gpt.LinuxFilesystem,
Name: "part2",
Attributes: 0,
},
},
}
if err := d.Partition(table); err != nil {
t.Fatalf("failed to write partition table: %v", err)
}
// define resize targets
resizes := []partitionResizeTarget{
{
original: partitionData{
number: 1,
start: int64(offset),
size: int64(table.Partitions[0].Size),
label: table.Partitions[0].Name,
},
target: partitionData{
number: 3,
start: int64(offset + 512*MB),
size: int64(table.Partitions[0].Size),
label: "part1_resized",
},
},
{
original: partitionData{
number: 2,
start: int64(offset + 36*MB),
size: int64(table.Partitions[1].Size),
label: table.Partitions[1].Name,
},
target: partitionData{
number: 4,
start: int64(offset + 36*MB + 512*MB),
size: int64(table.Partitions[1].Size),
label: "part2_resized",
},
},
}
// call createPartitions
if err := createPartitions(d, resizes); err != nil {
t.Fatalf("createPartitions failed: %v", err)
}
// verify partitions created
tableRaw, err := d.GetPartitionTable()
if err != nil {
t.Fatalf("failed to get partition table: %v", err)
}
newTable, ok := tableRaw.(*gpt.Table)
if !ok {
t.Fatalf("unsupported partition table type, only GPT is supported")
}
expectedCount := len(table.Partitions) + len(resizes)
if len(newTable.Partitions) != expectedCount {
t.Fatalf("expected %d partitions after resize, got %d", expectedCount, len(newTable.Partitions))
}
sectorSize := newTable.LogicalSectorSize
for _, r := range resizes {
newPartRaw, err := d.GetPartition(r.target.number)
if err != nil {
t.Fatalf("failed to get new partition %d: %v", r.target.number, err)
}
newPart, ok := newPartRaw.(*gpt.Partition)
if !ok {
t.Fatalf("unsupported partition table type, only GPT is supported")
}
newPartStart := int64(newPart.Start) * int64(sectorSize)
if newPartStart != r.target.start {
t.Errorf("partition %d start mismatch: expected %d, got %d", r.target.number, r.target.start, newPartStart)
}
if newPart.Size != uint64(r.target.size) {
t.Errorf("partition %d size mismatch: expected %d, got %d", r.target.number, r.target.size, newPart.Size)
}
if newPart.Name != r.original.label {
t.Errorf("partition %d label mismatch: expected %s, got %s", r.target.number, r.original.label, newPart.Name)
}
}
}
func TestRemovePartitions(t *testing.T) {
// create a disk with GPT partitions, call removePartitions, verify partitions removed correctly
workDir := t.TempDir()
f, err := os.CreateTemp(workDir, "disk.img")
if err != nil {
t.Fatalf("failed to create temp disk image: %v", err)
}
if err := os.Truncate(f.Name(), 1*GB); err != nil {
t.Fatalf("failed to truncate disk image: %v", err)
}
defer func() { _ = f.Close() }()
backend := file.New(f, false)
d, err := diskfs.OpenBackend(backend, diskfs.WithOpenMode(diskfs.ReadWrite))
if err != nil {
t.Fatalf("failed to open disk: %v", err)
}
var offset uint64 = 2048
table := &gpt.Table{
Partitions: []*gpt.Partition{
{
Index: 1,
Start: offset,
Size: 36 * MB,
Type: gpt.LinuxFilesystem,
Name: "part1",
Attributes: 0,
},
{
Index: 2,
Start: offset + 36*MB,
Size: 200 * MB,
Type: gpt.LinuxFilesystem,
Name: "part2",
Attributes: 0,
},
{
Index: 3,
Start: offset + 36*MB + 200*MB,
Size: 100 * MB,
Type: gpt.LinuxFilesystem,
Name: "part3",
Attributes: 0,
},
{
Index: 4,
Start: offset + 36*MB + 200*MB + 100*MB,
Size: 100 * MB,
Type: gpt.LinuxFilesystem,
Name: "part4",
Attributes: 0,
},
},
}
if err := d.Partition(table); err != nil {
t.Fatalf("failed to write partition table: %v", err)
}
// define resize targets
resizes := []partitionResizeTarget{
{
original: partitionData{
number: 2,
start: int64(offset + 36*MB),
size: int64(table.Partitions[1].Size),
label: table.Partitions[1].Name,
},
target: partitionData{
number: 5,
start: 0,
size: 0,
label: "",
},
},
{
original: partitionData{
number: 3,
start: int64(offset + 36*MB + 200*MB),
size: int64(table.Partitions[2].Size),
label: table.Partitions[2].Name,
},
target: partitionData{
number: 6,
start: 0,
size: 0,
label: "",
},
},
}
// call removePartitions
if err := removePartitions(d, resizes); err != nil {
t.Fatalf("removePartitions failed: %v", err)
}
// verify partitions removed
tableRaw, err := d.GetPartitionTable()
if err != nil {
t.Fatalf("failed to get partition table: %v", err)
}
newTable, ok := tableRaw.(*gpt.Table)
if !ok {
t.Fatalf("unsupported partition table type, only GPT is supported")
}
expectedCount := len(table.Partitions) - len(resizes)
if len(newTable.Partitions) != expectedCount {
t.Fatalf("expected %d partitions after resize, got %d", expectedCount, len(newTable.Partitions))
}
}
func TestCopyFilesystems(t *testing.T) {
// create a duplicate disk with a partition with the specified filesystem type
tmpdir := t.TempDir()
tmpfile := filepath.Join(tmpdir, "testcopyfilesystem")
if err := testCopyFile(imgFile, tmpfile); err != nil {
t.Fatalf("failed to copy disk image: %v", err)
}
f, err := os.OpenFile(tmpfile, os.O_RDWR, 0o666)
if err != nil {
t.Fatalf("failed to open disk image: %v", err)
}
defer func() { _ = f.Close() }()
backend := file.New(f, false)
d, err := diskfs.OpenBackend(backend, diskfs.WithOpenMode(diskfs.ReadWrite))
if err != nil {
t.Fatalf("failed to open disk: %v", err)
}
tableRaw, err := d.GetPartitionTable()
if err != nil {
t.Fatalf("failed to get partition table: %v", err)
}
table, ok := tableRaw.(*gpt.Table)
if !ok {
t.Fatalf("unsupported partition table type, only GPT is supported")
}
// define resize target
// find out what partitions we have and where they end, so we can determine where to start
var (
maxPart = 1
maxEnd uint64 = 0
)
for _, part := range table.Partitions {
if int(part.Index) > maxPart {
maxPart = int(part.Index)
}
if end := part.Start + part.Size; end > maxEnd {
maxEnd = end
}
}
resizes := []partitionResizeTarget{
{
original: partitionData{
number: table.Partitions[0].Index,
start: int64(table.Partitions[0].Start),
size: int64(table.Partitions[0].Size),
label: table.Partitions[0].Name,
},
target: partitionData{
number: maxPart + 1,
start: int64(maxEnd + MB), // start it 1 MB after end of previous for extra safety
size: int64(table.Partitions[0].Size),
label: "part1_resized",
},
},
}
// create the new partition directly
table.Partitions = append(table.Partitions, &gpt.Partition{
Start: uint64(resizes[0].target.start),
Size: uint64(resizes[0].target.size),
Type: table.Partitions[0].Type,
Name: table.Partitions[0].Name,
Attributes: table.Partitions[0].Attributes,
Index: len(table.Partitions) + 1,
})
if err := d.Partition(table); err != nil {
t.Fatalf("failed to write updated partition table: %v", err)
}
// call copyFilesystems
if err := copyFilesystems(d, resizes); err != nil {
t.Fatalf("copyFilesystems failed: %v", err)
}
// get old FS
fs, err := d.GetFilesystem(resizes[0].original.number)
if err != nil {
t.Fatalf("failed to get filesystem on original partition: %v", err)
}
// verify filesystem copied
newFS, err := d.GetFilesystem(resizes[0].target.number)
if err != nil {
t.Fatalf("failed to get filesystem on new partition: %v", err)
}
if newFS.Type() != fs.Type() {
t.Errorf("filesystem type mismatch: expected %v, got %v", fs.Type(), newFS.Type())
}
// check that the contents match
if err := iofs.WalkDir(fs, ".", func(path string, d iofs.DirEntry, err error) error {
if err != nil {
t.Fatalf("error walking original filesystem: %v", err)
}
if path == "." || path == "/" {
return nil
}
origF, err := fs.Open(path)
if err != nil {
t.Fatalf("failed to open %s in original filesystem: %v", path, err)
}
info, err := origF.Stat()
if err != nil {
t.Fatalf("failed to stat %s in original filesystem: %v", path, err)
}
newF, err := newFS.Open(path)
if err != nil {
t.Fatalf("failed to open %s in new filesystem: %v", path, err)
}
newInfo, err := newF.Stat()
if err != nil {
t.Fatalf("failed to stat %s in new filesystem: %v", path, err)
}
if info.IsDir() && !newInfo.IsDir() {
t.Errorf("expected %s to be a directory in new filesystem", path)
}
if !info.IsDir() && newInfo.IsDir() {
t.Errorf("expected %s to be a file in new filesystem", path)
}
// a directory already was matched, so continue
if info.IsDir() {
return nil
}
// file, so check contents
origData := make([]byte, info.Size())
if _, err := origF.Read(origData); err != nil && !errors.Is(err, io.EOF) {
t.Fatalf("failed to read file %s in original filesystem: %v", path, err)
}
newData := make([]byte, newInfo.Size())
if _, err := newF.Read(newData); err != nil && !errors.Is(err, io.EOF) {
t.Fatalf("failed to read file %s in new filesystem: %v", path, err)
}
if !bytes.Equal(origData, newData) {
t.Errorf("file content mismatch for %s: expected %q, got %q", path, string(origData), string(newData))
}
return nil
}); err != nil {
t.Fatalf("error walking original filesystem: %v", err)
}
}
func TestShrinkFilesystems(t *testing.T) {
}