blob: 7cc778aa167e26db86b0eeb229a1b2eff11a39d3 [file] [log] [blame]
John Edward Broadbenta6e3b992022-03-17 14:33:15 -07001#pragma once
John Wedigd32b9662022-04-13 18:12:25 -07002#include "getConfig.hpp"
3
4#include <filesystem>
Tom Tung043af592023-11-24 13:37:05 +08005#include <optional>
John Edward Broadbent5d799bb2022-03-22 16:14:24 -07006#include <string>
John Edward Broadbenta6e3b992022-03-17 14:33:15 -07007
8namespace estoraged
9{
10namespace util
11{
12
Tom Tung043af592023-11-24 13:37:05 +080013struct DeviceInfo
14{
15 std::filesystem::path deviceFile;
16 std::filesystem::path sysfsDir;
17 std::string luksName;
18 std::string locationCode;
19 uint64_t eraseMaxGeometry;
20 uint64_t eraseMinGeometry;
John Wedigd7be42b2024-01-19 16:07:19 -080021 std::string driveType;
Tom Tung043af592023-11-24 13:37:05 +080022
23 DeviceInfo(std::filesystem::path& deviceFile,
24 std::filesystem::path& sysfsDir, std::string& luksName,
25 std::string& locationCode, uint64_t eraseMaxGeometry,
John Wedigd7be42b2024-01-19 16:07:19 -080026 uint64_t eraseMinGeometry, std::string& driveType) :
Tom Tung043af592023-11-24 13:37:05 +080027 deviceFile(deviceFile),
28 sysfsDir(sysfsDir), luksName(luksName), locationCode(locationCode),
John Wedigd7be42b2024-01-19 16:07:19 -080029 eraseMaxGeometry(eraseMaxGeometry), eraseMinGeometry(eraseMinGeometry),
30 driveType(driveType)
Tom Tung043af592023-11-24 13:37:05 +080031 {}
32};
33
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070034/** @brief finds the size of the linux block device in bytes
35 * @param[in] devpath - the name of the linux block device
36 * @return size of a block device using the devPath
37 */
38uint64_t findSizeOfBlockDevice(const std::string& devPath);
39
40/** @brief finds the predicted life left for a eMMC device
41 * @param[in] sysfsPath - The path to the linux sysfs interface
42 * @return the life remaing for the emmc, as a percentage.
43 */
44uint8_t findPredictedMediaLifeLeftPercent(const std::string& sysfsPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -070045
John Wedigb4838302022-07-22 13:51:16 -070046/** @brief Get the part number (aka part name) for the storage device
47 * @param[in] sysfsPath - The path to the linux sysfs interface.
48 * @return part name as a string (or "unknown" if it couldn't be retrieved)
49 */
50std::string getPartNumber(const std::filesystem::path& sysfsPath);
51
52/** @brief Get the serial number for the storage device
53 * @param[in] sysfsPath - The path to the linux sysfs interface.
54 * @return serial name as a string (or "unknown" if it couldn't be retrieved)
55 */
56std::string getSerialNumber(const std::filesystem::path& sysfsPath);
57
John Wedigd32b9662022-04-13 18:12:25 -070058/** @brief Look for the device described by the provided StorageData.
59 * @details Currently, this function assumes that there's only one eMMC.
60 * When we need to support multiple eMMCs, we will put more information in
61 * the EntityManager config, to differentiate between them. Also, if we
62 * want to support other types of storage devices, this function will need
63 * to be updated.
64 *
65 * @param[in] data - map of properties from the config object.
66 * @param[in] searchDir - directory to search for devices in sysfs, e.g.
67 * /sys/block
Tom Tung043af592023-11-24 13:37:05 +080068 * @return DeviceInfo - metadata for the device if device is found. Null
69 * otherwise.
John Wedigd32b9662022-04-13 18:12:25 -070070 */
Tom Tung043af592023-11-24 13:37:05 +080071std::optional<DeviceInfo> findDevice(const StorageData& data,
72 const std::filesystem::path& searchDir);
John Wedigd32b9662022-04-13 18:12:25 -070073
John Edward Broadbenta6e3b992022-03-17 14:33:15 -070074} // namespace util
75
76} // namespace estoraged