AddDevice saves device info to database.
(ctx context.Context, info *DeviceInfo)
| 95 | |
| 96 | // AddDevice saves device info to database. |
| 97 | func (m *PoolMetadata) AddDevice(ctx context.Context, info *DeviceInfo) error { |
| 98 | err := m.db.Update(func(tx *bolt.Tx) error { |
| 99 | devicesBucket := tx.Bucket(devicesBucketName) |
| 100 | |
| 101 | // Make sure device name is unique. If there is already a device with the same name, |
| 102 | // but in Faulty state, give it a try with another devmapper device ID. |
| 103 | // See https://github.com/containerd/containerd/pull/3436 for more context. |
| 104 | var existing DeviceInfo |
| 105 | if err := getObject(devicesBucket, info.Name, &existing); err == nil && existing.State != Faulty { |
| 106 | return fmt.Errorf("device %q is already there %+v: %w", info.Name, existing, ErrAlreadyExists) |
| 107 | } |
| 108 | |
| 109 | // Find next available device ID |
| 110 | deviceID, err := getNextDeviceID(tx) |
| 111 | if err != nil { |
| 112 | return err |
| 113 | } |
| 114 | |
| 115 | info.DeviceID = deviceID |
| 116 | |
| 117 | return putObject(devicesBucket, info.Name, info, true) |
| 118 | }) |
| 119 | |
| 120 | if err != nil { |
| 121 | return fmt.Errorf("failed to save metadata for device %q (parent: %q): %w", info.Name, info.ParentName, err) |
| 122 | } |
| 123 | |
| 124 | return nil |
| 125 | } |
| 126 | |
| 127 | // ChangeDeviceState changes the device state given the device name in devices bucket. |
| 128 | func (m *PoolMetadata) ChangeDeviceState(ctx context.Context, name string, state DeviceState) error { |