adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 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 { |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 18 | const string kPostinstallScript("/postinst"); |
| 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); |
| 26 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 27 | // Make mountpoint |
| 28 | string temp_dir; |
| 29 | TEST_AND_RETURN(utils::MakeTempDirectory("/tmp/au_postint_mount.XXXXXX", |
| 30 | &temp_dir)); |
| 31 | ScopedDirRemover temp_dir_remover(temp_dir); |
| 32 | |
| 33 | { |
| 34 | // Scope for the mount |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame^] | 35 | unsigned long mountflags = MS_RDONLY; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 36 | |
| 37 | int rc = mount(install_device.c_str(), |
| 38 | temp_dir.c_str(), |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame^] | 39 | "ext4", |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 40 | mountflags, |
| 41 | NULL); |
| 42 | if (rc < 0) { |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame^] | 43 | LOG(INFO) << "Failed to mount install part as ext4. Trying ext3."; |
| 44 | rc = mount(install_device.c_str(), |
| 45 | temp_dir.c_str(), |
| 46 | "ext3", |
| 47 | mountflags, |
| 48 | NULL); |
| 49 | } |
| 50 | if (rc < 0) { |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 51 | LOG(ERROR) << "Unable to mount destination device " << install_device |
| 52 | << " onto " << temp_dir; |
| 53 | return; |
| 54 | } |
| 55 | ScopedFilesystemUnmounter unmounter(temp_dir); |
| 56 | |
| 57 | // run postinstall script |
| 58 | vector<string> command; |
| 59 | command.push_back(temp_dir + kPostinstallScript); |
| 60 | command.push_back(install_device); |
| 61 | command.push_back(precommit_ ? "" : "--postcommit"); |
| 62 | rc = 0; |
| 63 | TEST_AND_RETURN(Subprocess::SynchronousExec(command, &rc)); |
| 64 | bool success = (rc == 0); |
| 65 | if (!success) { |
| 66 | LOG(ERROR) << "Postinst command failed with code: " << rc; |
| 67 | return; |
| 68 | } |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 71 | if (HasOutputPipe()) { |
| 72 | SetOutputObject(install_plan); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 73 | } |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 74 | completer.set_success(true); |
adlr@google.com | 3defe6a | 2009-12-04 20:57:17 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | } // namespace chromeos_update_engine |