6
6
#include < folly/FileUtil.h>
7
7
#include < folly/String.h>
8
8
#include < folly/logging/xlog.h>
9
+ #include < range/v3/range/conversion.hpp>
10
+ #include < range/v3/view/drop.hpp>
11
+ #include < range/v3/view/filter.hpp>
12
+ #include < range/v3/view/split.hpp>
9
13
#include < re2/re2.h>
10
14
#include < sys/utsname.h>
11
15
#include < thrift/lib/cpp2/protocol/Serializer.h>
12
16
13
- #include " fboss/platform/helpers/PlatformUtils.h"
14
-
15
17
DEFINE_bool (
16
18
enable_pkg_mgmnt,
17
19
true ,
@@ -37,14 +39,16 @@ const re2::RE2 kBspRpmNameRe = "(?P<KEYWORD>[a-z]+)_bsp_kmods";
37
39
} // namespace
38
40
39
41
namespace package_manager {
40
- SystemInterface::SystemInterface () {}
42
+ SystemInterface::SystemInterface (
43
+ const std::shared_ptr<PlatformUtils>& platformUtils)
44
+ : platformUtils_(platformUtils) {}
41
45
42
46
bool SystemInterface::loadKmod (const std::string& moduleName) const {
43
47
int exitStatus{0 };
44
48
std::string standardOut{};
45
49
auto unloadCmd = fmt::format (" modprobe {}" , moduleName);
46
50
VLOG (1 ) << fmt::format (" Running command ({})" , unloadCmd);
47
- std::tie (exitStatus, standardOut) = PlatformUtils (). execCommand (unloadCmd);
51
+ std::tie (exitStatus, standardOut) = platformUtils_-> execCommand (unloadCmd);
48
52
return exitStatus == 0 ;
49
53
}
50
54
@@ -53,7 +57,7 @@ bool SystemInterface::unloadKmod(const std::string& moduleName) const {
53
57
std::string standardOut{};
54
58
auto unloadCmd = fmt::format (" rmmod {}" , moduleName);
55
59
VLOG (1 ) << fmt::format (" Running command ({})" , unloadCmd);
56
- std::tie (exitStatus, standardOut) = PlatformUtils (). execCommand (unloadCmd);
60
+ std::tie (exitStatus, standardOut) = platformUtils_-> execCommand (unloadCmd);
57
61
return exitStatus == 0 ;
58
62
}
59
63
@@ -62,7 +66,7 @@ int SystemInterface::installRpm(const std::string& rpmFullName) const {
62
66
std::string standardOut{};
63
67
auto cmd = fmt::format (" dnf install {} --assumeyes" , rpmFullName);
64
68
VLOG (1 ) << fmt::format (" Running command ({})" , cmd);
65
- std::tie (exitStatus, standardOut) = PlatformUtils (). execCommand (cmd);
69
+ std::tie (exitStatus, standardOut) = platformUtils_-> execCommand (cmd);
66
70
return exitStatus;
67
71
}
68
72
@@ -71,7 +75,7 @@ int SystemInterface::installLocalRpm() const {
71
75
std::string standardOut{};
72
76
auto cmd = fmt::format (" rpm -i --force {}" , FLAGS_local_rpm_path);
73
77
VLOG (1 ) << fmt::format (" Running command ({})" , cmd);
74
- std::tie (exitStatus, standardOut) = PlatformUtils (). execCommand (cmd);
78
+ std::tie (exitStatus, standardOut) = platformUtils_-> execCommand (cmd);
75
79
return exitStatus;
76
80
}
77
81
@@ -80,7 +84,7 @@ int SystemInterface::depmod() const {
80
84
std::string standardOut{};
81
85
auto depmodCmd = " depmod -a" ;
82
86
VLOG (1 ) << fmt::format (" Running command ({})" , depmodCmd);
83
- std::tie (exitStatus, standardOut) = PlatformUtils (). execCommand (depmodCmd);
87
+ std::tie (exitStatus, standardOut) = platformUtils_-> execCommand (depmodCmd);
84
88
return exitStatus;
85
89
}
86
90
@@ -90,7 +94,7 @@ std::vector<std::string> SystemInterface::getInstalledRpms(
90
94
int exitStatus{0 };
91
95
auto cmd = fmt::format (" rpm -qa | grep ^{}" , rpmBaseName);
92
96
VLOG (1 ) << fmt::format (" Running command ({})" , cmd);
93
- std::tie (exitStatus, standardOut) = PlatformUtils (). execCommand (cmd);
97
+ std::tie (exitStatus, standardOut) = platformUtils_-> execCommand (cmd);
94
98
if (exitStatus) {
95
99
return {};
96
100
}
@@ -107,14 +111,29 @@ int SystemInterface::removeRpms(
107
111
fmt::format (" rpm -e --allmatches {}" , folly::join (" " , installedRpms));
108
112
VLOG (1 ) << fmt::format (" Running command ({})" , removeOldRpmsCmd);
109
113
std::tie (exitStatus, standardOut) =
110
- PlatformUtils (). execCommand (removeOldRpmsCmd);
114
+ platformUtils_-> execCommand (removeOldRpmsCmd);
111
115
return exitStatus;
112
116
}
113
117
114
- std::string SystemInterface::lsmod () const {
118
+ std::set<std:: string> SystemInterface::lsmod () const {
115
119
VLOG (1 ) << " Running command (lsmod)" ;
116
- auto result = PlatformUtils ().execCommand (" lsmod" );
117
- return result.second ;
120
+ auto result = platformUtils_->execCommand (" lsmod" );
121
+ auto rows = result.second | ranges::views::split (' \n ' ) |
122
+ ranges::views::drop (1 ) | ranges::to<std::vector<std::string>>;
123
+ std::set<std::string> kmods;
124
+ // row -> kmod | size | used by | dependent kmods
125
+ for (const auto & row : rows) {
126
+ auto tokens = row | ranges::views::split (' ' ) |
127
+ ranges::views::filter (
128
+ [](const auto & token) { return !token.empty (); }) |
129
+ ranges::to<std::vector<std::string>>;
130
+ if (tokens.empty ()) {
131
+ XLOG (WARNING) << fmt::format (" Failed to scan lsmod row -- {}" , row);
132
+ continue ;
133
+ }
134
+ kmods.emplace (tokens.front ());
135
+ }
136
+ return kmods;
118
137
}
119
138
120
139
std::string SystemInterface::getHostKernelVersion () const {
@@ -299,7 +318,7 @@ void PkgManager::unloadBspKmods() const {
299
318
XLOG (INFO) << " Unloading kernel modules based on kmods.json" ;
300
319
const auto loadedKmods = systemInterface_->lsmod ();
301
320
for (const auto & kmod : *bspKmodsFile.bspKmods ()) {
302
- if (loadedKmods.find (kmod) != std::string::npos ) {
321
+ if (loadedKmods.contains (kmod)) {
303
322
XLOG (INFO) << fmt::format (" Unloading {}" , kmod);
304
323
if (!systemInterface_->unloadKmod (kmod)) {
305
324
throw std::runtime_error (fmt::format (" Failed to unload ({})" , kmod));
@@ -311,7 +330,7 @@ void PkgManager::unloadBspKmods() const {
311
330
}
312
331
XLOG (INFO) << " Unloading shared kernel modules" ;
313
332
for (const auto & kmod : *bspKmodsFile.sharedKmods ()) {
314
- if (loadedKmods.find (kmod) != std::string::npos ) {
333
+ if (loadedKmods.contains (kmod)) {
315
334
XLOG (INFO) << fmt::format (" Unloading {}" , kmod);
316
335
if (!systemInterface_->unloadKmod (kmod)) {
317
336
throw std::runtime_error (fmt::format (" Failed to unload ({})" , kmod));
0 commit comments