MCPcopy Create free account
hub / github.com/containerd/nerdctl / ParseDevice

Function ParseDevice

pkg/cmd/container/run_cgroup_linux.go:269–301  ·  view source on GitHub ↗

ParseDevice parses the give device string into hostDevPath, containerPath and mode(defaults: "rwm").

(s string)

Source from the content-addressed store, hash-verified

267
268// ParseDevice parses the give device string into hostDevPath, containerPath and mode(defaults: "rwm").
269func 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
303func validateDeviceMode(mode string) error {
304 for _, r := range mode {

Callers 2

TestParseDeviceFunction · 0.92
generateCgroupOptsFunction · 0.85

Calls 1

validateDeviceModeFunction · 0.85

Tested by 1

TestParseDeviceFunction · 0.74

Used in the wild real call sites across dependent graphs

searching dependent graphs…