blob: fe468cc6fcf641872b61eb7bbb05ac4fe4905569 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
2// Copyright (C) 2011 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
adlr@google.com3defe6a2009-12-04 20:57:17 +000016
Alex Deymo39910dc2015-11-09 17:04:30 -080017#include "update_engine/payload_consumer/postinstall_runner_action.h"
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070018
adlr@google.com3defe6a2009-12-04 20:57:17 +000019#include <stdlib.h>
Alex Vakulenko44cab302014-07-23 13:12:15 -070020#include <sys/mount.h>
Andrew de los Reyesf9714432010-05-04 10:21:23 -070021#include <vector>
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070022
Alex Deymo461b2592015-07-24 20:10:52 -070023#include <base/bind.h>
Alex Deymoe5e5fe92015-10-05 09:28:19 -070024#include <base/files/file_path.h>
25#include <base/files/file_util.h>
Alex Deymo390efed2016-02-18 11:00:40 -080026#include <base/strings/string_util.h>
Alex Deymo461b2592015-07-24 20:10:52 -070027
Alex Deymo39910dc2015-11-09 17:04:30 -080028#include "update_engine/common/action_processor.h"
Alex Deymob15a0b82015-11-25 20:30:40 -030029#include "update_engine/common/boot_control_interface.h"
Alex Deymo39910dc2015-11-09 17:04:30 -080030#include "update_engine/common/subprocess.h"
31#include "update_engine/common/utils.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000032
33namespace chromeos_update_engine {
34
35using std::string;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070036using std::vector;
adlr@google.com3defe6a2009-12-04 20:57:17 +000037
adlr@google.com3defe6a2009-12-04 20:57:17 +000038void PostinstallRunnerAction::PerformAction() {
39 CHECK(HasInputObject());
Chris Sosad317e402013-06-12 13:47:09 -070040 install_plan_ = GetInputObject();
Darin Petkov6f03a3b2010-11-10 14:27:14 -080041
Chris Sosad317e402013-06-12 13:47:09 -070042 if (install_plan_.powerwash_required) {
Gilad Arnold30dedd82013-07-03 06:19:09 -070043 if (utils::CreatePowerwashMarkerFile(powerwash_marker_file_)) {
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070044 powerwash_marker_created_ = true;
45 } else {
Alex Deymoe5e5fe92015-10-05 09:28:19 -070046 return CompletePostinstall(ErrorCode::kPostinstallPowerwashError);
Jay Srinivasan1c0fe792013-03-28 16:45:25 -070047 }
48 }
49
Alex Deymoe5e5fe92015-10-05 09:28:19 -070050 PerformPartitionPostinstall();
51}
52
53void PostinstallRunnerAction::PerformPartitionPostinstall() {
Alex Deymo390efed2016-02-18 11:00:40 -080054 if (install_plan_.download_url.empty()) {
55 LOG(INFO) << "Skipping post-install during rollback";
56 return CompletePostinstall(ErrorCode::kSuccess);
57 }
58
Alex Deymoe5e5fe92015-10-05 09:28:19 -070059 // Skip all the partitions that don't have a post-install step.
60 while (current_partition_ < install_plan_.partitions.size() &&
61 !install_plan_.partitions[current_partition_].run_postinstall) {
62 VLOG(1) << "Skipping post-install on partition "
63 << install_plan_.partitions[current_partition_].name;
64 current_partition_++;
65 }
66 if (current_partition_ == install_plan_.partitions.size())
67 return CompletePostinstall(ErrorCode::kSuccess);
68
69 const InstallPlan::Partition& partition =
70 install_plan_.partitions[current_partition_];
71
72 const string mountable_device =
73 utils::MakePartitionNameForMount(partition.target_path);
74 if (mountable_device.empty()) {
75 LOG(ERROR) << "Cannot make mountable device from " << partition.target_path;
76 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
77 }
78
79 // Perform post-install for the current_partition_ partition. At this point we
80 // need to call CompletePartitionPostinstall to complete the operation and
81 // cleanup.
Alex Deymo390efed2016-02-18 11:00:40 -080082#ifdef __ANDROID__
83 fs_mount_dir_ = "/postinstall";
84#else // __ANDROID__
Alex Deymoe5e5fe92015-10-05 09:28:19 -070085 TEST_AND_RETURN(
Alex Deymo390efed2016-02-18 11:00:40 -080086 utils::MakeTempDirectory("au_postint_mount.XXXXXX", &fs_mount_dir_));
87#endif // __ANDROID__
Alex Deymoe5e5fe92015-10-05 09:28:19 -070088
Alex Deymo390efed2016-02-18 11:00:40 -080089 string abs_path = base::FilePath(fs_mount_dir_)
90 .AppendASCII(partition.postinstall_path)
91 .value();
92 if (!base::StartsWith(
93 abs_path, fs_mount_dir_, base::CompareCase::SENSITIVE)) {
94 LOG(ERROR) << "Invalid relative postinstall path: "
95 << partition.postinstall_path;
96 return CompletePostinstall(ErrorCode::kPostinstallRunnerError);
97 }
98
99 if (!utils::MountFilesystem(mountable_device,
100 fs_mount_dir_,
101 MS_RDONLY,
102 partition.filesystem_type)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700103 return CompletePartitionPostinstall(
104 1, "Error mounting the device " + mountable_device);
105 }
106
Alex Deymo390efed2016-02-18 11:00:40 -0800107 LOG(INFO) << "Performing postinst (" << partition.postinstall_path << " at "
108 << abs_path << ") installed on device " << partition.target_path
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700109 << " and mountable device " << mountable_device;
110
Alex Deymo032e7722014-03-25 17:53:56 -0700111 // Logs the file format of the postinstall script we are about to run. This
112 // will help debug when the postinstall script doesn't match the architecture
113 // of our build.
Alex Deymo390efed2016-02-18 11:00:40 -0800114 LOG(INFO) << "Format file for new " << partition.postinstall_path
115 << " is: " << utils::GetFileFormat(abs_path);
Alex Deymo032e7722014-03-25 17:53:56 -0700116
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800117 // Runs the postinstall script asynchronously to free up the main loop while
118 // it's running.
Alex Deymo390efed2016-02-18 11:00:40 -0800119 vector<string> command = {abs_path, partition.target_path};
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700120 if (!Subprocess::Get().Exec(
121 command,
122 base::Bind(
123 &PostinstallRunnerAction::CompletePartitionPostinstall,
124 base::Unretained(this)))) {
125 CompletePartitionPostinstall(1, "Postinstall didn't launch");
Darin Petkov58dd1342011-05-06 12:05:13 -0700126 }
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800127}
128
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700129void PostinstallRunnerAction::CompletePartitionPostinstall(
130 int return_code,
131 const string& output) {
Alex Deymo390efed2016-02-18 11:00:40 -0800132 utils::UnmountFilesystem(fs_mount_dir_);
133#ifndef ANDROID
134 if (!base::DeleteFile(base::FilePath(fs_mount_dir_), false)) {
135 PLOG(WARNING) << "Not removing temporary mountpoint " << fs_mount_dir_;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700136 }
Alex Deymo390efed2016-02-18 11:00:40 -0800137#endif // !ANDROID
138 fs_mount_dir_.clear();
Alex Deymo31d95ac2015-09-17 11:56:18 -0700139
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800140 if (return_code != 0) {
141 LOG(ERROR) << "Postinst command failed with code: " << return_code;
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700142 ErrorCode error_code = ErrorCode::kPostinstallRunnerError;
Jay Srinivasan1c0fe792013-03-28 16:45:25 -0700143
Andrew de los Reyesfe57d542011-06-07 09:00:36 -0700144 if (return_code == 3) {
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700145 // This special return code means that we tried to update firmware,
146 // but couldn't because we booted from FW B, and we need to reboot
147 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700148 error_code = ErrorCode::kPostinstallBootedFromFirmwareB;
Andrew de los Reyesc1d5c932011-04-20 17:15:47 -0700149 }
Don Garrett81018e02013-07-30 18:46:31 -0700150
151 if (return_code == 4) {
152 // This special return code means that we tried to update firmware,
153 // but couldn't because we booted from FW B, and we need to reboot
154 // to get back to FW A.
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700155 error_code = ErrorCode::kPostinstallFirmwareRONotUpdatable;
Don Garrett81018e02013-07-30 18:46:31 -0700156 }
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700157 return CompletePostinstall(error_code);
158 }
159 current_partition_++;
160 PerformPartitionPostinstall();
161}
162
163void PostinstallRunnerAction::CompletePostinstall(ErrorCode error_code) {
164 // We only attempt to mark the new slot as active if all the postinstall
165 // steps succeeded.
166 if (error_code == ErrorCode::kSuccess &&
Alex Deymob15a0b82015-11-25 20:30:40 -0300167 !boot_control_->SetActiveBootSlot(install_plan_.target_slot)) {
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700168 error_code = ErrorCode::kPostinstallRunnerError;
169 }
170
171 ScopedActionCompleter completer(processor_, this);
172
173 if (error_code != ErrorCode::kSuccess) {
174 LOG(ERROR) << "Postinstall action failed.";
175
176 // Undo any changes done to trigger Powerwash using clobber-state.
177 if (powerwash_marker_created_)
178 utils::DeletePowerwashMarkerFile(powerwash_marker_file_);
Don Garrett81018e02013-07-30 18:46:31 -0700179
Darin Petkov6f03a3b2010-11-10 14:27:14 -0800180 return;
181 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700182
Alex Deymoe5e5fe92015-10-05 09:28:19 -0700183 LOG(INFO) << "All post-install commands succeeded";
Chris Sosad317e402013-06-12 13:47:09 -0700184 if (HasOutputPipe()) {
185 SetOutputObject(install_plan_);
186 }
Jay Srinivasanae4697c2013-03-18 17:08:08 -0700187
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -0700188 completer.set_code(ErrorCode::kSuccess);
adlr@google.com3defe6a2009-12-04 20:57:17 +0000189}
190
adlr@google.com3defe6a2009-12-04 20:57:17 +0000191} // namespace chromeos_update_engine