blob: 9a84563a882117b6804ee4ce6a6cdd459b8049bc [file] [log] [blame]
Darin Petkov6d5dbf62010-11-08 16:09:55 -08001// Copyright (c) 2010 The Chromium OS 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);
Andrew de los Reyesf9714432010-05-04 10:21:23 -070061 rc = 0;
62 TEST_AND_RETURN(Subprocess::SynchronousExec(command, &rc));
63 bool success = (rc == 0);
64 if (!success) {
65 LOG(ERROR) << "Postinst command failed with code: " << rc;
66 return;
67 }
adlr@google.com3defe6a2009-12-04 20:57:17 +000068 }
69
Andrew de los Reyesf9714432010-05-04 10:21:23 -070070 if (HasOutputPipe()) {
71 SetOutputObject(install_plan);
adlr@google.com3defe6a2009-12-04 20:57:17 +000072 }
Darin Petkovc1a8b422010-07-19 11:34:49 -070073 completer.set_code(kActionCodeSuccess);
adlr@google.com3defe6a2009-12-04 20:57:17 +000074}
75
76} // namespace chromeos_update_engine