func (s *S3ObjectStoreDriver) FinalizeInit(root, cfgName, id string) error { s.ID = id if err := util.SaveConfig(root, cfgName, s); err != nil { return err } return nil }
func (device *Device) saveImage(image *Image) error { uuid := image.UUID cfgName, err := getImageCfgName(uuid) if err != nil { return err } return util.SaveConfig(device.Root, cfgName, image) }
func (device *Device) saveVolume(volume *Volume) error { uuid := volume.UUID cfgName, err := getVolumeCfgName(uuid) if err != nil { return err } return util.SaveConfig(device.Root, cfgName, volume) }
func (d *Driver) allocateDevID() (int, error) { d.Mutex.Lock() defer d.Mutex.Unlock() d.LastDevID++ log.Debug("Current devID ", d.LastDevID) if err := util.SaveConfig(d.root, d.configName, d.Device); err != nil { return 0, err } return d.LastDevID, nil }
func Register(root, kind string, config map[string]string) (*ObjectStore, error) { driver, err := GetObjectStoreDriver(kind, root, "", config) if err != nil { return nil, err } var id string bs, err := loadRemoteObjectStoreConfig(driver) if err == nil { // ObjectStore has already been created if bs.Kind != kind { return nil, generateError(logrus.Fields{ LOG_FIELD_OBJECTSTORE: bs.UUID, LOG_FIELD_KIND: bs.Kind, }, "Specific kind is different from config stored in objectstore") } id = bs.UUID log.Debug("Loaded objectstore cfg in objectstore: ", id) driver.FinalizeInit(root, getDriverCfgName(kind, id), id) } else { log.Debug("Cannot load existed objectstore cfg in objectstore, create a new one: ", err.Error()) id = uuid.New() driver.FinalizeInit(root, getDriverCfgName(kind, id), id) bs = &ObjectStore{ UUID: id, Kind: kind, BlockSize: DEFAULT_BLOCK_SIZE, } if err := saveRemoteObjectStoreConfig(driver, bs); err != nil { return nil, err } log.Debug("Created objectstore cfg in objectstore", bs.UUID) } if err := util.SaveConfig(root, getCfgName(id), bs); err != nil { return nil, err } log.Debug("Created local copy of ", getCfgName(id)) log.Debug("Registered block store ", bs.UUID) return bs, nil }
func (s *Server) saveVolume(volume *Volume) error { uuid := volume.UUID cfgName, err := getVolumeCfgName(uuid) if err != nil { return err } if err := util.SaveConfig(s.Root, cfgName, volume); err != nil { return err } if volume.Name != "" { if oldUUID, exists := s.NameVolumeMap[volume.Name]; exists { if oldUUID != volume.UUID { log.Errorf("BUG: Volume name %v already assign to %v, but %v want it too. How did it pass the test?", volume.Name, oldUUID, uuid) return fmt.Errorf("BUG: Volume name %v already assign to %v, but %v want it too", volume.Name, oldUUID, uuid) } return nil } s.NameVolumeMap[volume.Name] = volume.UUID } 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 }
func initServer(c *cli.Context) (*Server, error) { root := c.String("root") driverName := c.String("driver") driverOpts := util.SliceToMap(c.StringSlice("driver-opts")) imagesDir := c.String("images-dir") mountsDir := c.String("mounts-dir") defaultSize := c.String("default-volume-size") if root == "" || driverName == "" || driverOpts == nil || imagesDir == "" || mountsDir == "" || defaultSize == "" { return nil, fmt.Errorf("Missing or invalid parameters") } size, err := util.ParseSize(defaultSize) if err != nil { return nil, err } log.Debug("Config root is ", root) if util.ConfigExists(root, getCfgName()) { return nil, fmt.Errorf("Configuration file already existed. Don't need to initialize.") } if err := util.MkdirIfNotExists(imagesDir); err != nil { return nil, err } log.Debug("Images would be stored at ", imagesDir) if err := util.MkdirIfNotExists(mountsDir); err != nil { return nil, err } log.Debug("Default mounting directory would be ", mountsDir) log.WithFields(logrus.Fields{ LOG_FIELD_REASON: LOG_REASON_PREPARE, LOG_FIELD_EVENT: LOG_EVENT_INIT, LOG_FIELD_DRIVER: driverName, "root": root, "driverOpts": driverOpts, }).Debug() driver, err := drivers.GetDriver(driverName, root, driverOpts) if err != nil { return nil, err } log.WithFields(logrus.Fields{ LOG_FIELD_REASON: LOG_REASON_COMPLETE, LOG_FIELD_EVENT: LOG_EVENT_INIT, LOG_FIELD_DRIVER: driverName, }).Debug() config := Config{ Root: root, Driver: driverName, ImagesDir: imagesDir, MountsDir: mountsDir, DefaultVolumeSize: size, } server := &Server{ Config: config, StorageDriver: driver, NameVolumeMap: make(map[string]string), } err = util.SaveConfig(root, getCfgName(), &config) return server, err }