Browse code

devmapper: Construct initial device Id map from device meta files

When docker starts, build a used/free Device Id map from the per
device meta files we already have. These meta files have the data
which device Ids are in use. Parse these files and mark device as
used in the map.

Signed-off-by: Vivek Goyal <vgoyal@redhat.com>

Vivek Goyal authored on 2014/12/04 03:06:43
Showing 1 changed files
... ...
@@ -303,6 +303,65 @@ func (devices *DeviceSet) lookupDevice(hash string) (*DevInfo, error) {
303 303
 	return info, nil
304 304
 }
305 305
 
306
+func (devices *DeviceSet) deviceFileWalkFunction(path string, finfo os.FileInfo) error {
307
+
308
+	// Skip some of the meta files which are not device files.
309
+	if strings.HasSuffix(finfo.Name(), ".migrated") {
310
+		log.Debugf("Skipping file %s", path)
311
+		return nil
312
+	}
313
+
314
+	if finfo.Name() == deviceSetMetaFile {
315
+		log.Debugf("Skipping file %s", path)
316
+		return nil
317
+	}
318
+
319
+	log.Debugf("Loading data for file %s", path)
320
+
321
+	hash := finfo.Name()
322
+	if hash == "base" {
323
+		hash = ""
324
+	}
325
+
326
+	dinfo := devices.loadMetadata(hash)
327
+	if dinfo == nil {
328
+		return fmt.Errorf("Error loading device metadata file %s", hash)
329
+	}
330
+
331
+	if dinfo.DeviceId > MaxDeviceId {
332
+		log.Errorf("Warning: Ignoring Invalid DeviceId=%d", dinfo.DeviceId)
333
+		return nil
334
+	}
335
+
336
+	devices.Lock()
337
+	devices.markDeviceIdUsed(dinfo.DeviceId)
338
+	devices.Unlock()
339
+
340
+	log.Debugf("Added deviceId=%d to DeviceIdMap", dinfo.DeviceId)
341
+	return nil
342
+}
343
+
344
+func (devices *DeviceSet) constructDeviceIdMap() error {
345
+	log.Debugf("[deviceset] constructDeviceIdMap()")
346
+	defer log.Debugf("[deviceset] constructDeviceIdMap() END")
347
+
348
+	var scan = func(path string, info os.FileInfo, err error) error {
349
+		if err != nil {
350
+			log.Debugf("Can't walk the file %s", path)
351
+			return nil
352
+		}
353
+
354
+		// Skip any directories
355
+		if info.IsDir() {
356
+			return nil
357
+		}
358
+
359
+		return devices.deviceFileWalkFunction(path, info)
360
+	}
361
+
362
+	return filepath.Walk(devices.metadataDir(), scan)
363
+}
364
+
306 365
 func (devices *DeviceSet) unregisterDevice(id int, hash string) error {
307 366
 	log.Debugf("unregisterDevice(%v, %v)", id, hash)
308 367
 	info := &DevInfo{
... ...
@@ -429,6 +488,10 @@ func (devices *DeviceSet) initMetaData() error {
429 429
 	}
430 430
 
431 431
 	devices.TransactionId = transactionId
432
+
433
+	if err := devices.constructDeviceIdMap(); err != nil {
434
+		return err
435
+	}
432 436
 	return nil
433 437
 }
434 438