// Device transforms a libcontainer configs.Device to a specs.LinuxDevice object.
164759
func Device(d *configs.Device) specs.LinuxDevice {
	return specs.LinuxDevice{
		Type:     string(d.Type),
		Path:     d.Path,
		Major:    d.Major,
		Minor:    d.Minor,
		FileMode: fmPtr(int64(d.FileMode)),
		UID:      u32Ptr(int64(d.Uid)),
		GID:      u32Ptr(int64(d.Gid)),
	}
}
103135
func (devices *DeviceSet) registerDevice(id int, hash string, size uint64, transactionID uint64) (*devInfo, error) {
	logrus.Debugf("devmapper: registerDevice(%v, %v)", id, hash)
	info := &devInfo{
		Hash:          hash,
		DeviceID:      id,
		Size:          size,
		TransactionID: transactionID,
		Initialized:   false,
		devices:       devices,
	}

	devices.Devices[hash] = info

	if err := devices.saveMetadata(info); err != nil {
		// Try to remove unused device
		delete(devices.Devices, hash)
		return nil, err
	}

	return info, nil
}
1052049
func (d *DeviceMapperDriver) addDevice(headDeviceHash string) (string, error) {

	// Generate the device ID.
	deviceHash, err := utils.NewUUID62()
	if err != nil {
		return "", err
	}

	// Create the device.
	if err := d.DeviceSet.AddDevice(deviceHash, headDeviceHash); err != nil {
		glog.Errorf("Unable to create device %s: %s", deviceHash, err)
		return "", err
	}

	if headDeviceHash == "" {

		// Activate the device.
		d.DeviceSet.GetDeviceStatus(deviceHash)

		// CC-2219: Ensure the device base size is equal or greater than the
		// configured dm.basesize.
		size, err := d.deviceSize(deviceHash)
		if err != nil {
			glog.Errorf("Could not read the size of device %s: %s", deviceHash, err)
			return "", err
		}
		if baseSize := d.baseSize(); baseSize > size {

			// Make the device to match dm.basesize
			glog.V(2).Infof("Device size is smaller than dm.basesize; expanding")
			if err := d.resize(deviceHash, baseSize); err != nil {
				glog.Errorf("Could not update the size of the device %s: %s", deviceHash, err)
				return "", err
			}
		}
	}

	return deviceHash, nil
}
116630
func NewDeviceAdd(g *libkb.GlobalContext) *DeviceAdd {
	return &DeviceAdd{
		Contextified: libkb.NewContextified(g),
	}
}
1206004
func parseDevice(device, serverOS string) (container.DeviceMapping, error) {
	switch serverOS {
	case "linux":
		return parseLinuxDevice(device)
	case "windows":
		return parseWindowsDevice(device)
	}
	return container.DeviceMapping{}, errors.Errorf("unknown server OS: %s", serverOS)
}
1241425
func (g *Generator) AddLinuxResourcesDevice(allow bool, devType string, major, minor *int64, access string) {
	g.initConfigLinuxResources()

	device := rspec.LinuxDeviceCgroup{
		Allow:  allow,
		Type:   devType,
		Access: access,
		Major:  major,
		Minor:  minor,
	}
	g.Config.Linux.Resources.Devices = append(g.Config.Linux.Resources.Devices, device)
}
576052
func convertProtoDeviceSpec(in *proto.DeviceSpec) *DeviceSpec {
	if in == nil {
		return nil
	}

	return &DeviceSpec{
		TaskPath:    in.TaskPath,
		HostPath:    in.HostPath,
		CgroupPerms: in.Permissions,
	}
}
826413
func (devices *DeviceSet) lookupDevice(hash string) (*devInfo, error) {
	info := devices.Devices[hash]
	if info == nil {
		info = devices.loadMetadata(hash)
		if info == nil {
			return nil, fmt.Errorf("devmapper: Unknown device %s", hash)
		}

		devices.Devices[hash] = info
	}
	return info, nil
}
1034417
func DeviceFromPath(path, permissions string) (*configs.Device, error) {
	var stat unix.Stat_t
	err := unixLstat(path, &stat)
	if err != nil {
		return nil, err
	}

	var (
		devNumber = uint64(stat.Rdev)
		major     = unix.Major(devNumber)
		minor     = unix.Minor(devNumber)
	)
	if major == 0 {
		return nil, ErrNotADevice
	}

	var (
		devType rune
		mode    = stat.Mode
	)
	switch {
	case mode&unix.S_IFBLK == unix.S_IFBLK:
		devType = 'b'
	case mode&unix.S_IFCHR == unix.S_IFCHR:
		devType = 'c'
	}
	return &configs.Device{
		Type:        devType,
		Path:        path,
		Major:       int64(major),
		Minor:       int64(minor),
		Permissions: permissions,
		FileMode:    os.FileMode(mode),
		Uid:         stat.Uid,
		Gid:         stat.Gid,
	}, nil
}
56190
func (d *driver) NextDevice(
	ctx types.Context,
	opts types.Store) (string, error) {

	return "", types.ErrNotImplemented
}
183065
func DeviceHandleByIndex(idx uint) (Device, error) {
	if C.nvmlHandle == nil {
		return Device{}, errLibraryNotLoaded
	}
	var dev C.nvmlDevice_t
	r := C.nvmlDeviceGetHandleByIndex(C.uint(idx), &dev)
	return Device{dev}, errorString(r)
}
153264
func (s *DeviceEvent) SetDevice(v *Device) *DeviceEvent {
	s.Device = v
	return s
}
1365078
func (u *storageosUtil) DeviceDir(b *storageosMounter) string {

	ctrl, err := u.api.Node(b.plugin.host.GetHostName())
	if err != nil {
		klog.Warningf("node device path lookup failed: %v", err)
		return defaultDeviceDir
	}
	if ctrl == nil || ctrl.DeviceDir == "" {
		klog.Warningf("node device path not set, using default: %s", defaultDeviceDir)
		return defaultDeviceDir
	}
	return ctrl.DeviceDir
}
550658
func (_m *DBClient) AddDevice(d models.Device) (string, error) {
	ret := _m.Called(d)

	var r0 string
	if rf, ok := ret.Get(0).(func(models.Device) string); ok {
		r0 = rf(d)
	} else {
		r0 = ret.Get(0).(string)
	}

	var r1 error
	if rf, ok := ret.Get(1).(func(models.Device) error); ok {
		r1 = rf(d)
	} else {
		r1 = ret.Error(1)
	}

	return r0, r1
}
131248
func (devices *DeviceSet) removeDevice(devname string) error {
	var err error

	logrus.WithField("storage-driver", "devicemapper").Debugf("removeDevice START(%s)", devname)
	defer logrus.WithField("storage-driver", "devicemapper").Debugf("removeDevice END(%s)", devname)

	for i := 0; i < 200; i++ {
		err = devicemapper.RemoveDevice(devname)
		if err == nil {
			break
		}
		if err != devicemapper.ErrBusy {
			return err
		}

		// If we see EBUSY it may be a transient error,
		// sleep a bit a retry a few times.
		devices.Unlock()
		time.Sleep(100 * time.Millisecond)
		devices.Lock()
	}

	return err
}
1255928
func (s *RemoteAccessSession) SetDevice(v *Device) *RemoteAccessSession {
	s.Device = v
	return s
}
438771
func (c *Connect) DeviceCreateXML(xmlConfig string, flags uint32) (*NodeDevice, error) {
	cXml := C.CString(string(xmlConfig))
	defer C.free(unsafe.Pointer(cXml))
	var err C.virError
	ptr := C.virNodeDeviceCreateXMLWrapper(c.ptr, cXml, C.uint(flags), &err)
	if ptr == nil {
		return nil, makeError(&err)
	}
	return &NodeDevice{ptr: ptr}, nil
}