|
| 1 | +package virtualbox |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + |
| 9 | + "github.com/docker/machine/libmachine/log" |
| 10 | + "github.com/docker/machine/libmachine/mcnutils" |
| 11 | +) |
| 12 | + |
| 13 | +type VirtualDisk struct { |
| 14 | + UUID string |
| 15 | + Path string |
| 16 | +} |
| 17 | + |
| 18 | +type DiskCreator interface { |
| 19 | + Create(size int, publicSSHKeyPath, diskPath string) error |
| 20 | +} |
| 21 | + |
| 22 | +func NewDiskCreator() DiskCreator { |
| 23 | + return &defaultDiskCreator{} |
| 24 | +} |
| 25 | + |
| 26 | +type defaultDiskCreator struct{} |
| 27 | + |
| 28 | +// Make a boot2docker VM disk image. |
| 29 | +func (c *defaultDiskCreator) Create(size int, publicSSHKeyPath, diskPath string) error { |
| 30 | + log.Debugf("Creating %d MB hard disk image...", size) |
| 31 | + |
| 32 | + tarBuf, err := mcnutils.MakeDiskImage(publicSSHKeyPath) |
| 33 | + if err != nil { |
| 34 | + return err |
| 35 | + } |
| 36 | + |
| 37 | + log.Debug("Calling inner createDiskImage") |
| 38 | + |
| 39 | + return createDiskImage(diskPath, size, tarBuf) |
| 40 | +} |
| 41 | + |
| 42 | +// createDiskImage makes a disk image at dest with the given size in MB. If r is |
| 43 | +// not nil, it will be read as a raw disk image to convert from. |
| 44 | +func createDiskImage(dest string, size int, r io.Reader) error { |
| 45 | + // Convert a raw image from stdin to the dest VMDK image. |
| 46 | + sizeBytes := int64(size) << 20 // usually won't fit in 32-bit int (max 2GB) |
| 47 | + // FIXME: why isn't this just using the vbm*() functions? |
| 48 | + cmd := exec.Command(vboxManageCmd, "convertfromraw", "stdin", dest, |
| 49 | + fmt.Sprintf("%d", sizeBytes), "--format", "VMDK") |
| 50 | + |
| 51 | + log.Debug(cmd) |
| 52 | + |
| 53 | + if os.Getenv("MACHINE_DEBUG") != "" { |
| 54 | + cmd.Stdout = os.Stdout |
| 55 | + cmd.Stderr = os.Stderr |
| 56 | + } |
| 57 | + |
| 58 | + stdin, err := cmd.StdinPipe() |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + log.Debug("Starting command") |
| 64 | + |
| 65 | + if err := cmd.Start(); err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + log.Debug("Copying to stdin") |
| 70 | + |
| 71 | + n, err := io.Copy(stdin, r) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + |
| 76 | + log.Debug("Filling zeroes") |
| 77 | + |
| 78 | + // The total number of bytes written to stdin must match sizeBytes, or |
| 79 | + // VBoxManage.exe on Windows will fail. Fill remaining with zeros. |
| 80 | + if left := sizeBytes - n; left > 0 { |
| 81 | + if err := zeroFill(stdin, left); err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + log.Debug("Closing STDIN") |
| 87 | + |
| 88 | + // cmd won't exit until the stdin is closed. |
| 89 | + if err := stdin.Close(); err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + |
| 93 | + log.Debug("Waiting on cmd") |
| 94 | + |
| 95 | + return cmd.Wait() |
| 96 | +} |
| 97 | + |
| 98 | +// zeroFill writes n zero bytes into w. |
| 99 | +func zeroFill(w io.Writer, n int64) error { |
| 100 | + const blocksize = 32 << 10 |
| 101 | + zeros := make([]byte, blocksize) |
| 102 | + var k int |
| 103 | + var err error |
| 104 | + for n > 0 { |
| 105 | + if n > blocksize { |
| 106 | + k, err = w.Write(zeros) |
| 107 | + } else { |
| 108 | + k, err = w.Write(zeros[:n]) |
| 109 | + } |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + n -= int64(k) |
| 114 | + } |
| 115 | + return nil |
| 116 | +} |
| 117 | + |
| 118 | +func getVMDiskInfo(name string, vbox VBoxManager) (*VirtualDisk, error) { |
| 119 | + out, err := vbox.vbmOut("showvminfo", name, "--machinereadable") |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + |
| 124 | + disk := &VirtualDisk{} |
| 125 | + |
| 126 | + err = parseKeyValues(out, reEqualQuoteLine, func(key, val string) error { |
| 127 | + switch key { |
| 128 | + case "SATA-1-0": |
| 129 | + disk.Path = val |
| 130 | + case "SATA-ImageUUID-1-0": |
| 131 | + disk.UUID = val |
| 132 | + } |
| 133 | + |
| 134 | + return nil |
| 135 | + }) |
| 136 | + if err != nil { |
| 137 | + return nil, err |
| 138 | + } |
| 139 | + |
| 140 | + return disk, nil |
| 141 | +} |
0 commit comments