提交 9ef45b43 编写于 作者: T Thomas Voß

Report errors if loopback device creation fails

上级 d8612d3e
......@@ -100,9 +100,17 @@ bool anbox::cmds::ContainerManager::setup_mounts() {
if (!fs::exists(android_rootfs_dir))
fs::create_directory(android_rootfs_dir);
auto loop_device = common::LoopDeviceAllocator::new_device();
if (!loop_device)
std::shared_ptr<common::LoopDevice> loop_device;
try {
loop_device = common::LoopDeviceAllocator::new_device();
} catch (const std::exception& e) {
ERROR("Could not create loopback device: %s", e.what());
return false;
} catch (...) {
ERROR("Could not create loopback device");
return false;
}
if (!loop_device->attach_file(android_img_path)) {
ERROR("Failed to attach Android rootfs image to loopback device");
......
......@@ -18,7 +18,10 @@
#include "anbox/common/loop_device.h"
#include "anbox/defer_action.h"
#include <system_error>
#include <linux/loop.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
......@@ -27,7 +30,7 @@ namespace common {
std::shared_ptr<LoopDevice> LoopDevice::create(const boost::filesystem::path &path) {
const auto fd = ::open(path.c_str(), O_RDWR);
if (fd < 0)
return nullptr;
throw std::system_error{errno, std::system_category()};
return std::shared_ptr<LoopDevice>(new LoopDevice(Fd{fd}, path));
}
......
......@@ -23,9 +23,12 @@
#include <boost/filesystem.hpp>
#include <linux/loop.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <system_error>
namespace fs = boost::filesystem;
namespace {
......@@ -38,19 +41,15 @@ namespace common {
std::shared_ptr<LoopDevice> LoopDeviceAllocator::new_device() {
const auto ctl_fd = ::open(loop_control_path, O_RDWR);
if (ctl_fd < 0)
return nullptr;
throw std::system_error{errno, std::system_category()};
DeferAction close_ctl_fd{[&]() { ::close(ctl_fd); }};
const auto device_nr = ::ioctl(ctl_fd, LOOP_CTL_GET_FREE);
if (device_nr < 0)
return nullptr;
const auto path = utils::string_format("%s%d", base_loop_path, device_nr);
if (!fs::exists(path))
return nullptr;
throw std::system_error{errno, std::system_category()};
return LoopDevice::create(path);
return LoopDevice::create(utils::string_format("%s%d", base_loop_path, device_nr));
}
} // namespace common
} // namespace anbox
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册