lxd/container_lxc.go
func (c *containerLXC) setupUnixDevice(prefix string, dev types.Device, major int, minor int, path string, createMustSucceed bool, defaultMode bool) error {
	if c.isCurrentlyPrivileged() && !c.state.OS.RunningInUserNS && c.state.OS.CGroupDevicesController {
		err := lxcSetConfigItem(c.c, "lxc.cgroup.devices.allow", fmt.Sprintf("c %d:%d rwm", major, minor))
		if err != nil {
			return err
		}
	}

	temp := types.Device{}
	err := shared.DeepCopy(&dev, &temp)
	if err != nil {
		return err
	}

	temp["major"] = fmt.Sprintf("%d", major)
	temp["minor"] = fmt.Sprintf("%d", minor)
	temp["path"] = path

	paths, err := c.createUnixDevice(prefix, temp, defaultMode)
	if err != nil {
		logger.Debug("Failed to create device", log.Ctx{"err": err, "device": prefix})
		if createMustSucceed {
			return err
		}

		return nil
	}

	devPath := shared.EscapePathFstab(paths[0])
	tgtPath := shared.EscapePathFstab(paths[1])
	val := fmt.Sprintf("%s %s none bind,create=file 0 0", devPath, tgtPath)

	return lxcSetConfigItem(c.c, "lxc.mount.entry", val)
}
func (c *containerLXC) setupUnixDevice(prefix string, dev types.Device, major int, minor int, path string, createMustSucceed bool, defaultMode bool) error {
	if c.isCurrentlyPrivileged() && !c.state.OS.RunningInUserNS && c.state.OS.CGroupDevicesController {
		err := lxcSetConfigItem(c.c, "lxc.cgroup.devices.allow", fmt.Sprintf("c %d:%d rwm", major, minor))
		if err != nil {
			return err
		}
	}

	temp := types.Device{}
	err := shared.DeepCopy(&dev, &temp)
	if err != nil {
		return err
	}

	temp["major"] = fmt.Sprintf("%d", major)
	temp["minor"] = fmt.Sprintf("%d", minor)
	temp["path"] = path

	paths, err := c.createUnixDevice(prefix, temp, defaultMode)
	if err != nil {
		logger.Debug("Failed to create device", log.Ctx{"err": err, "device": prefix})
		if createMustSucceed {
			return err
		}

		return nil
	}

	devPath := shared.EscapePathFstab(paths[0])
	tgtPath := shared.EscapePathFstab(paths[1])
	val := fmt.Sprintf("%s %s none bind,create=file 0 0", devPath, tgtPath)

	return lxcSetConfigItem(c.c, "lxc.mount.entry", val)
}
// setupUnixDevice() creates the unix device and sets up the necessary low-level
// liblxc configuration items.