blob: 21d7231d7eb8a55b8090627e42a9bd7bd0edb590 [file] [log] [blame]
Darin Petkovc1a8b422010-07-19 11:34:49 -07001// Copyright (c) 2010 The Chromium Authors. All rights reserved.
adlr@google.com3defe6a2009-12-04 20:57:17 +00002// 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 Reyesf9714432010-05-04 10:21:23 -07008#include <vector>
9#include "update_engine/subprocess.h"
adlr@google.com3defe6a2009-12-04 20:57:17 +000010#include "update_engine/utils.h"
11
12namespace chromeos_update_engine {
13
14using std::string;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070015using std::vector;
adlr@google.com3defe6a2009-12-04 20:57:17 +000016
17namespace {
adlr@google.com3defe6a2009-12-04 20:57:17 +000018const string kPostinstallScript("/postinst");
19}
20
21void PostinstallRunnerAction::PerformAction() {
22 CHECK(HasInputObject());
Andrew de los Reyesf9714432010-05-04 10:21:23 -070023 const InstallPlan install_plan = GetInputObject();
24 const string install_device = install_plan.install_path;
25 ScopedActionCompleter completer(processor_, this);
Darin Petkovc1a8b422010-07-19 11:34:49 -070026
Andrew de los Reyesf9714432010-05-04 10:21:23 -070027 // 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 Reyes3270f742010-07-15 22:28:14 -070035 unsigned long mountflags = MS_RDONLY;
Andrew de los Reyesf9714432010-05-04 10:21:23 -070036
37 int rc = mount(install_device.c_str(),
38 temp_dir.c_str(),
Andrew de los Reyes3270f742010-07-15 22:28:14 -070039 "ext4",
Andrew de los Reyesf9714432010-05-04 10:21:23 -070040 mountflags,
41 NULL);
42 if (rc < 0) {
Andrew de los Reyes3270f742010-07-15 22:28:14 -070043 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 Reyesf9714432010-05-04 10:21:23 -070051 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.com3defe6a2009-12-04 20:57:17 +000069 }
70
Andrew de los Reyesf9714432010-05-04 10:21:23 -070071 if (HasOutputPipe()) {
72 SetOutputObject(install_plan);
adlr@google.com3defe6a2009-12-04 20:57:17 +000073 }
Darin Petkovc1a8b422010-07-19 11:34:49 -070074 completer.set_code(kActionCodeSuccess);
adlr@google.com3defe6a2009-12-04 20:57:17 +000075}
76
77} // namespace chromeos_update_engine