-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisk.go
393 lines (357 loc) · 7.99 KB
/
disk.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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package main
import (
"bufio"
"bytes"
"compress/gzip"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"crypto/sha512"
"crypto/tls"
"fmt"
"hash"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"runtime"
"strings"
"time"
"github.com/biogo/hts/bgzf"
"github.com/cheggaaa/pb"
"github.com/cloudflare/golibs/bytepool"
pgzip "github.com/klauspost/pgzip"
"github.com/vtolstov/go-ioctl"
compress "github.com/vtolstov/packer-post-processor-compress/compress"
"gopkg.in/yaml.v2"
)
func getHash(t string) hash.Hash {
var h hash.Hash
switch t {
case "md5":
h = md5.New()
case "sha1":
h = sha1.New()
case "sha224":
h = sha256.New224()
case "sha256":
h = sha256.New()
case "sha384":
h = sha512.New384()
case "sha512":
h = sha512.New()
}
return h
}
type zeroSkipWriter struct {
w io.Writer
bp *bytepool.BytePool
pos int64
zero []byte
}
const (
bsize = 4096
)
func ZeroSkipWriter(w io.Writer) io.WriteCloser {
zsw := &zeroSkipWriter{w: w}
zsw.bp = new(bytepool.BytePool)
zsw.bp.Init(time.Second, 65536)
zsw.zero = zsw.bp.Get(bsize)
return zsw
}
func (w *zeroSkipWriter) Write(p []byte) (n int, err error) {
l := len(p)
bcount := 0
start := 0
if l > bsize {
bcount = l / bsize
}
for i := 0; i < bcount; i++ {
if bytes.Equal(p[start:start+bsize], w.zero) {
_, err = w.w.(io.WriteSeeker).Seek(w.pos+int64(bsize), os.SEEK_SET)
if err != nil {
return 0, err
}
} else {
n, err = w.w.Write(p[start : start+bsize])
if err != nil {
return
}
if n != bsize {
err = io.ErrShortWrite
return
}
}
w.pos += int64(bsize)
start += bsize
}
if l > start {
n, err = w.w.Write(p[start:])
if err != nil {
return
}
if n != len(p[start:]) {
err = io.ErrShortWrite
return
}
w.pos += int64(n)
}
return l, nil
}
func (w *zeroSkipWriter) Close() error {
w.bp.Put(w.zero)
w.bp.Close()
return nil
}
func copyImage(img string, dev string, fetchaddrs []string) (err error) {
var gr io.ReadCloser
var h hash.Hash
var checksum string
var mw io.Writer
var bar *pb.ProgressBar
// var n int64
httpTransport := &http.Transport{
Dial: (&net.Dialer{DualStack: true}).Dial,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
httpClient := &http.Client{Transport: httpTransport}
var host string
var port string
var src string
for _, fetchaddr := range fetchaddrs {
src = fmt.Sprintf("%s/%s", fetchaddr, img)
u, err := url.Parse(src)
if err != nil {
if debug {
fmt.Printf("url err: %s", err)
}
continue
}
if !strings.HasPrefix(u.Host, "[") && strings.Index(u.Host, ":") > 0 {
host, port, _ = net.SplitHostPort(u.Host)
} else {
host = u.Host
}
if port == "" {
if u.Scheme == "https" {
port = "443"
} else {
port = "80"
}
}
if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
host = host[1 : len(host)-1]
}
addrs, err := net.LookupIP(host)
if err != nil {
addrs = []net.IP{net.ParseIP(host)}
err = nil
}
for _, addr := range addrs {
if ipv4 && addr.To4() == nil {
continue
}
if ipv6 && addr.To4() != nil {
continue
}
req, _ := http.NewRequest("HEAD", src, nil)
req.Header.Set("Host", host)
req.URL = u
req.URL.Host = net.JoinHostPort(addr.String(), port)
req.Host = host
res, err := httpClient.Do(req)
if err != nil || res.StatusCode != 200 {
if debug {
err = fmt.Errorf("failed to fetch image %s", req)
fmt.Printf("http err: %s\n", err)
time.Sleep(5 * time.Second)
}
continue
}
for _, ct := range []string{"md5", "sha1", "sha244", "sha256", "sha384", "sha512"} {
csumurl := fmt.Sprintf("%s/%s.%ssums", fetchaddr, img, ct)
cu, err := url.Parse(csumurl)
if err != nil {
if debug {
fmt.Printf("url err: %s", err)
time.Sleep(5 * time.Second)
}
continue
}
req.URL = cu
req.Method = "GET"
res, err = httpClient.Do(req)
if err == nil && res.StatusCode == 200 && res.Body != nil {
rd := bufio.NewReader(res.Body)
lines:
for {
line, err := rd.ReadString('\n')
if err != nil {
break lines
}
parts := strings.Fields(line)
if len(parts) > 1 {
if parts[1] == img {
checksum = parts[0]
break lines
}
}
}
res.Body.Close()
h = getHash(ct)
}
}
metaurl := fmt.Sprintf("%s/%s.metadata", fetchaddr, img)
mu, err := url.Parse(metaurl)
if err != nil {
if debug {
fmt.Printf("url err: %s", err)
time.Sleep(5 * time.Second)
}
}
meta := make(compress.Metadata, 0)
req.Method = "GET"
req.URL = mu
res, err = httpClient.Do(req)
if err == nil && res.StatusCode == 200 && res.Body != nil {
metaBody, _ := ioutil.ReadAll(res.Body)
res.Body.Close()
if err = yaml.Unmarshal(metaBody, &meta); err != nil {
fmt.Printf("metadata err %s\n", err.Error())
}
} else {
if debug && err != nil {
fmt.Printf("meta: %s\n", err.Error())
time.Sleep(20 * time.Second)
}
}
if debug {
fmt.Printf("meta %+v\n", meta)
time.Sleep(5 * time.Second)
}
if len(meta) > 0 {
if m, ok := meta[img]; ok {
if m.OrigSize != 0 {
bar = pb.New64(m.OrigSize)
bar.ShowSpeed = true
bar.ShowTimeLeft = true
bar.ShowPercent = true
bar.SetRefreshRate(time.Second)
bar.SetWidth(80)
bar.SetMaxWidth(80)
bar.SetUnits(pb.U_BYTES)
bar.Start()
defer bar.Finish()
}
}
}
req.Method = "GET"
req.URL = u
res, err = httpClient.Do(req)
if err != nil || res.StatusCode != 200 {
if debug {
err = fmt.Errorf("failed to fetch image %s", req)
fmt.Printf("http err: %s\n", err)
time.Sleep(10 * time.Second)
}
continue
}
/*
rf := &ranger.HTTPRanger{URL: u}
if err = rf.Initialize(0); err != nil {
fmt.Printf("rf err %s\n", err.Error())
continue
}
*/
rs := res.Body
defer res.Body.Close()
fw, err := os.OpenFile(dev, os.O_WRONLY, 0600)
if err != nil {
fmt.Printf("open err: %s\n", err)
time.Sleep(10 * time.Second)
return err
}
defer fw.Close()
zsw := ZeroSkipWriter(fw)
defer zsw.Close()
//TODO: check for error
comptype := ""
if len(meta) > 0 {
comptype = meta[img].CompType
}
pr, pw := io.Pipe()
var cmw io.Writer
if checksum != "" {
cmw = io.MultiWriter(pw, h)
} else {
cmw = io.MultiWriter(pw)
}
go func() {
//TODO: chck for error
_, err := io.Copy(cmw, rs)
if err != nil {
fmt.Printf("copy err: %s\n", err)
time.Sleep(10 * time.Second)
}
err = pw.Close()
if err != nil {
fmt.Printf("close err: %s\n", err)
time.Sleep(10 * time.Second)
}
}()
switch comptype {
case "bgzf":
gr, err = bgzf.NewReader(pr, runtime.NumCPU())
case "pgzip":
gr, err = pgzip.NewReader(pr)
case "gzip":
gr, err = gzip.NewReader(pr)
default:
if gr, err = pgzip.NewReader(pr); err != nil {
if gr, err = bgzf.NewReader(pr, runtime.NumCPU()); err != nil {
if gr, err = gzip.NewReader(pr); err != nil {
fmt.Printf("gz error: %s\n", err)
return err
}
}
}
}
if err != nil {
return err
}
defer gr.Close()
writers := []io.Writer{zsw}
if len(meta) > 0 {
if m, ok := meta[img]; ok && m.OrigSize != 0 {
writers = append(writers, bar)
}
}
mw = io.MultiWriter(writers...)
io.Copy(mw, gr)
if checksum != "" {
if checksum != fmt.Sprintf("%x", h.Sum(nil)) {
err = fmt.Errorf("checksum mismatch %s != %s", checksum, fmt.Sprintf("%x", h.Sum(nil)))
if debug {
fmt.Printf("%s\n", err.Error())
time.Sleep(10 * time.Second)
}
return err
} else {
fmt.Printf("checksum ok %s == %s\n", checksum, fmt.Sprintf("%x", h.Sum(nil)))
}
}
return nil
}
}
return fmt.Errorf("failed to fetch image %s", err.Error())
}
func blkpart(dst string) error {
w, err := os.OpenFile(dst, os.O_WRONLY, 0600)
if err != nil {
return err
}
defer w.Close()
return ioctl.BlkRRPart(w.Fd())
}