| // Copyright 2022 The ChromiumOS Authors | |
| // Use of this source code is governed by a BSD-style license that can be | |
| // found in the LICENSE file. | |
| #ifndef DLCSERVICE_LVM_LVMD_PROXY_WRAPPER_H_ | |
| #define DLCSERVICE_LVM_LVMD_PROXY_WRAPPER_H_ | |
| #include <memory> | |
| #include <string> | |
| #include <vector> | |
| #include <gtest/gtest_prod.h> // for FRIEND_TEST | |
| #include <lvmd/proto_bindings/lvmd.pb.h> | |
| #include <lvmd/dbus-proxies.h> | |
| #include "dlcservice/utils/utils.h" | |
| namespace dlcservice { | |
| // Provides a simpler interface into lvmd. | |
| class LvmdProxyWrapperInterface { | |
| public: | |
| virtual ~LvmdProxyWrapperInterface() = default; | |
| // Creates the logical volumes, for logical volumes that already exist, they | |
| // will be activated. | |
| virtual bool CreateLogicalVolumes( | |
| const std::vector<lvmd::LogicalVolumeConfiguration>& lv_configs) = 0; | |
| // Removes the logical volumes, if they exist. | |
| virtual bool RemoveLogicalVolumes( | |
| const std::vector<std::string>& lv_names) = 0; | |
| virtual void RemoveLogicalVolumesAsync( | |
| const std::vector<std::string>& lv_names, | |
| base::OnceCallback<void(bool)> cb) = 0; | |
| // Activates the logical volume, if they exist. | |
| virtual bool ActivateLogicalVolume(const std::string& lv_name) = 0; | |
| // Returns the list of logical volumes on the devices. | |
| virtual bool ListLogicalVolumes(lvmd::LogicalVolumeList* lvs) = 0; | |
| // Returns the logical volume path as a string. | |
| // Returns empty string if the logical volume does not exist. | |
| virtual std::string GetLogicalVolumePath(const std::string& lv_name) = 0; | |
| // Returns the logical volume size in MiB. Returns nullopt if failed to get | |
| // logical volume or its size. | |
| virtual std::optional<int64_t> GetLogicalVolumeSize( | |
| const std::string& lv_name) = 0; | |
| // Returns the physical volume information. | |
| virtual bool GetPhysicalVolume(const std::string& device_path, | |
| lvmd::PhysicalVolume* pv) = 0; | |
| // Resize the logical volumes to the sizes in the configs. | |
| virtual bool ResizeLogicalVolumes( | |
| const std::vector<lvmd::LogicalVolumeConfiguration>& lv_configs) = 0; | |
| }; | |
| class LvmdProxyWrapper : public LvmdProxyWrapperInterface { | |
| public: | |
| using LvmdProxyInterface = org::chromium::LvmdProxyInterface; | |
| explicit LvmdProxyWrapper(std::unique_ptr<LvmdProxyInterface> lvmd_proxy); | |
| LvmdProxyWrapper(std::unique_ptr<LvmdProxyInterface> lvmd_proxy, | |
| std::unique_ptr<UtilsInterface> utils); | |
| ~LvmdProxyWrapper() = default; | |
| LvmdProxyWrapper(const LvmdProxyWrapper&) = delete; | |
| LvmdProxyWrapper& operator=(const LvmdProxyWrapper&) = delete; | |
| // `LvmdProxyWrapper` overrides. | |
| bool CreateLogicalVolumes( | |
| const std::vector<lvmd::LogicalVolumeConfiguration>& lv_configs) override; | |
| bool RemoveLogicalVolumes(const std::vector<std::string>& lv_names) override; | |
| void RemoveLogicalVolumesAsync(const std::vector<std::string>& lv_names, | |
| base::OnceCallback<void(bool)> cb) override; | |
| bool ActivateLogicalVolume(const std::string& lv_name) override; | |
| bool ListLogicalVolumes(lvmd::LogicalVolumeList* lvs) override; | |
| std::string GetLogicalVolumePath(const std::string& lv_name) override; | |
| std::optional<int64_t> GetLogicalVolumeSize( | |
| const std::string& lv_name) override; | |
| bool GetPhysicalVolume(const std::string& device_path, | |
| lvmd::PhysicalVolume* pv) override; | |
| bool ResizeLogicalVolumes( | |
| const std::vector<lvmd::LogicalVolumeConfiguration>& lv_configs) override; | |
| private: | |
| friend class LvmdProxyWrapperTest; | |
| FRIEND_TEST(LvmdProxyWrapperTest, CreateLogicalVolumeGidCheck); | |
| bool GetVolumeGroup(const lvmd::PhysicalVolume& pv, lvmd::VolumeGroup* vg); | |
| bool GetThinpool(const lvmd::VolumeGroup& vg, lvmd::Thinpool* thinpool); | |
| bool GetLogicalVolume(const lvmd::VolumeGroup& vg, | |
| const std::string& lv_name, | |
| lvmd::LogicalVolume* lv); | |
| bool GetLogicalVolume(const std::string& lv_name, lvmd::LogicalVolume* lv); | |
| bool CreateLogicalVolume(const lvmd::Thinpool& thinpool, | |
| const lvmd::LogicalVolumeConfiguration& lv_config, | |
| lvmd::LogicalVolume* lv); | |
| bool ToggleLogicalVolumeActivation(const lvmd::LogicalVolume& lv, | |
| bool activate); | |
| std::unique_ptr<LvmdProxyInterface> lvmd_proxy_; | |
| std::unique_ptr<UtilsInterface> utils_; | |
| }; | |
| } // namespace dlcservice | |
| #endif // DLCSERVICE_LVM_LVMD_PROXY_WRAPPER_H_ |