Don't use sudo when not using libvirt
Move all uses of sudo to the libvirt manager
Bug: 110478603
Test: local & gce
Change-Id: Id82738636d79e2c797dcc335061fada2685f0ecf
Merged-In: Id82738636d79e2c797dcc335061fada2685f0ecf
(cherry picked from commit 38193249bbddc8682bf1bcbbe6142d46c9b94eae)
diff --git a/host/commands/launch/main.cc b/host/commands/launch/main.cc
index e6c0335..f08568a 100644
--- a/host/commands/launch/main.cc
+++ b/host/commands/launch/main.cc
@@ -298,29 +298,6 @@
return true;
}
-bool EnsureDirExists(const std::string& dir) {
- if (!cvd::DirectoryExists(dir.c_str())) {
- LOG(INFO) << "Setting up " << dir;
- if (mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
- if (errno == EACCES) {
- // TODO(79170615) Don't use sudo once libvirt is replaced
- LOG(WARNING) << "Not enough permission to create " << dir
- << " retrying with sudo";
- cvd::execute({"/usr/bin/sudo", "/bin/mkdir", "-m", "0775", dir});
-
- // When created with sudo the owner and group is root.
- std::string user_group = getenv("USER");
- user_group += ":libvirt-qemu";
- cvd::execute({"/usr/bin/sudo", "/bin/chown", user_group, dir});
- } else {
- LOG(FATAL) << "Unable to create " << dir << ". Error: " << errno;
- return false;
- }
- }
- }
- return true;
-}
-
std::string GetConfigFile() {
return vsoc::CuttlefishConfig::Get()->PerInstancePath(
"cuttlefish_config.json");
@@ -476,9 +453,6 @@
auto config = vsoc::CuttlefishConfig::Get();
// Set this first so that calls to PerInstancePath below are correct
config->set_instance_dir(FLAGS_instance_dir);
- if (!EnsureDirExists(FLAGS_instance_dir)) {
- return false;
- }
config->set_serial_number(FLAGS_serial_number);
@@ -572,7 +546,7 @@
return true;
}
-void ParseCommandLineFlags(int argc, char** argv) {
+bool ParseCommandLineFlags(int argc, char** argv) {
// The config_file is created by the launcher, so the launcher is the only
// host process that doesn't use the flag.
// Set the default to empty.
@@ -583,29 +557,8 @@
FLAGS_config_file = "";
ValidateAdbModeFlag();
-}
-bool CleanPriorFiles() {
- auto config = vsoc::CuttlefishConfig::Get();
- std::string run_files = config->PerInstancePath("*") + " " +
- config->mempath() + " " +
- config->cuttlefish_env_path();
- LOG(INFO) << "Assuming run files of " << run_files;
- // TODO(b/78512938): Shouldn't need sudo here
- std::string fuser_cmd = "sudo fuser " + run_files + " 2> /dev/null";
- int rval = std::system(fuser_cmd.c_str());
- // fuser returns 0 if any of the files are open
- if (WEXITSTATUS(rval) == 0) {
- LOG(ERROR) << "Clean aborted: files are in use";
- return false;
- }
- std::string clean_command = "sudo rm -rf " + run_files;
- rval = std::system(clean_command.c_str());
- if (WEXITSTATUS(rval) != 0) {
- LOG(ERROR) << "Remove of files failed";
- return false;
- }
- return true;
+ return ResolveInstanceFiles();
}
bool WriteCuttlefishEnvironment() {
@@ -628,24 +581,30 @@
env->Write(config_env.c_str(), config_env.size());
return true;
}
-
} // namespace
int main(int argc, char** argv) {
::android::base::InitLogging(argv, android::base::StderrLogger);
- ParseCommandLineFlags(argc, argv);
-
- if (!ResolveInstanceFiles()) {
+ if (!ParseCommandLineFlags(argc, argv)) {
return -1;
}
+
auto boot_img_unpacker = cvd::BootImageUnpacker::FromImage(FLAGS_boot_image);
+ auto vm_manager = vm_manager::VmManager::Get();
+
// Do this early so that the config object is ready for anything that needs it
if (!SetUpGlobalConfiguration(*boot_img_unpacker)) {
return -1;
}
- if (!CleanPriorFiles()) {
- LOG(FATAL) << "Failed to clean prior files";
+ if (!vm_manager->EnsureInstanceDirExists()) {
+ LOG(ERROR) << "Failed to create instance directory: " << FLAGS_instance_dir;
+ return -1;
+ }
+
+ if (!vm_manager->CleanPriorFiles()) {
+ LOG(ERROR) << "Failed to clean prior files";
+ return -1;
}
if (!UnpackBootImage(*boot_img_unpacker)) {
@@ -679,7 +638,6 @@
PreLaunchInitializers::Initialize();
// Start the guest VM
- auto vm_manager = vm_manager::VmManager::Get();
if (!vm_manager->Start()) {
LOG(FATAL) << "Unable to start vm_manager";
return -1;
diff --git a/host/commands/stop_cvd/main.cc b/host/commands/stop_cvd/main.cc
index ce2d02d..7f5752e 100644
--- a/host/commands/stop_cvd/main.cc
+++ b/host/commands/stop_cvd/main.cc
@@ -64,9 +64,8 @@
auto config = vsoc::CuttlefishConfig::Get();
- // TODO(b/78512938): Shouldn't need sudo to shut down
std::string run_files = config->PerInstancePath("*");
- std::string fuser_cmd = "sudo fuser -k ";
+ std::string fuser_cmd = "fuser -k ";
fuser_cmd += run_files;
fuser_cmd += " ";
fuser_cmd += config->mempath();
diff --git a/host/libs/vm_manager/libvirt_manager.cpp b/host/libs/vm_manager/libvirt_manager.cpp
index 3dbc9a1..1108444 100644
--- a/host/libs/vm_manager/libvirt_manager.cpp
+++ b/host/libs/vm_manager/libvirt_manager.cpp
@@ -16,6 +16,8 @@
#include "host/libs/vm_manager/libvirt_manager.h"
+#include <sys/stat.h>
+#include <sys/types.h>
#include <stdio.h>
#include <cstdlib>
#include <iomanip>
@@ -26,6 +28,8 @@
#include <glog/logging.h>
#include <libxml/tree.h>
+#include "common/libs/utils/files.h"
+#include "common/libs/utils/subprocess.h"
#include "host/libs/config/cuttlefish_config.h"
DEFINE_string(hypervisor_uri, "qemu:///system", "Hypervisor cannonical uri.");
@@ -331,7 +335,6 @@
}
} // namespace
-
bool LibvirtManager::Start() const {
std::string start_command = GetLibvirtCommand();
start_command += " create /dev/fd/0";
@@ -366,4 +369,39 @@
return std::system(stop_command.c_str()) == 0;
}
+bool LibvirtManager::EnsureInstanceDirExists() const {
+ auto instance_dir = vsoc::CuttlefishConfig::Get()->instance_dir();
+ if (!cvd::DirectoryExists(instance_dir)) {
+ LOG(INFO) << "Setting up " << instance_dir;
+ cvd::execute({"/usr/bin/sudo", "/bin/mkdir", "-m", "0775", instance_dir});
+
+ // When created with sudo the owner and group is root.
+ std::string user_group = getenv("USER");
+ user_group += ":libvirt-qemu";
+ cvd::execute({"/usr/bin/sudo", "/bin/chown", user_group, instance_dir});
+ }
+ return true;
+}
+
+bool LibvirtManager::CleanPriorFiles() const {
+ auto config = vsoc::CuttlefishConfig::Get();
+ std::string run_files = config->PerInstancePath("*") + " " +
+ config->mempath() + " " +
+ config->cuttlefish_env_path();
+ LOG(INFO) << "Assuming run files of " << run_files;
+ std::string fuser_cmd = "fuser " + run_files + " 2> /dev/null";
+ int rval = std::system(fuser_cmd.c_str());
+ // fuser returns 0 if any of the files are open
+ if (WEXITSTATUS(rval) == 0) {
+ LOG(ERROR) << "Clean aborted: files are in use";
+ return false;
+ }
+ std::string clean_command = "rm -rf " + run_files;
+ rval = std::system(clean_command.c_str());
+ if (WEXITSTATUS(rval) != 0) {
+ LOG(ERROR) << "Remove of files failed";
+ return false;
+ }
+ return true;
+}
} // namespace vm_manager
diff --git a/host/libs/vm_manager/libvirt_manager.h b/host/libs/vm_manager/libvirt_manager.h
index 9537af6..d773eff 100644
--- a/host/libs/vm_manager/libvirt_manager.h
+++ b/host/libs/vm_manager/libvirt_manager.h
@@ -26,6 +26,9 @@
bool Start() const override;
bool Stop() const override;
+
+ bool EnsureInstanceDirExists() const override;
+ bool CleanPriorFiles() const override;
};
} // namespace vm_manager
diff --git a/host/libs/vm_manager/qemu_manager.cpp b/host/libs/vm_manager/qemu_manager.cpp
index 16d0282..2eb8098 100644
--- a/host/libs/vm_manager/qemu_manager.cpp
+++ b/host/libs/vm_manager/qemu_manager.cpp
@@ -18,6 +18,7 @@
#include <string.h>
#include <sys/socket.h>
+#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
@@ -32,6 +33,7 @@
#include <gflags/gflags.h>
#include <glog/logging.h>
+#include "common/libs/utils/files.h"
#include "common/libs/utils/subprocess.h"
#include "host/libs/config/cuttlefish_config.h"
@@ -140,4 +142,37 @@
return true;
}
+bool QemuManager::EnsureInstanceDirExists() const {
+ auto instance_dir = vsoc::CuttlefishConfig::Get()->instance_dir();
+ if (!cvd::DirectoryExists(instance_dir.c_str())) {
+ LOG(INFO) << "Setting up " << instance_dir;
+ if (mkdir(instance_dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
+ LOG(ERROR) << "Unable to create " << instance_dir << ". Error: " << errno;
+ return false;
+ }
+ }
+ return true;
+
+}
+bool QemuManager::CleanPriorFiles() const {
+ auto config = vsoc::CuttlefishConfig::Get();
+ std::string run_files = config->PerInstancePath("*") + " " +
+ config->mempath() + " " +
+ config->cuttlefish_env_path();
+ LOG(INFO) << "Assuming run files of " << run_files;
+ std::string fuser_cmd = "fuser " + run_files + " 2> /dev/null";
+ int rval = std::system(fuser_cmd.c_str());
+ // fuser returns 0 if any of the files are open
+ if (WEXITSTATUS(rval) == 0) {
+ LOG(ERROR) << "Clean aborted: files are in use";
+ return false;
+ }
+ std::string clean_command = "rm -rf " + run_files;
+ rval = std::system(clean_command.c_str());
+ if (WEXITSTATUS(rval) != 0) {
+ LOG(ERROR) << "Remove of files failed";
+ return false;
+ }
+ return true;
+}
} // namespace vm_manager
diff --git a/host/libs/vm_manager/qemu_manager.h b/host/libs/vm_manager/qemu_manager.h
index 566c106..8180822 100644
--- a/host/libs/vm_manager/qemu_manager.h
+++ b/host/libs/vm_manager/qemu_manager.h
@@ -28,6 +28,9 @@
bool Start() const override;
bool Stop() const override;
+
+ bool EnsureInstanceDirExists() const override;
+ bool CleanPriorFiles() const override;
};
} // namespace vm_manager
diff --git a/host/libs/vm_manager/vm_manager.cpp b/host/libs/vm_manager/vm_manager.cpp
index 35e572f..961556c 100644
--- a/host/libs/vm_manager/vm_manager.cpp
+++ b/host/libs/vm_manager/vm_manager.cpp
@@ -28,4 +28,5 @@
: std::shared_ptr<VmManager>(new LibvirtManager()));
return vm_manager;
}
+
} // namespace vm_manager
diff --git a/host/libs/vm_manager/vm_manager.h b/host/libs/vm_manager/vm_manager.h
index a329571..f2e0122 100644
--- a/host/libs/vm_manager/vm_manager.h
+++ b/host/libs/vm_manager/vm_manager.h
@@ -31,6 +31,9 @@
virtual bool Start() const = 0;
virtual bool Stop() const = 0;
+
+ virtual bool EnsureInstanceDirExists() const = 0;
+ virtual bool CleanPriorFiles() const = 0;
};
} // namespace vm_manager