-
-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathdisk.go
More file actions
61 lines (54 loc) · 1.65 KB
/
disk.go
File metadata and controls
61 lines (54 loc) · 1.65 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
package vz
import (
"context"
"fmt"
"os"
"os/exec"
"strconv"
)
// CreateDiskImage is creating disk image with specified filename and filesize.
// For example, if you want to create disk with 64GiB, you can set "64 * 1024 * 1024 * 1024" to size.
//
// Note that if you have specified a pathname which already exists, this function
// returns os.ErrExist error. So you can handle it with os.IsExist function.
func CreateDiskImage(pathname string, size int64) error {
f, err := os.OpenFile(pathname, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600)
if err != nil {
return err
}
defer f.Close()
if err := f.Truncate(size); err != nil {
return err
}
return nil
}
// CreateSparseDiskImage is creating an "Apple Sparse Image Format" disk image
// with specified filename and filesize. The function "shells out" to diskutil, as currently
// this is the only known way of creating ASIF images.
// For example, if you want to create disk with 64GiB, you can set "64 * 1024 * 1024 * 1024" to size.
//
// Note that ASIF is only available from macOS Tahoe, so the function will return error
// on earlier versions.
func CreateSparseDiskImage(ctx context.Context, pathname string, size int64) error {
if err := macOSAvailable(26); err != nil {
return err
}
diskutil, err := exec.LookPath("diskutil")
if err != nil {
return fmt.Errorf("failed to find disktuil: %w", err)
}
sizeStr := strconv.FormatInt(size, 10)
cmd := exec.CommandContext(ctx,
diskutil,
"image",
"create",
"blank",
"--fs", "none",
"--format", "ASIF",
"--size", sizeStr,
pathname)
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to create ASIF disk image: %w", err)
}
return nil
}