github.com/MerlinKodo/gvisor@v0.0.0-20231110090155-957f62ecf90e/runsc/boot/nvidia.go (about)

     1  // Copyright 2023 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package boot
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  	"strings"
    21  )
    22  
    23  // NvidiaDevMinors can be used to pass nvidia device minors via flags.
    24  type NvidiaDevMinors []uint32
    25  
    26  // String implements flag.Value.
    27  func (n *NvidiaDevMinors) String() string {
    28  	minors := make([]string, 0, len(*n))
    29  	for _, minor := range *n {
    30  		minors = append(minors, strconv.Itoa(int(minor)))
    31  	}
    32  	return strings.Join(minors, ",")
    33  }
    34  
    35  // Get implements flag.Value.
    36  func (n *NvidiaDevMinors) Get() any {
    37  	return n
    38  }
    39  
    40  // Set implements flag.Value and appends a device minor from the command
    41  // line to the device minors array. Set(String()) should be idempotent.
    42  func (n *NvidiaDevMinors) Set(s string) error {
    43  	minors := strings.Split(s, ",")
    44  	for _, minor := range minors {
    45  		minorVal, err := strconv.Atoi(minor)
    46  		if err != nil {
    47  			return fmt.Errorf("invalid device minor value (%d): %v", minorVal, err)
    48  		}
    49  		*n = append(*n, uint32(minorVal))
    50  	}
    51  	return nil
    52  }