Darin Petkov | 6d5dbf6 | 2010-11-08 16:09:55 -0800 | [diff] [blame] | 1 | // Copyright (c) 2010 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" |
| 6 | #include <sys/mount.h> |
| 7 | #include <stdlib.h> |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 8 | #include <vector> |
| 9 | #include "update_engine/subprocess.h" |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 10 | #include "update_engine/utils.h" |
| 11 | |
| 12 | namespace chromeos_update_engine { |
| 13 | |
| 14 | using std::string; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 15 | using std::vector; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 16 | |
| 17 | namespace { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 18 | const char kPostinstallScript[] = "/postinst"; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | void PostinstallRunnerAction::PerformAction() { |
| 22 | CHECK(HasInputObject()); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 23 | const InstallPlan install_plan = GetInputObject(); |
| 24 | const string install_device = install_plan.install_path; |
| 25 | ScopedActionCompleter completer(processor_, this); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 26 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 27 | // Make mountpoint. |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 28 | TEST_AND_RETURN(utils::MakeTempDirectory("/tmp/au_postint_mount.XXXXXX", |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 29 | &temp_rootfs_dir_)); |
| 30 | ScopedDirRemover temp_dir_remover(temp_rootfs_dir_); |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 31 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 32 | unsigned long mountflags = MS_RDONLY; |
| 33 | int rc = mount(install_device.c_str(), |
| 34 | temp_rootfs_dir_.c_str(), |
Andrew de los Reyes | bfabc30 | 2011-01-31 17:23:50 -0800 | [diff] [blame] | 35 | "ext2", |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 36 | mountflags, |
| 37 | NULL); |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 38 | if (rc < 0) { |
Andrew de los Reyes | bfabc30 | 2011-01-31 17:23:50 -0800 | [diff] [blame] | 39 | LOG(INFO) << "Failed to mount install part as ext2. Trying ext3."; |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 40 | rc = mount(install_device.c_str(), |
| 41 | temp_rootfs_dir_.c_str(), |
| 42 | "ext3", |
| 43 | mountflags, |
| 44 | NULL); |
| 45 | } |
| 46 | if (rc < 0) { |
| 47 | LOG(ERROR) << "Unable to mount destination device " << install_device |
| 48 | << " onto " << temp_rootfs_dir_; |
| 49 | return; |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 52 | temp_dir_remover.set_should_remove(false); |
| 53 | completer.set_should_complete(false); |
| 54 | |
| 55 | // Runs the postinstall script asynchronously to free up the main loop while |
| 56 | // it's running. |
| 57 | vector<string> command; |
| 58 | command.push_back(temp_rootfs_dir_ + kPostinstallScript); |
| 59 | command.push_back(install_device); |
| 60 | Subprocess::Get().Exec(command, StaticCompletePostinstall, this); |
| 61 | } |
| 62 | |
| 63 | void PostinstallRunnerAction::CompletePostinstall(int return_code) { |
| 64 | ScopedActionCompleter completer(processor_, this); |
| 65 | ScopedTempUnmounter temp_unmounter(temp_rootfs_dir_); |
| 66 | if (return_code != 0) { |
| 67 | LOG(ERROR) << "Postinst command failed with code: " << return_code; |
Andrew de los Reyes | c1d5c93 | 2011-04-20 17:15:47 -0700 | [diff] [blame] | 68 | if (return_code == 2) { |
| 69 | // This special return code means that we tried to update firmware, |
| 70 | // but couldn't because we booted from FW B, and we need to reboot |
| 71 | // to get back to FW A. |
| 72 | completer.set_code(kActionCodePostinstallBootedFromFirmwareB); |
| 73 | } |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 74 | return; |
| 75 | } |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 76 | if (HasOutputPipe()) { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 77 | CHECK(HasInputObject()); |
| 78 | SetOutputObject(GetInputObject()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 79 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 80 | completer.set_code(kActionCodeSuccess); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 83 | void PostinstallRunnerAction::StaticCompletePostinstall(int return_code, |
| 84 | const string& output, |
| 85 | void* p) { |
| 86 | reinterpret_cast<PostinstallRunnerAction*>(p)->CompletePostinstall( |
| 87 | return_code); |
| 88 | } |
| 89 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 90 | } // namespace chromeos_update_engine |