func (devices *DeviceSet) initDevmapper(doInit bool) error { if os.Getenv("DEBUG") != "" { devicemapper.LogInitVerbose(devicemapper.LogLevelDebug) } else { devicemapper.LogInitVerbose(devicemapper.LogLevelWarn) } // give ourselves to libdm as a log handler devicemapper.LogInit(devices) _, err := devicemapper.GetDriverVersion() if err != nil { // Can't even get driver version, assume not supported return graphdriver.ErrNotSupported } // https://github.com/docker/docker/issues/4036 if supported := devicemapper.UdevSetSyncSupport(true); !supported { log.Warnf("WARNING: Udev sync is not supported. This will lead to unexpected behavior, data loss and errors") } log.Debugf("devicemapper: udev sync support: %v", devicemapper.UdevSyncSupported()) if err := os.MkdirAll(devices.metadataDir(), 0700); err != nil && !os.IsExist(err) { return err } // Set the device prefix from the device id and inode of the docker root dir st, err := os.Stat(devices.root) if err != nil { return fmt.Errorf("Error looking up dir %s: %s", devices.root, err) } sysSt := st.Sys().(*syscall.Stat_t) // "reg-" stands for "regular file". // In the future we might use "dev-" for "device file", etc. // docker-maj,min[-inode] stands for: // - Managed by docker // - The target of this device is at major <maj> and minor <min> // - If <inode> is defined, use that file inside the device as a loopback image. Otherwise use the device itself. devices.devicePrefix = fmt.Sprintf("docker-%d:%d-%d", major(sysSt.Dev), minor(sysSt.Dev), sysSt.Ino) log.Debugf("Generated prefix: %s", devices.devicePrefix) // Check for the existence of the thin-pool device log.Debugf("Checking for existence of the pool '%s'", devices.getPoolName()) info, err := devicemapper.GetInfo(devices.getPoolName()) if info == nil { log.Debugf("Error device devicemapper.GetInfo: %s", err) return err } // It seems libdevmapper opens this without O_CLOEXEC, and go exec will not close files // that are not Close-on-exec, and lxc-start will die if it inherits any unexpected files, // so we add this badhack to make sure it closes itself setCloseOnExec("/dev/mapper/control") // Make sure the sparse images exist in <root>/devicemapper/data and // <root>/devicemapper/metadata createdLoopback := false // If the pool doesn't exist, create it if info.Exists == 0 && devices.thinPoolDevice == "" { log.Debugf("Pool doesn't exist. Creating it.") var ( dataFile *os.File metadataFile *os.File ) if devices.dataDevice == "" { // Make sure the sparse images exist in <root>/devicemapper/data hasData := devices.hasImage("data") if !doInit && !hasData { return errors.New("Loopback data file not found") } if !hasData { createdLoopback = true } data, err := devices.ensureImage("data", devices.dataLoopbackSize) if err != nil { log.Debugf("Error device ensureImage (data): %s", err) return err } dataFile, err = devicemapper.AttachLoopDevice(data) if err != nil { return err } devices.dataLoopFile = data devices.dataDevice = dataFile.Name() } else { dataFile, err = os.OpenFile(devices.dataDevice, os.O_RDWR, 0600) if err != nil { return err } } defer dataFile.Close() if devices.metadataDevice == "" { // Make sure the sparse images exist in <root>/devicemapper/metadata hasMetadata := devices.hasImage("metadata") if !doInit && !hasMetadata { return errors.New("Loopback metadata file not found") } if !hasMetadata { createdLoopback = true } metadata, err := devices.ensureImage("metadata", devices.metaDataLoopbackSize) if err != nil { log.Debugf("Error device ensureImage (metadata): %s", err) return err } metadataFile, err = devicemapper.AttachLoopDevice(metadata) if err != nil { return err } devices.metadataLoopFile = metadata devices.metadataDevice = metadataFile.Name() } else { metadataFile, err = os.OpenFile(devices.metadataDevice, os.O_RDWR, 0600) if err != nil { return err } } defer metadataFile.Close() if err := devicemapper.CreatePool(devices.getPoolName(), dataFile, metadataFile, devices.thinpBlockSize); err != nil { return err } } // If we didn't just create the data or metadata image, we need to // load the transaction id and migrate old metadata if !createdLoopback { if err = devices.initMetaData(); err != nil { return err } } // Right now this loads only NextDeviceId. If there is more metatadata // down the line, we might have to move it earlier. if err = devices.loadDeviceSetMetaData(); err != nil { return err } // Setup the base image if doInit { if err := devices.setupBaseImage(); err != nil { log.Debugf("Error device setupBaseImage: %s", err) return err } } return nil }
func Init(root, cfgName string, config map[string]string) (drivers.Driver, error) { devicemapper.LogInitVerbose(1) devicemapper.LogInit(&DMLogger{}) if supported := devicemapper.UdevSetSyncSupport(true); !supported { return nil, fmt.Errorf("Udev sync is not supported. Cannot proceed.") } if util.ConfigExists(root, cfgName) { dev := Device{} err := util.LoadConfig(root, cfgName, &dev) d := &Driver{ Mutex: &sync.Mutex{}, } if err != nil { return d, err } d.Device = dev d.configName = cfgName d.root = root if err := d.activatePool(); err != nil { return d, err } return d, nil } dev, err := verifyConfig(config) if err != nil { return nil, err } dev.Root = root dataDev, err := os.Open(dev.DataDevice) if err != nil { return nil, err } defer dataDev.Close() metadataDev, err := os.Open(dev.MetadataDevice) if err != nil { return nil, err } defer metadataDev.Close() thinpSize, err := devicemapper.GetBlockDeviceSize(dataDev) if err != nil { return nil, err } dev.ThinpoolSize = int64(thinpSize) dev.LastDevID = 0 err = createPool(filepath.Base(dev.ThinpoolDevice), dataDev, metadataDev, uint32(dev.ThinpoolBlockSize)) if err != nil { return nil, err } err = util.SaveConfig(root, cfgName, &dev) if err != nil { return nil, err } d := &Driver{ root: root, configName: cfgName, Device: *dev, Mutex: &sync.Mutex{}, } log.Debug("Init done") return d, nil }