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(), |
| 35 | "ext4", |
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) { |
| 39 | LOG(INFO) << "Failed to mount install part as ext4. Trying ext3."; |
| 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; |
| 68 | return; |
| 69 | } |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 70 | if (HasOutputPipe()) { |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 71 | CHECK(HasInputObject()); |
| 72 | SetOutputObject(GetInputObject()); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 73 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 74 | completer.set_code(kActionCodeSuccess); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Darin Petkov | 6f03a3b | 2010-11-10 14:27:14 -0800 | [diff] [blame] | 77 | void PostinstallRunnerAction::StaticCompletePostinstall(int return_code, |
| 78 | const string& output, |
| 79 | void* p) { |
| 80 | reinterpret_cast<PostinstallRunnerAction*>(p)->CompletePostinstall( |
| 81 | return_code); |
| 82 | } |
| 83 | |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 84 | } // namespace chromeos_update_engine |