-
Notifications
You must be signed in to change notification settings - Fork 19
/
prepareForUnpack.go
179 lines (171 loc) · 4.71 KB
/
prepareForUnpack.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
package unpacker
import (
"errors"
"fmt"
"os"
"path/filepath"
"time"
"github.com/Symantec/Dominator/lib/filesystem"
"github.com/Symantec/Dominator/lib/filesystem/scanner"
"github.com/Symantec/Dominator/lib/format"
"github.com/Symantec/Dominator/lib/objectcache"
"github.com/Symantec/Dominator/lib/wsyscall"
proto "github.com/Symantec/Dominator/proto/imageunpacker"
)
func (u *Unpacker) prepareForUnpack(streamName string, skipIfPrepared bool,
doNotWaitForResult bool) error {
u.rwMutex.Lock()
u.updateUsageTimeWithLock()
streamInfo, err := u.setupStream(streamName)
u.rwMutex.Unlock()
defer u.updateUsageTime()
if err != nil {
return err
}
errorChannel := make(chan error)
request := requestType{
request: requestScan,
skipIfPrepared: skipIfPrepared,
errorChannel: errorChannel,
}
streamInfo.requestChannel <- request
if doNotWaitForResult {
go func() {
<-errorChannel
}()
return nil
}
return <-errorChannel
}
func (stream *streamManagerState) scan(skipIfPrepared bool) error {
if err := stream.getDevice(); err != nil {
return err
}
streamInfo := stream.streamInfo
if streamInfo.status == proto.StatusStreamNoFileSystem {
return nil // Nothing to scan.
}
mountPoint := filepath.Join(stream.unpacker.baseDir, "mnt")
if err := stream.mount(mountPoint); err != nil {
return err
}
switch streamInfo.status {
case proto.StatusStreamNoDevice:
return errors.New("no device")
case proto.StatusStreamNotMounted:
return errors.New("not mounted")
case proto.StatusStreamMounted:
// Start scanning.
case proto.StatusStreamScanning:
return errors.New("stream scan in progress")
case proto.StatusStreamScanned:
if skipIfPrepared {
return nil
}
// Start scanning.
case proto.StatusStreamFetching:
return errors.New("fetch in progress")
case proto.StatusStreamUpdating:
return errors.New("update in progress")
case proto.StatusStreamPreparing:
return errors.New("preparing to capture")
default:
panic("invalid status")
}
streamInfo.status = proto.StatusStreamScanning
stream.unpacker.logger.Printf("Initiating scan(%s)\n", stream.streamName)
startTime := time.Now()
var err error
stream.fileSystem, err = stream.scanFS(mountPoint)
if err != nil {
return err
}
stream.objectCache, err = objectcache.ScanObjectCache(
filepath.Join(stream.unpacker.baseDir, "mnt", ".subd", "objects"))
if err != nil {
return err
}
streamInfo.status = proto.StatusStreamScanned
stream.unpacker.logger.Printf("Scanned(%s) in %s\n",
stream.streamName, format.Duration(time.Since(startTime)))
return nil
}
func (stream *streamManagerState) scanFS(mountPoint string) (
*filesystem.FileSystem, error) {
sfs, err := scanner.ScanFileSystem(mountPoint, nil, nil, nil,
scanner.GetSimpleHasher(false), nil)
if err != nil {
return nil, err
}
fs := &sfs.FileSystem
if err := fs.RebuildInodePointers(); err != nil {
return nil, err
}
fs.BuildEntryMap()
return fs, nil
}
func (stream *streamManagerState) getDevice() error {
u := stream.unpacker
u.rwMutex.Lock()
defer u.rwMutex.Unlock()
return stream.getDeviceWithLock()
}
func (stream *streamManagerState) getDeviceWithLock() error {
u := stream.unpacker
streamInfo := stream.streamInfo
if streamInfo.DeviceId != "" {
return nil
}
// Search for unused device.
for deviceId, deviceInfo := range u.pState.Devices {
if deviceInfo.StreamName == "" {
deviceInfo.StreamName = stream.streamName
u.pState.Devices[deviceId] = deviceInfo
streamInfo.DeviceId = deviceId
streamInfo.status = proto.StatusStreamNoFileSystem
stream.rootLabel = ""
if err := u.writeStateWithLock(); err != nil {
return err
}
break
}
}
if streamInfo.DeviceId == "" {
return errors.New("no available device")
}
return nil
}
func (stream *streamManagerState) mount(mountPoint string) error {
streamInfo := stream.streamInfo
switch streamInfo.status {
case proto.StatusStreamNoDevice:
panic("no device")
case proto.StatusStreamNotMounted:
// Not mounted: go ahead and mount.
case proto.StatusStreamNoFileSystem:
panic("no file-system")
default:
// Already mounted.
return nil
}
stream.unpacker.rwMutex.RLock()
device := stream.unpacker.pState.Devices[stream.streamInfo.DeviceId]
stream.unpacker.rwMutex.RUnlock()
rootDevice, err := getPartition(filepath.Join("/dev", device.DeviceName))
if err != nil {
return err
}
err = wsyscall.Mount(rootDevice, mountPoint, "ext4", 0, "")
if err != nil {
return fmt.Errorf("error mounting: %s onto: %s: %s", rootDevice,
mountPoint, err)
}
err = os.MkdirAll(filepath.Join(mountPoint, ".subd", "objects"), dirPerms)
if err != nil {
return err
}
streamInfo.status = proto.StatusStreamMounted
stream.unpacker.logger.Printf("Mounted(%s) %s\n",
stream.streamName, rootDevice)
return nil
}