| 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 <sys/mount.h> | 
|  | 8 | #include <stdlib.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 |  | 
| Chris Sosa | be45bef | 2013-04-09 18:25:12 -0700 | [diff] [blame] | 11 | #include "update_engine/action_processor.h" | 
| Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 12 | #include "update_engine/subprocess.h" | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 13 | #include "update_engine/utils.h" | 
|  | 14 |  | 
|  | 15 | namespace chromeos_update_engine { | 
|  | 16 |  | 
|  | 17 | using std::string; | 
| Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 18 | using std::vector; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 19 |  | 
|  | 20 | namespace { | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 21 | const char kPostinstallScript[] = "/postinst"; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 22 | } | 
|  | 23 |  | 
|  | 24 | void PostinstallRunnerAction::PerformAction() { | 
|  | 25 | CHECK(HasInputObject()); | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 26 | install_plan_ = GetInputObject(); | 
|  | 27 | const string install_device = install_plan_.install_path; | 
| Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 28 | ScopedActionCompleter completer(processor_, this); | 
| Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 29 |  | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 30 | // Make mountpoint. | 
| Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 31 | TEST_AND_RETURN(utils::MakeTempDirectory("/tmp/au_postint_mount.XXXXXX", | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 32 | &temp_rootfs_dir_)); | 
|  | 33 | ScopedDirRemover temp_dir_remover(temp_rootfs_dir_); | 
| Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 34 |  | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 35 | unsigned long mountflags = MS_RDONLY; | 
|  | 36 | int rc = mount(install_device.c_str(), | 
|  | 37 | temp_rootfs_dir_.c_str(), | 
| Andrew de los Reyes | bfabc30 | 2011-01-31 17:23:50 -0800 | [diff] [blame] | 38 | "ext2", | 
| Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 39 | mountflags, | 
|  | 40 | NULL); | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 41 | // TODO(sosa): Remove once crbug.com/208022 is resolved. | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 42 | if (rc < 0) { | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 43 | LOG(INFO) << "Failed to mount install part " | 
|  | 44 | << install_device << " as ext2. Trying ext3."; | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 45 | rc = mount(install_device.c_str(), | 
|  | 46 | temp_rootfs_dir_.c_str(), | 
|  | 47 | "ext3", | 
|  | 48 | mountflags, | 
|  | 49 | NULL); | 
|  | 50 | } | 
|  | 51 | if (rc < 0) { | 
|  | 52 | LOG(ERROR) << "Unable to mount destination device " << install_device | 
|  | 53 | << " onto " << temp_rootfs_dir_; | 
|  | 54 | return; | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 55 | } | 
|  | 56 |  | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 57 | temp_dir_remover.set_should_remove(false); | 
|  | 58 | completer.set_should_complete(false); | 
|  | 59 |  | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 60 | if (install_plan_.powerwash_required) { | 
| Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 61 | if (utils::CreatePowerwashMarkerFile(powerwash_marker_file_)) { | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 62 | powerwash_marker_created_ = true; | 
|  | 63 | } else { | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 64 | completer.set_code(kErrorCodePostinstallPowerwashError); | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 65 | return; | 
|  | 66 | } | 
|  | 67 | } | 
|  | 68 |  | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 69 | // Runs the postinstall script asynchronously to free up the main loop while | 
|  | 70 | // it's running. | 
|  | 71 | vector<string> command; | 
|  | 72 | command.push_back(temp_rootfs_dir_ + kPostinstallScript); | 
|  | 73 | command.push_back(install_device); | 
| Darin Petkov | 58dd134 | 2011-05-06 12:05:13 -0700 | [diff] [blame] | 74 | if (!Subprocess::Get().Exec(command, StaticCompletePostinstall, this)) { | 
|  | 75 | CompletePostinstall(1); | 
|  | 76 | } | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 77 | } | 
|  | 78 |  | 
|  | 79 | void PostinstallRunnerAction::CompletePostinstall(int return_code) { | 
|  | 80 | ScopedActionCompleter completer(processor_, this); | 
|  | 81 | ScopedTempUnmounter temp_unmounter(temp_rootfs_dir_); | 
|  | 82 | if (return_code != 0) { | 
|  | 83 | LOG(ERROR) << "Postinst command failed with code: " << return_code; | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 84 |  | 
|  | 85 | // Undo any changes done to trigger Powerwash using clobber-state. | 
|  | 86 | if (powerwash_marker_created_) | 
| Gilad Arnold | 30dedd8 | 2013-07-03 06:19:09 -0700 | [diff] [blame] | 87 | utils::DeletePowerwashMarkerFile(powerwash_marker_file_); | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 88 |  | 
| Andrew de los Reyes | fe57d54 | 2011-06-07 09:00:36 -0700 | [diff] [blame] | 89 | if (return_code == 3) { | 
| Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 90 | // This special return code means that we tried to update firmware, | 
|  | 91 | // but couldn't because we booted from FW B, and we need to reboot | 
|  | 92 | // to get back to FW A. | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 93 | completer.set_code(kErrorCodePostinstallBootedFromFirmwareB); | 
| Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 94 | } | 
| Don Garrett | 81018e0 | 2013-07-30 18:46:31 -0700 | [diff] [blame] | 95 |  | 
|  | 96 | if (return_code == 4) { | 
|  | 97 | // This special return code means that we tried to update firmware, | 
|  | 98 | // but couldn't because we booted from FW B, and we need to reboot | 
|  | 99 | // to get back to FW A. | 
|  | 100 | completer.set_code(kErrorCodePostinstallFirmwareRONotUpdatable); | 
|  | 101 | } | 
|  | 102 |  | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 103 | return; | 
|  | 104 | } | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 105 |  | 
|  | 106 | LOG(INFO) << "Postinst command succeeded"; | 
| Chris Sosa | d317e40 | 2013-06-12 13:47:09 -0700 | [diff] [blame] | 107 | if (HasOutputPipe()) { | 
|  | 108 | SetOutputObject(install_plan_); | 
|  | 109 | } | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 110 |  | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 111 | completer.set_code(kErrorCodeSuccess); | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 112 | } | 
|  | 113 |  | 
| Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 114 | void PostinstallRunnerAction::StaticCompletePostinstall(int return_code, | 
|  | 115 | const string& output, | 
|  | 116 | void* p) { | 
|  | 117 | reinterpret_cast<PostinstallRunnerAction*>(p)->CompletePostinstall( | 
|  | 118 | return_code); | 
|  | 119 | } | 
|  | 120 |  | 
| adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 121 | }  // namespace chromeos_update_engine |