ParseDevice parses the give device string into hostDevPath, containerPath and mode(defaults: "rwm").
(s string)
| 267 | |
| 268 | // ParseDevice parses the give device string into hostDevPath, containerPath and mode(defaults: "rwm"). |
| 269 | func ParseDevice(s string) (hostDevPath string, containerPath string, mode string, err error) { |
| 270 | mode = "rwm" |
| 271 | split := strings.Split(s, ":") |
| 272 | var containerDevPath string |
| 273 | switch len(split) { |
| 274 | case 1: // e.g. "/dev/sda1" |
| 275 | hostDevPath = split[0] |
| 276 | containerDevPath = hostDevPath |
| 277 | case 2: // e.g., "/dev/sda1:rwm", or "/dev/sda1:/dev/sda1 |
| 278 | hostDevPath = split[0] |
| 279 | if !strings.Contains(split[1], "/") { |
| 280 | containerDevPath = hostDevPath |
| 281 | mode = split[1] |
| 282 | } else { |
| 283 | containerDevPath = split[1] |
| 284 | } |
| 285 | case 3: // e.g., "/dev/sda1:/dev/sda1:rwm" |
| 286 | hostDevPath = split[0] |
| 287 | containerDevPath = split[1] |
| 288 | mode = split[2] |
| 289 | default: |
| 290 | return "", "", "", errors.New("too many `:` symbols") |
| 291 | } |
| 292 | |
| 293 | if !filepath.IsAbs(hostDevPath) { |
| 294 | return "", "", "", fmt.Errorf("%q is not an absolute path", hostDevPath) |
| 295 | } |
| 296 | |
| 297 | if err := validateDeviceMode(mode); err != nil { |
| 298 | return "", "", "", err |
| 299 | } |
| 300 | return hostDevPath, containerDevPath, mode, nil |
| 301 | } |
| 302 | |
| 303 | func validateDeviceMode(mode string) error { |
| 304 | for _, r := range mode { |
searching dependent graphs…