blob: e1050ed0524c742bd1f5698783f33c1694219613 [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;
John Wedigc0d66eb2024-02-26 15:54:47 -080022 std::string driveProtocol;
Tom Tung043af592023-11-24 13:37:05 +080023
24 DeviceInfo(std::filesystem::path& deviceFile,
25 std::filesystem::path& sysfsDir, std::string& luksName,
26 std::string& locationCode, uint64_t eraseMaxGeometry,
John Wedigc0d66eb2024-02-26 15:54:47 -080027 uint64_t eraseMinGeometry, std::string& driveType,
28 std::string& driveProtocol) :
Tom Tung043af592023-11-24 13:37:05 +080029 deviceFile(deviceFile),
30 sysfsDir(sysfsDir), luksName(luksName), locationCode(locationCode),
John Wedigd7be42b2024-01-19 16:07:19 -080031 eraseMaxGeometry(eraseMaxGeometry), eraseMinGeometry(eraseMinGeometry),
John Wedigc0d66eb2024-02-26 15:54:47 -080032 driveType(driveType), driveProtocol(driveProtocol)
Tom Tung043af592023-11-24 13:37:05 +080033 {}
34};
35
John Edward Broadbent5d799bb2022-03-22 16:14:24 -070036/** @brief finds the size of the linux block device in bytes
37 * @param[in] devpath - the name of the linux block device
38 * @return size of a block device using the devPath
39 */
40uint64_t findSizeOfBlockDevice(const std::string& devPath);
41
42/** @brief finds the predicted life left for a eMMC device
43 * @param[in] sysfsPath - The path to the linux sysfs interface
44 * @return the life remaing for the emmc, as a percentage.
45 */
46uint8_t findPredictedMediaLifeLeftPercent(const std::string& sysfsPath);
John Edward Broadbenta6e3b992022-03-17 14:33:15 -070047
John Wedigb4838302022-07-22 13:51:16 -070048/** @brief Get the part number (aka part name) for the storage device
49 * @param[in] sysfsPath - The path to the linux sysfs interface.
50 * @return part name as a string (or "unknown" if it couldn't be retrieved)
51 */
52std::string getPartNumber(const std::filesystem::path& sysfsPath);
53
54/** @brief Get the serial number for the storage device
55 * @param[in] sysfsPath - The path to the linux sysfs interface.
56 * @return serial name as a string (or "unknown" if it couldn't be retrieved)
57 */
58std::string getSerialNumber(const std::filesystem::path& sysfsPath);
59
John Wedigd32b9662022-04-13 18:12:25 -070060/** @brief Look for the device described by the provided StorageData.
61 * @details Currently, this function assumes that there's only one eMMC.
62 * When we need to support multiple eMMCs, we will put more information in
63 * the EntityManager config, to differentiate between them. Also, if we
64 * want to support other types of storage devices, this function will need
65 * to be updated.
66 *
67 * @param[in] data - map of properties from the config object.
68 * @param[in] searchDir - directory to search for devices in sysfs, e.g.
69 * /sys/block
Tom Tung043af592023-11-24 13:37:05 +080070 * @return DeviceInfo - metadata for the device if device is found. Null
71 * otherwise.
John Wedigd32b9662022-04-13 18:12:25 -070072 */
Tom Tung043af592023-11-24 13:37:05 +080073std::optional<DeviceInfo> findDevice(const StorageData& data,
74 const std::filesystem::path& searchDir);
John Wedigd32b9662022-04-13 18:12:25 -070075
John Edward Broadbenta6e3b992022-03-17 14:33:15 -070076} // namespace util
77
78} // namespace estoraged