-
Notifications
You must be signed in to change notification settings - Fork 0
/
highsierra.go
143 lines (121 loc) · 3.68 KB
/
highsierra.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
package mic
import (
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"strings"
"github.com/stephen-fox/mic/internal/createinstallmediautil"
"github.com/stephen-fox/mic/internal/hdiutilw"
)
type HighSierra struct {
name string
isLoggingEnabled bool
}
func (o HighSierra) Name() string {
return o.name
}
func (o *HighSierra) SetLogging(enabled bool) {
o.isLoggingEnabled = enabled
}
func (o HighSierra) CreateIso(isoDestinationPath string, installerApplicationPath string) error {
cim, err := createinstallmediautil.Get(installerApplicationPath + "/Contents/Resources/createinstallmedia")
if err != nil {
return err
}
// Note: Creating individual temp files and removing them
// seems to cause some kind of file system race. It was
// unnecessary to begin with - but "Resource temporarily
// unavailable" errors sealed the deal.
tempDirPath, err := ioutil.TempDir("", "macos-installer-")
if err != nil {
return errors.New("Failed to create empty .cdr file - " + err.Error())
}
installerAppSizeBytes, err := dirSizeBytes(installerApplicationPath)
if err != nil {
return fmt.Errorf("failed to get installer application size - %s", err.Error())
}
// TODO: Used to be 500 mb - now 5gb.
//
// Investigate auto-sizing based on hdiutil error:
// Failed to create .dmg image - Failed to execute command:
// '.../createinstallmedia --volume /tmp/mount-cdr-12345 --nointeraction'. Output:
// /tmp/mount-cdr-12345 is not large enough for install media. An additional 1.34 GB is needed.
installerAppSizeBytes += 5000000000
if o.isLoggingEnabled {
log.Printf("Creating empty %d gb .cdr...", installerAppSizeBytes/1000000000)
}
cdrFinalFilePath, err := hdiutilw.CreateCdr(path.Join(tempDirPath, "cdr"), installerAppSizeBytes)
if err != nil {
return err
}
defer os.Remove(cdrFinalFilePath)
cdrMountPath, err := ioutil.TempDir("", "mount-cdr-")
if err != nil {
return errors.New("Failed to create .cdr mount point - " + err.Error())
}
err = hdiutilw.Attach(cdrFinalFilePath, cdrMountPath)
if err != nil {
return errors.New("Failed to mount .cdr - " + err.Error())
}
defer hdiutilw.Detach(cdrMountPath)
if o.isLoggingEnabled {
log.Println("Writing installer files to image...")
}
ejectInstallVolumeFn := func() {
const volumesPath = "/Volumes"
infos, err := ioutil.ReadDir(volumesPath)
if err != nil {
if o.isLoggingEnabled {
log.Printf("unable to unmount installer volume - failed to read volumes directory - %s",
err.Error())
}
return
}
installerAppName := strings.TrimSuffix(path.Base(installerApplicationPath), ".app")
for _, info := range infos {
if strings.Contains(info.Name(), installerAppName) {
installVolume := path.Join(volumesPath, info.Name())
err := hdiutilw.ForceEject(installVolume)
if err != nil && o.isLoggingEnabled {
log.Printf("failed to detach installer volume '%s' - %s",
installVolume, err.Error())
}
}
}
}
err = cim.CreateDmg(cdrMountPath)
ejectInstallVolumeFn()
if err != nil {
return errors.New("Failed to create .dmg image - " + err.Error())
}
if o.isLoggingEnabled {
log.Println("Converting image to an .iso...")
}
isoFinalFilePath, err := hdiutilw.ConvertImageToIso(cdrFinalFilePath, path.Join(tempDirPath, "installer-iso"))
if err != nil {
return err
}
err = os.Rename(isoFinalFilePath, isoDestinationPath)
if err != nil {
return err
}
return nil
}
// https://stackoverflow.com/a/32482941
func dirSizeBytes(path string) (int64, error) {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
size += info.Size()
}
return err
})
return size, err
}