Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "update_engine/postinstall_runner_action.h" |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 6 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 7 | #include <stdlib.h> |
Alex Vakulenko | 44cab30 | 2014-07-23 13:12:15 -0700 | [diff] [blame] | 8 | #include <sys/mount.h> |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 9 | #include <vector> |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 10 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 11 | #include <base/bind.h> |
| 12 | |
Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 13 | #include "update_engine/action_processor.h" |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 14 | #include "update_engine/subprocess.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 15 | #include "update_engine/utils.h" |
| 16 | |
| 17 | namespace chromeos_update_engine { |
| 18 | |
| 19 | using std::string; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 20 | using std::vector; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 21 | |
| 22 | namespace { |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 23 | // The absolute path to the post install command. |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 24 | const char kPostinstallScript[] = "/postinst"; |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 25 | |
| 26 | // Path to the binary file used by kPostinstallScript. Used to get and log the |
| 27 | // file format of the binary to debug issues when the ELF format on the update |
| 28 | // doesn't match the one on the current system. This path is not executed. |
| 29 | const char kDebugPostinstallBinaryPath[] = "/usr/bin/cros_installer"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | void PostinstallRunnerAction::PerformAction() { |
| 33 | CHECK(HasInputObject()); |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 34 | install_plan_ = GetInputObject(); |
| 35 | const string install_device = install_plan_.install_path; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 36 | ScopedActionCompleter completer(processor_, this); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 37 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 38 | // Make mountpoint. |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 39 | TEST_AND_RETURN(utils::MakeTempDirectory("/tmp/au_postint_mount.XXXXXX", |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 40 | &temp_rootfs_dir_)); |
| 41 | ScopedDirRemover temp_dir_remover(temp_rootfs_dir_); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 42 | |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 43 | const string mountable_device = |
| 44 | utils::MakePartitionNameForMount(install_device); |
| 45 | if (mountable_device.empty()) { |
| 46 | LOG(ERROR) << "Cannot make mountable device from " << install_device; |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 47 | return; |
Nam T. Nguyen | a78b28c | 2015-03-06 22:30:12 -0800 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | if (!utils::MountFilesystem(mountable_device, temp_rootfs_dir_, MS_RDONLY)) |
| 51 | return; |
| 52 | |
| 53 | LOG(INFO) << "Performing postinst with install device " << install_device |
| 54 | << " and mountable device " << mountable_device; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 55 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 56 | temp_dir_remover.set_should_remove(false); |
| 57 | completer.set_should_complete(false); |
| 58 | |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 59 | if (install_plan_.powerwash_required) { |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 60 | if (utils::CreatePowerwashMarkerFile(powerwash_marker_file_)) { |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 61 | powerwash_marker_created_ = true; |
| 62 | } else { |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 63 | completer.set_code(ErrorCode::kPostinstallPowerwashError); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 64 | return; |
| 65 | } |
| 66 | } |
| 67 | |
Alex Deymo | 032e772 | 2014-03-25 17:53:56 -0700 | [diff] [blame] | 68 | // Logs the file format of the postinstall script we are about to run. This |
| 69 | // will help debug when the postinstall script doesn't match the architecture |
| 70 | // of our build. |
| 71 | LOG(INFO) << "Format file for new " << kPostinstallScript << " is: " |
| 72 | << utils::GetFileFormat(temp_rootfs_dir_ + kPostinstallScript); |
| 73 | LOG(INFO) << "Format file for new " << kDebugPostinstallBinaryPath << " is: " |
| 74 | << utils::GetFileFormat( |
| 75 | temp_rootfs_dir_ + kDebugPostinstallBinaryPath); |
| 76 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 77 | // Runs the postinstall script asynchronously to free up the main loop while |
| 78 | // it's running. |
| 79 | vector<string> command; |
Chris Sosa | 000ecb3 | 2014-04-23 12:21:18 -0700 | [diff] [blame] | 80 | if (!install_plan_.download_url.empty()) { |
| 81 | command.push_back(temp_rootfs_dir_ + kPostinstallScript); |
| 82 | } else { |
| 83 | // TODO(sosa): crbug.com/366207. |
| 84 | // If we're doing a rollback, just run our own postinstall. |
| 85 | command.push_back(kPostinstallScript); |
| 86 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 87 | command.push_back(install_device); |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 88 | if (!Subprocess::Get().Exec(command, |
| 89 | base::Bind( |
| 90 | &PostinstallRunnerAction::CompletePostinstall, |
| 91 | base::Unretained(this)))) { |
| 92 | CompletePostinstall(1, "Postinstall didn't launch"); |
Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 93 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Alex Deymo | 461b259 | 2015-07-24 20:10:52 -0700 | [diff] [blame] | 96 | void PostinstallRunnerAction::CompletePostinstall(int return_code, |
| 97 | const string& output) { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 98 | ScopedActionCompleter completer(processor_, this); |
| 99 | ScopedTempUnmounter temp_unmounter(temp_rootfs_dir_); |
| 100 | if (return_code != 0) { |
| 101 | LOG(ERROR) << "Postinst command failed with code: " << return_code; |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 102 | |
| 103 | // Undo any changes done to trigger Powerwash using clobber-state. |
| 104 | if (powerwash_marker_created_) |
Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 105 | utils::DeletePowerwashMarkerFile(powerwash_marker_file_); |
Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 106 | |
Andrew de los Reyes | fe57d54 | 2011-06-07 09:00:36 -0700 | [diff] [blame] | 107 | if (return_code == 3) { |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 108 | // This special return code means that we tried to update firmware, |
| 109 | // but couldn't because we booted from FW B, and we need to reboot |
| 110 | // to get back to FW A. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 111 | completer.set_code(ErrorCode::kPostinstallBootedFromFirmwareB); |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 112 | } |
Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 113 | |
| 114 | if (return_code == 4) { |
| 115 | // This special return code means that we tried to update firmware, |
| 116 | // but couldn't because we booted from FW B, and we need to reboot |
| 117 | // to get back to FW A. |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 118 | completer.set_code(ErrorCode::kPostinstallFirmwareRONotUpdatable); |
Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 121 | return; |
| 122 | } |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 123 | |
| 124 | LOG(INFO) << "Postinst command succeeded"; |
Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 125 | if (HasOutputPipe()) { |
| 126 | SetOutputObject(install_plan_); |
| 127 | } |
Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 128 | |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 129 | completer.set_code(ErrorCode::kSuccess); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 130 | } |
| 131 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 132 | } // namespace chromeos_update_engine |