Retry opening loop device

If more than the default number of loop devices is in use, we may need
to wait for the device path to be available.

Bug: 128873591
Bug: 122059364
Test: Set up adopted virtual disk and check that it loads on boot
Change-Id: I201dcc32043664076f50b0d6f40de6e5e1a65342
This commit is contained in:
Daniel Rosenberg 2019-04-01 16:09:28 -07:00
parent 444a24558d
commit 4538cb20b9

View file

@ -45,6 +45,7 @@ using android::base::StringPrintf;
using android::base::unique_fd;
static const char* kVoldPrefix = "vold:";
static constexpr size_t kLoopDeviceRetryAttempts = 3u;
int Loop::create(const std::string& target, std::string& out_device) {
unique_fd ctl_fd(open("/dev/loop-control", O_RDWR | O_CLOEXEC));
@ -61,7 +62,14 @@ int Loop::create(const std::string& target, std::string& out_device) {
out_device = StringPrintf("/dev/block/loop%d", num);
unique_fd target_fd(open(target.c_str(), O_RDWR | O_CLOEXEC));
unique_fd target_fd;
for (size_t i = 0; i != kLoopDeviceRetryAttempts; ++i) {
target_fd.reset(open(target.c_str(), O_RDWR | O_CLOEXEC));
if (target_fd.get() != -1) {
break;
}
usleep(50000);
}
if (target_fd.get() == -1) {
PLOG(ERROR) << "Failed to open " << target;
return -errno;