#include <stdio.h>
#include <stdlib.h>
+#include <inttypes.h>
#include <string.h>
#include <unistd.h>
#include "c-ctype.h"
#include "actions.h"
+/* Confirmed this is true up to ext4 from the Linux sources. */
+#define EXT2_LABEL_MAX 16
+
/* Choose which tools like mke2fs to use. For RHEL 5 (only) there
* is a special set of tools which support ext2/3/4. eg. On RHEL 5,
* mke2fs only supports ext2/3, but mke4fs supports ext2/3/4.
if (e2prog (prog) == -1)
return -1;
+ if (strlen (label) > EXT2_LABEL_MAX) {
+ reply_with_error ("%s: ext2 labels are limited to %d bytes",
+ label, EXT2_LABEL_MAX);
+ return -1;
+ }
+
r = command (NULL, &err, prog, device, label, NULL);
if (r == -1) {
reply_with_error ("%s", err);
char *
do_get_e2label (const char *device)
{
- int r, len;
- char *out, *err;
-
- char prog[] = "e2label";
- if (e2prog (prog) == -1)
- return NULL;
-
- r = command (&out, &err, prog, device, NULL);
- if (r == -1) {
- reply_with_error ("%s", err);
- free (out);
- free (err);
- return NULL;
- }
-
- free (err);
-
- /* Remove any trailing \n from the label. */
- len = strlen (out);
- if (len > 0 && out[len-1] == '\n')
- out[len-1] = '\0';
-
- return out; /* caller frees */
+ return do_vfs_label (device);
}
int
char *
do_get_e2uuid (const char *device)
{
+ return do_vfs_uuid (device);
+}
+
+int
+do_resize2fs (const char *device)
+{
+ char *err;
int r;
- char *out, *err, *p, *q;
- /* It's not so straightforward to get the volume UUID. We have
- * to use tune2fs -l and then look for a particular string in
- * the output.
- */
- char prog[] = "tune2fs";
+ char prog[] = "resize2fs";
if (e2prog (prog) == -1)
- return NULL;
+ return -1;
- r = command (&out, &err, prog, "-l", device, NULL);
+ r = command (NULL, &err, prog, device, NULL);
if (r == -1) {
reply_with_error ("%s", err);
- free (out);
free (err);
- return NULL;
+ return -1;
}
free (err);
-
- /* Look for /\nFilesystem UUID:\s+/ in the output. */
- p = strstr (out, "\nFilesystem UUID:");
- if (p == NULL) {
- reply_with_error ("no Filesystem UUID in the output of tune2fs -l");
- free (out);
- return NULL;
- }
-
- p += 17;
- while (*p && c_isspace (*p))
- p++;
- if (!*p) {
- reply_with_error ("malformed Filesystem UUID in the output of tune2fs -l");
- free (out);
- return NULL;
- }
-
- /* Now 'p' hopefully points to the start of the UUID. */
- q = p;
- while (*q && (c_isxdigit (*q) || *q == '-'))
- q++;
- if (!*q) {
- reply_with_error ("malformed Filesystem UUID in the output of tune2fs -l");
- free (out);
- return NULL;
- }
-
- *q = '\0';
-
- p = strdup (p);
- if (!p) {
- reply_with_perror ("strdup");
- free (out);
- return NULL;
- }
-
- free (out);
- return p; /* caller frees */
+ return 0;
}
int
-do_resize2fs (const char *device)
+do_resize2fs_size (const char *device, int64_t size)
{
char *err;
int r;
if (e2prog (prog) == -1)
return -1;
- r = command (NULL, &err, prog, device, NULL);
+ /* resize2fs itself may impose additional limits. Since we are
+ * going to use the 'K' suffix however we can only work with whole
+ * kilobytes.
+ */
+ if (size & 1023) {
+ reply_with_error ("%" PRIi64 ": size must be a round number of kilobytes",
+ size);
+ return -1;
+ }
+ size /= 1024;
+
+ char buf[32];
+ snprintf (buf, sizeof buf, "%" PRIi64 "K", size);
+
+ r = command (NULL, &err, prog, device, buf, NULL);
if (r == -1) {
reply_with_error ("%s", err);
free (err);
if (e2prog (prog) == -1)
return -1;
+ if (strlen (label) > EXT2_LABEL_MAX) {
+ reply_with_error ("%s: ext2 labels are limited to %d bytes",
+ label, EXT2_LABEL_MAX);
+ return -1;
+ }
+
char blocksize_s[32];
snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);
if (e2prog (prog) == -1)
return -1;
+ if (strlen (label) > EXT2_LABEL_MAX) {
+ reply_with_error ("%s: ext2 labels are limited to %d bytes",
+ label, EXT2_LABEL_MAX);
+ return -1;
+ }
+
char blocksize_s[32];
snprintf (blocksize_s, sizeof blocksize_s, "%d", blocksize);