1
2
3 package lcow
4
5 import (
6 "bytes"
7 "context"
8 "fmt"
9 "time"
10
11 cmdpkg "github.com/Microsoft/hcsshim/internal/cmd"
12 "github.com/Microsoft/hcsshim/internal/log"
13 "github.com/Microsoft/hcsshim/internal/timeout"
14 "github.com/Microsoft/hcsshim/internal/uvm"
15 "github.com/sirupsen/logrus"
16 )
17
18
19
20 func formatDiskUvm(ctx context.Context, lcowUVM *uvm.UtilityVM, controller int, lun int32, destPath string) error {
21
22 devicePath := fmt.Sprintf("/sys/bus/scsi/devices/%d:0:0:%d/block", controller, lun)
23 testdCtx, cancel := context.WithTimeout(ctx, timeout.TestDRetryLoop)
24 defer cancel()
25 for {
26 cmd := cmdpkg.CommandContext(testdCtx, lcowUVM, "test", "-d", devicePath)
27 err := cmd.Run()
28 if err == nil {
29 break
30 }
31 if _, ok := err.(*cmdpkg.ExitError); !ok {
32 return fmt.Errorf("failed to run %+v following hot-add %s to utility VM: %s", cmd.Spec.Args, destPath, err)
33 }
34 time.Sleep(time.Millisecond * 10)
35 }
36 cancel()
37
38
39 lsCtx, cancel := context.WithTimeout(ctx, timeout.ExternalCommandToStart)
40 cmd := cmdpkg.CommandContext(lsCtx, lcowUVM, "ls", devicePath)
41 lsOutput, err := cmd.Output()
42 cancel()
43 if err != nil {
44 return fmt.Errorf("failed to `%+v` following hot-add %s to utility VM: %s", cmd.Spec.Args, destPath, err)
45 }
46 device := fmt.Sprintf(`/dev/%s`, bytes.TrimSpace(lsOutput))
47 log.G(ctx).WithFields(logrus.Fields{
48 "dest": destPath,
49 "device": device,
50 }).Debug("lcow::FormatDisk device guest location")
51
52
53 mkfsCtx, cancel := context.WithTimeout(ctx, timeout.ExternalCommandToStart)
54 cmd = cmdpkg.CommandContext(mkfsCtx, lcowUVM, "mkfs.ext4", "-q", "-E", "lazy_itable_init=0,nodiscard", "-O", `^has_journal,sparse_super2,^resize_inode`, device)
55 var mkfsStderr bytes.Buffer
56 cmd.Stderr = &mkfsStderr
57 err = cmd.Run()
58 cancel()
59 if err != nil {
60 return fmt.Errorf("failed to `%+v` following hot-add %s to utility VM: %s. detailed error: %s", cmd.Spec.Args, destPath, err, mkfsStderr.String())
61 }
62
63 log.G(ctx).WithField("dest", destPath).Debug("lcow::FormatDisk complete")
64
65 return nil
66 }
67
View as plain text