Skip to content

Commit

Permalink
Added support for ext4 ASEC resizing.
Browse files Browse the repository at this point in the history
ASECs formatted as ext4 can now be resized using vdc asec resize.
Refactored some common code.
Requires resize2fs.

Change-Id: Ie78bb6015114a7bc4af42b16d1f299322ffc1e2a
Signed-off-by: Daniel Rosenberg <drosen@google.com>
  • Loading branch information
drosen-google committed Jun 10, 2014
1 parent 73d7a02 commit fcd34a0
Show file tree
Hide file tree
Showing 8 changed files with 338 additions and 115 deletions.
8 changes: 8 additions & 0 deletions CommandListener.cpp
Expand Up @@ -359,6 +359,14 @@ int CommandListener::AsecCmd::runCommand(SocketClient *cli,
unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
const bool isExternal = (atoi(argv[7]) == 1);
rc = vm->createAsec(argv[2], numSectors, argv[4], argv[5], atoi(argv[6]), isExternal);
} else if (!strcmp(argv[1], "resize")) {
dumpArgs(argc, argv, -1);
if (argc != 5) {
cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: asec resize <container-id> <size_mb> <key>", false);
return 0;
}
unsigned int numSectors = (atoi(argv[3]) * (1024 * 1024)) / 512;
rc = vm->resizeAsec(argv[2], numSectors, argv[4]);
} else if (!strcmp(argv[1], "finalize")) {
dumpArgs(argc, argv, -1);
if (argc != 3) {
Expand Down
4 changes: 2 additions & 2 deletions Devmapper.cpp
Expand Up @@ -125,8 +125,8 @@ void Devmapper::ioctlInit(struct dm_ioctl *io, size_t dataSize,
io->flags = flags;
if (name) {
size_t ret = strlcpy(io->name, name, sizeof(io->name));
if (ret >= sizeof(io->name))
abort();
if (ret >= sizeof(io->name))
abort();
}
}

Expand Down
46 changes: 45 additions & 1 deletion Ext4.cpp
Expand Up @@ -43,7 +43,8 @@
#include "Ext4.h"
#include "VoldUtil.h"

#define MKEXT4FS_PATH "/system/bin/make_ext4fs";
#define MKEXT4FS_PATH "/system/bin/make_ext4fs"
#define RESIZE2FS_PATH "/system/bin/resize2fs"

int Ext4::doMount(const char *fsPath, const char *mountPoint, bool ro, bool remount,
bool executable) {
Expand All @@ -67,6 +68,49 @@ int Ext4::doMount(const char *fsPath, const char *mountPoint, bool ro, bool remo
return rc;
}

int Ext4::resize(const char *fspath, unsigned int numSectors) {
const char *args[4];
char* size_str;
int rc;
int status;

args[0] = RESIZE2FS_PATH;
args[1] = "-f";
args[2] = fspath;
if (asprintf(&size_str, "%ds", numSectors) < 0)
{
SLOGE("Filesystem (ext4) resize failed to set size");
return -1;
}
args[3] = size_str;
rc = android_fork_execvp(ARRAY_SIZE(args), (char **)args, &status, false,
true);
free(size_str);
if (rc != 0) {
SLOGE("Filesystem (ext4) resize failed due to logwrap error");
errno = EIO;
return -1;
}

if (!WIFEXITED(status)) {
SLOGE("Filesystem (ext4) resize did not exit properly");
errno = EIO;
return -1;
}

status = WEXITSTATUS(status);

if (status == 0) {
SLOGI("Filesystem (ext4) resized OK");
return 0;
} else {
SLOGE("Resize (ext4) failed (unknown exit code %d)", status);
errno = EIO;
return -1;
}
return 0;
}

int Ext4::format(const char *fsPath, unsigned int numSectors, const char *mountpoint) {
int fd;
const char *args[7];
Expand Down
1 change: 1 addition & 0 deletions Ext4.h
Expand Up @@ -24,6 +24,7 @@ class Ext4 {
static int doMount(const char *fsPath, const char *mountPoint, bool ro, bool remount,
bool executable);
static int format(const char *fsPath, unsigned int numSectors, const char *mountpoint);
static int resize(const char *fsPath, unsigned int numSectors);
};

#endif
28 changes: 28 additions & 0 deletions Loop.cpp
Expand Up @@ -249,6 +249,34 @@ int Loop::createImageFile(const char *file, unsigned int numSectors) {
return 0;
}

int Loop::resizeImageFile(const char *file, unsigned int numSectors) {
int fd;

if ((fd = open(file, O_RDWR)) < 0) {
SLOGE("Error opening imagefile (%s)", strerror(errno));
return -1;
}

SLOGD("Attempting to increase size of %s to %d sectors.", file, numSectors);

if (fallocate(fd, 0, 0, numSectors * 512)) {
if (errno == ENOSYS) {
SLOGW("fallocate not found. Falling back to ftruncate.");
if (ftruncate(fd, numSectors * 512) < 0) {
SLOGE("Error truncating imagefile (%s)", strerror(errno));
close(fd);
return -1;
}
} else {
SLOGE("Error allocating space (%s)", strerror(errno));
close(fd);
return -1;
}
}
close(fd);
return 0;
}

int Loop::lookupInfo(const char *loopDevice, struct asec_superblock *sb, unsigned int *nr_sec) {
int fd;
struct asec_superblock buffer;
Expand Down
1 change: 1 addition & 0 deletions Loop.h
Expand Up @@ -32,6 +32,7 @@ class Loop {
static int destroyByDevice(const char *loopDevice);
static int destroyByFile(const char *loopFile);
static int createImageFile(const char *file, unsigned int numSectors);
static int resizeImageFile(const char *file, unsigned int numSectors);

static int dumpState(SocketClient *c);
};
Expand Down

0 comments on commit fcd34a0

Please sign in to comment.