blob: 422e1bf5494b58cb3f3726e94c37a8339c7e9432 [file] [log] [blame]
Gunnar Mills19e4ce52017-04-27 11:24:19 -05001#include "config.h"
Gunnar Millsb0ce9962018-09-07 13:39:10 -05002
3#include "image_manager.hpp"
4
Gunnar Mills19e4ce52017-04-27 11:24:19 -05005#include "version.hpp"
Gunnar Mills3027bba2017-04-27 15:49:03 -05006#include "watch.hpp"
Gunnar Millsb0ce9962018-09-07 13:39:10 -05007
8#include <stdio.h>
9#include <stdlib.h>
10#include <sys/stat.h>
11#include <sys/wait.h>
12#include <unistd.h>
13
14#include <algorithm>
15#include <cstring>
16#include <elog-errors.hpp>
17#include <experimental/filesystem>
18#include <phosphor-logging/elog.hpp>
19#include <phosphor-logging/log.hpp>
20#include <string>
Adriana Kobylak43699ca2018-10-17 14:56:29 -050021#include <xyz/openbmc_project/Software/Image/error.hpp>
Gunnar Millse91d3212017-04-19 15:42:47 -050022
23namespace phosphor
24{
25namespace software
26{
27namespace manager
28{
29
Gunnar Mills19e4ce52017-04-27 11:24:19 -050030using namespace phosphor::logging;
Adriana Kobylak43699ca2018-10-17 14:56:29 -050031using namespace sdbusplus::xyz::openbmc_project::Software::Image::Error;
Gunnar Millsb30cadd2017-10-06 16:07:32 -050032namespace Software = phosphor::logging::xyz::openbmc_project::Software;
Adriana Kobylak43699ca2018-10-17 14:56:29 -050033using ManifestFail = Software::Image::ManifestFileFailure;
34using UnTarFail = Software::Image::UnTarFailure;
35using InternalFail = Software::Image::InternalFailure;
Gunnar Mills19e4ce52017-04-27 11:24:19 -050036namespace fs = std::experimental::filesystem;
37
38struct RemovablePath
39{
40 fs::path path;
41
Adriana Kobylak2285fe02018-02-27 15:36:59 -060042 RemovablePath(const fs::path& path) : path(path)
43 {
44 }
Gunnar Mills19e4ce52017-04-27 11:24:19 -050045 ~RemovablePath()
46 {
Lei YU5d930282019-03-08 16:41:51 +080047 if (!path.empty())
Adriana Kobylak2b2cd9f2018-02-12 16:20:13 -060048 {
Lei YU5d930282019-03-08 16:41:51 +080049 std::error_code ec;
50 fs::remove_all(path, ec);
Adriana Kobylak2b2cd9f2018-02-12 16:20:13 -060051 }
Gunnar Mills19e4ce52017-04-27 11:24:19 -050052 }
53};
54
Gunnar Mills3027bba2017-04-27 15:49:03 -050055int Manager::processImage(const std::string& tarFilePath)
Gunnar Millse91d3212017-04-19 15:42:47 -050056{
Gunnar Mills19e4ce52017-04-27 11:24:19 -050057 if (!fs::is_regular_file(tarFilePath))
58 {
59 log<level::ERR>("Error tarball does not exist",
Adriana Kobylak596466b2018-02-13 14:48:53 -060060 entry("FILENAME=%s", tarFilePath.c_str()));
Gunnar Millsb30cadd2017-10-06 16:07:32 -050061 report<ManifestFileFailure>(ManifestFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -050062 return -1;
Gunnar Mills19e4ce52017-04-27 11:24:19 -050063 }
64 RemovablePath tarPathRemove(tarFilePath);
65 fs::path tmpDirPath(std::string{IMG_UPLOAD_DIR});
66 tmpDirPath /= "imageXXXXXX";
Lei YU5d930282019-03-08 16:41:51 +080067 auto tmpDir = tmpDirPath.string();
Gunnar Mills19e4ce52017-04-27 11:24:19 -050068
Lei YU5d930282019-03-08 16:41:51 +080069 // Create a tmp dir to extract tarball.
70 if (!mkdtemp(tmpDir.data()))
Gunnar Mills19e4ce52017-04-27 11:24:19 -050071 {
Gunnar Mills601bbf02017-10-25 13:46:11 -050072 log<level::ERR>("Error occurred during mkdtemp",
Gunnar Mills19e4ce52017-04-27 11:24:19 -050073 entry("ERRNO=%d", errno));
Gunnar Millsb30cadd2017-10-06 16:07:32 -050074 report<InternalFailure>(InternalFail::FAIL("mkdtemp"));
Gunnar Mills19e4ce52017-04-27 11:24:19 -050075 return -1;
76 }
77
Lei YU5d930282019-03-08 16:41:51 +080078 tmpDirPath = tmpDir;
79 RemovablePath tmpDirToRemove(tmpDirPath);
Gunnar Mills19e4ce52017-04-27 11:24:19 -050080 fs::path manifestPath = tmpDirPath;
81 manifestPath /= MANIFEST_FILE_NAME;
Gunnar Mills19e4ce52017-04-27 11:24:19 -050082
Lei YU5d930282019-03-08 16:41:51 +080083 // Untar tarball into the tmp dir
84 auto rc = unTar(tarFilePath, tmpDirPath.string());
85 if (rc < 0)
Gunnar Mills19e4ce52017-04-27 11:24:19 -050086 {
Lei YU5d930282019-03-08 16:41:51 +080087 log<level::ERR>("Error occurred during untar");
Gunnar Mills19e4ce52017-04-27 11:24:19 -050088 return -1;
89 }
90
91 // Verify the manifest file
92 if (!fs::is_regular_file(manifestPath))
93 {
Adriana Kobylak596466b2018-02-13 14:48:53 -060094 log<level::ERR>("Error No manifest file",
95 entry("FILENAME=%s", tarFilePath.c_str()));
96 report<ManifestFileFailure>(ManifestFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -050097 return -1;
98 }
99
100 // Get version
101 auto version = Version::getValue(manifestPath.string(), "version");
102 if (version.empty())
103 {
104 log<level::ERR>("Error unable to read version from manifest file");
Adriana Kobylak596466b2018-02-13 14:48:53 -0600105 report<ManifestFileFailure>(ManifestFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500106 return -1;
107 }
108
Vijay Khemkab7c062e2019-09-18 17:15:57 -0700109 // Get running machine name
110 std::string currMachine = Version::getBMCMachine(OS_RELEASE_FILE);
111 if (currMachine.empty())
112 {
113 log<level::ERR>("Failed to read machine name from osRelease",
114 entry("FILENAME=%s", OS_RELEASE_FILE));
115 return -1;
116 }
117
118 // Get machine name for image to be upgraded
119 std::string machineStr =
120 Version::getValue(manifestPath.string(), "MachineName");
121 if (!machineStr.empty())
122 {
123 if (machineStr != currMachine)
124 {
125 log<level::ERR>("BMC upgrade: Machine name doesn't match",
126 entry("CURR_MACHINE=%s", currMachine.c_str()),
127 entry("NEW_MACHINE=%s", machineStr.c_str()));
128 return -1;
129 }
130 }
131 else
132 {
133 log<level::WARNING>("No machine name in Manifest file");
134 }
135
Gunnar Mills3027bba2017-04-27 15:49:03 -0500136 // Get purpose
137 auto purposeString = Version::getValue(manifestPath.string(), "purpose");
138 if (purposeString.empty())
139 {
140 log<level::ERR>("Error unable to read purpose from manifest file");
Adriana Kobylak596466b2018-02-13 14:48:53 -0600141 report<ManifestFileFailure>(ManifestFail::PATH(tarFilePath.c_str()));
Gunnar Mills3027bba2017-04-27 15:49:03 -0500142 return -1;
143 }
144
Gunnar Mills3027bba2017-04-27 15:49:03 -0500145 auto purpose = Version::VersionPurpose::Unknown;
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500146 try
147 {
Leonel Gonzalez9a8d14c2017-06-22 11:42:24 -0500148 purpose = Version::convertVersionPurposeFromString(purposeString);
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500149 }
150 catch (const sdbusplus::exception::InvalidEnumString& e)
151 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600152 log<level::ERR>("Error: Failed to convert manifest purpose to enum."
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500153 " Setting to Unknown.");
Gunnar Mills3027bba2017-04-27 15:49:03 -0500154 }
155
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500156 // Compute id
157 auto id = Version::getId(version);
158
159 fs::path imageDirPath = std::string{IMG_UPLOAD_DIR};
160 imageDirPath /= id;
161
Gunnar Millsf576bca2017-05-18 13:40:39 -0500162 if (fs::exists(imageDirPath))
163 {
164 fs::remove_all(imageDirPath);
165 }
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500166
Lei YU5d930282019-03-08 16:41:51 +0800167 // Rename the temp dir to image dir
168 fs::rename(tmpDirPath, imageDirPath);
169
170 // Clear the path, so it does not attemp to remove a non-existing path
171 tmpDirToRemove.path.clear();
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500172
Gunnar Mills3027bba2017-04-27 15:49:03 -0500173 // Create Version object
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600174 auto objPath = std::string{SOFTWARE_OBJPATH} + '/' + id;
Gunnar Mills3027bba2017-04-27 15:49:03 -0500175
Saqib Khand377ba12017-10-16 14:32:30 -0500176 if (versions.find(id) == versions.end())
177 {
Saqib Khanee13e832017-10-23 12:53:11 -0500178 auto versionPtr = std::make_unique<Version>(
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600179 bus, objPath, version, purpose, imageDirPath.string(),
180 std::bind(&Manager::erase, this, std::placeholders::_1));
Saqib Khanee13e832017-10-23 12:53:11 -0500181 versionPtr->deleteObject =
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600182 std::make_unique<phosphor::software::manager::Delete>(bus, objPath,
183 *versionPtr);
Saqib Khanee13e832017-10-23 12:53:11 -0500184 versions.insert(std::make_pair(id, std::move(versionPtr)));
Saqib Khand377ba12017-10-16 14:32:30 -0500185 }
186 else
187 {
188 log<level::INFO>("Software Object with the same version already exists",
Adriana Kobylak596466b2018-02-13 14:48:53 -0600189 entry("VERSION_ID=%s", id.c_str()));
Saqib Khand377ba12017-10-16 14:32:30 -0500190 }
Gunnar Millse91d3212017-04-19 15:42:47 -0500191 return 0;
192}
193
Leonel Gonzalez50d559c2017-07-07 14:38:25 -0500194void Manager::erase(std::string entryId)
195{
196 auto it = versions.find(entryId);
197 if (it == versions.end())
198 {
199 return;
200 }
Eddie James6d873712017-09-01 11:29:07 -0500201
202 if (it->second->isFunctional())
203 {
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600204 log<level::ERR>(("Error: Version " + entryId +
205 " is currently running on the BMC."
206 " Unable to remove.")
207 .c_str());
Eddie James6d873712017-09-01 11:29:07 -0500208 return;
209 }
210
Leonel Gonzalez50d559c2017-07-07 14:38:25 -0500211 // Delete image dir
212 fs::path imageDirPath = (*(it->second)).path();
213 if (fs::exists(imageDirPath))
214 {
215 fs::remove_all(imageDirPath);
216 }
217 this->versions.erase(entryId);
218}
219
Gunnar Mills3027bba2017-04-27 15:49:03 -0500220int Manager::unTar(const std::string& tarFilePath,
221 const std::string& extractDirPath)
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500222{
223 if (tarFilePath.empty())
224 {
225 log<level::ERR>("Error TarFilePath is empty");
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500226 report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500227 return -1;
228 }
229 if (extractDirPath.empty())
230 {
231 log<level::ERR>("Error ExtractDirPath is empty");
Adriana Kobylak596466b2018-02-13 14:48:53 -0600232 report<UnTarFailure>(UnTarFail::PATH(extractDirPath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500233 return -1;
234 }
235
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600236 log<level::INFO>("Untaring", entry("FILENAME=%s", tarFilePath.c_str()),
Adriana Kobylak596466b2018-02-13 14:48:53 -0600237 entry("EXTRACTIONDIR=%s", extractDirPath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500238 int status = 0;
239 pid_t pid = fork();
240
241 if (pid == 0)
242 {
243 // child process
Adriana Kobylak2285fe02018-02-27 15:36:59 -0600244 execl("/bin/tar", "tar", "-xf", tarFilePath.c_str(), "-C",
245 extractDirPath.c_str(), (char*)0);
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500246 // execl only returns on fail
Adriana Kobylak596466b2018-02-13 14:48:53 -0600247 log<level::ERR>("Failed to execute untar file",
248 entry("FILENAME=%s", tarFilePath.c_str()));
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500249 report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500250 return -1;
251 }
252 else if (pid > 0)
253 {
254 waitpid(pid, &status, 0);
Adriana Kobylak596466b2018-02-13 14:48:53 -0600255 if (WEXITSTATUS(status))
256 {
257 log<level::ERR>("Failed to untar file",
258 entry("FILENAME=%s", tarFilePath.c_str()));
259 report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str()));
260 return -1;
261 }
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500262 }
263 else
264 {
265 log<level::ERR>("fork() failed.");
Gunnar Millsb30cadd2017-10-06 16:07:32 -0500266 report<UnTarFailure>(UnTarFail::PATH(tarFilePath.c_str()));
Gunnar Mills19e4ce52017-04-27 11:24:19 -0500267 return -1;
268 }
269
270 return 0;
271}
Gunnar Mills3027bba2017-04-27 15:49:03 -0500272
Gunnar Millse91d3212017-04-19 15:42:47 -0500273} // namespace manager
274} // namespace software
Gunnar Millsfa34e022018-09-04 10:05:45 -0500275} // namespace phosphor