| 112 | } |
| 113 | |
| 114 | func (vs *Set) createDiskImage(ctx context.Context, size int64) (path string, retErr error) { |
| 115 | f, err := os.CreateTemp(vs.tempDir, "createDiskImage") |
| 116 | if err != nil { |
| 117 | retErr = err |
| 118 | return |
| 119 | } |
| 120 | defer func() { |
| 121 | // The file must be closed even in the success case. |
| 122 | err := f.Close() |
| 123 | if err != nil { |
| 124 | retErr = multierror.Append(retErr, err) |
| 125 | } |
| 126 | // But the file must not be deleted in the success case. |
| 127 | if retErr != nil { |
| 128 | err = os.Remove(path) |
| 129 | if err != nil { |
| 130 | retErr = multierror.Append(retErr, err) |
| 131 | } |
| 132 | } |
| 133 | }() |
| 134 | |
| 135 | err = f.Truncate(size) |
| 136 | if err != nil { |
| 137 | retErr = err |
| 138 | return |
| 139 | } |
| 140 | |
| 141 | out, err := exec.CommandContext(ctx, "mkfs."+fsType, "-F", f.Name()).CombinedOutput() |
| 142 | if err != nil { |
| 143 | retErr = fmt.Errorf("failed to execute mkfs.%s: %s: %w", fsType, out, err) |
| 144 | return |
| 145 | } |
| 146 | |
| 147 | path = f.Name() |
| 148 | return |
| 149 | } |
| 150 | |
| 151 | func mountDiskImage(source, target string) error { |
| 152 | return mount.All([]mount.Mount{{Type: fsType, Source: source, Options: []string{"loop"}}}, target) |