xref: /openbmc/phosphor-networkd/src/hostname_manager.hpp (revision 6b1b0ea9732f4e1ce33e041062f5b957d6c76f4a)
1 #pragma once
2 
3 #include <sdbusplus/bus.hpp>
4 #include <stdplus/pinned.hpp>
5 
6 #include <filesystem>
7 #include <string>
8 
9 namespace phosphor
10 {
11 namespace network
12 {
13 
14 class Manager;
15 
16 /** @class HostnameManager
17  *  @brief Generates and manages unique BMC hostname
18  *  @details Sets unique hostname on first boot by appending either
19  *           the BMC serial number or MAC address to the default hostname.
20  */
21 class HostnameManager
22 {
23   public:
24     HostnameManager() = delete;
25     HostnameManager(const HostnameManager&) = delete;
26     HostnameManager& operator=(const HostnameManager&) = delete;
27     HostnameManager(HostnameManager&&) = delete;
28     HostnameManager& operator=(HostnameManager&&) = delete;
29 
30     explicit HostnameManager(stdplus::PinnedRef<sdbusplus::bus_t> bus,
31                              stdplus::PinnedRef<Manager> manager);
32 
33     /** @brief Checks if this is first boot and sets unique hostname
34      */
35     void initialize();
36 
37   private:
38     stdplus::PinnedRef<sdbusplus::bus_t> bus;
39     stdplus::PinnedRef<Manager> manager;
40 
41     static constexpr const char* firstBootFile =
42         "/var/lib/phosphor-networkd-hostname-set";
43 
44     /** @brief Check if this is the first boot
45      *  @return true if first boot, false otherwise
46      */
47     bool isFirstBoot() const;
48 
49     /** @brief Mark that hostname has been set */
50     void markHostnameSet();
51 
52     /** @brief Get BMC serial number from inventory
53      *  @return Serial number string, or empty if not found
54      */
55     std::string getBmcSerialNumber();
56 
57     /** @brief Get MAC address from first network interface
58      *  @return MAC address string, or empty if not found
59      */
60     std::string getMacAddress();
61 
62     /** @brief Set the system hostname
63      *  @param[in] hostname - The hostname to set
64      *  @return true if successful, false otherwise
65      */
66     bool setHostname(const std::string& hostname);
67 
68     /** @brief Get current hostname
69      *  @return Current hostname string
70      */
71     std::string getCurrentHostname();
72 
73     /** @brief Generate and set unique hostname */
74     void setUniqueHostname();
75 };
76 
77 } // namespace network
78 } // namespace phosphor
79