1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
/*
* Copyright (C) 2004 Red Hat GmbH. All rights reserved.
*
* LVM is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*/
/*
* FIXME: pass smart resizer arguments through from lvresize
* (eg, for xfs_growfs)
*/
/* FIXME All funcs to return 0 on success or an error from errno.h on failure */
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#define MAX_ARGS 8
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <inttypes.h>
#include <fstab.h>
#include <sys/mount.h>
#include <sys/vfs.h>
#define log_error(str, x...) fprintf(stderr, "%s(%u): " str "\n", __FILE__, __LINE__, x)
/* Filesystem related information */
struct fsinfo {
struct fstab *fsent;
struct statfs statfs;
uint64_t new_size;
const char *cmd;
};
static void _usage(const char *cmd)
{
log_error("Usage: %s [check <filesystem> | resize <filesystem> <size>]",
basename(cmd));
}
/* FIXME Make this more robust - /proc, multiple mounts, TMPDIR + security etc. */
/* FIXME Ensure filesystem is not mounted anywhere before running fsck/resize */
/* Gather filesystem information (VFS type, special file and block size) */
static int _get_fsinfo(const char *file, struct fsinfo *fsinfo)
{
char *dir, template[] = "/tmp/fscmd_XXXXXX";
struct stat info;
int ret = 0;
if (stat(file, &info)) {
log_error("%s: stat failed: %s", file, strerror(errno));
return errno;
}
/* FIXME: are we limited to /etc/fstab entries ? */
if (!(fsinfo->fsent = info.st_rdev ? getfsspec(file) : getfsfile(file))) {
log_error("%s: getfsspec/getfsfile failed: "
"Missing from /etc/fstab?", file);
return EINVAL;
}
/* FIXME: any other way to retrieve fs blocksize avoiding mounting ? */
if (!(dir = (mkdtemp(template)))) {
log_error("%s: mkdtemp failed: %s", template, strerror(errno));
return errno;
}
if (mount(fsinfo->fsent->fs_spec, dir, fsinfo->fsent->fs_vfstype,
MS_RDONLY, NULL)) {
log_error("%s: mount %s failed: %s", fsinfo->fsent->fs_spec,
dir, strerror(errno));
ret = errno;
goto out;
}
if (statfs(dir, &fsinfo->statfs)) {
log_error("%s: statfs failed: %s", dir, strerror(errno));
ret = errno;
goto out1;
}
out1:
if (umount(dir))
log_error("%s: umount failed: %s", dir, strerror(errno));
out:
if (rmdir(dir))
log_error("%s: rmdir failed: %s", dir, strerror(errno));
return ret;
}
enum {
BLOCKS,
KILOBYTES
};
#define LEN 32 /* Length of temporary string */
/* Create size string in units of blocks or kilobytes
* (size expected in kilobytes)
*/
static char *_size_str(struct fsinfo *fsinfo, int unit)
{
uint64_t new_size = fsinfo->new_size;
static char s[LEN];
/* Avoid switch() as long as there's only 2 cases */
snprintf(s, LEN, "%" PRIu64,
unit == BLOCKS ? new_size / (fsinfo->statfs.f_bsize >> 10) :
new_size);
return s;
}
/* Allocate memory and store string updating string pointer */
static int _store(char **arg, char **str)
{
size_t len = 0;
char *s = *str;
while (*s && *s != '%' && *(s++) != ' ')
len++;
if ((*arg = (char *) malloc(len + 1))) {
strncpy(*arg, *str, len);
(*arg)[len] = '\0';
*str = s - 1;
return 0;
}
return 1;
}
/* Construct a new argument vector for the real external command */
static int _new_argv(char **new_argv, struct fsinfo *fsinfo)
{
int i = 0, b = 0, d = 0, k = 0, m = 0;
char *s1;
char *s = (char *) fsinfo->cmd;
if (!s || !strlen(s))
return 1;
do {
switch (*s) {
case ' ':
break;
case '%':
s++;
switch (tolower(*s)) {
case 'b':
s1 = _size_str(fsinfo, BLOCKS);
if (b++ + k || _store(&new_argv[i++], &s1))
goto error;
break;
case 'k':
s1 = _size_str(fsinfo, KILOBYTES);
if (b + k++ || _store(&new_argv[i++], &s1))
goto error;
break;
case 'd':
s1 = fsinfo->fsent->fs_spec;
if (d++ + m || _store(&new_argv[i++], &s1))
goto error;
break;
case 'm':
s1 = fsinfo->fsent->fs_file;
if (d + m++ || _store(&new_argv[i++], &s1))
goto error;
break;
default:
goto error;
}
break;
default:
if (_store(&new_argv[i++], &s))
goto error;
}
} while (*++s);
new_argv[i] = NULL;
return 0;
error:
new_argv[i] = NULL;
log_error("Failed constructing arguments for %s", s);
return EINVAL;
}
/*
* Get filesystem command arguments derived from a command definition string
*
* Command definition syntax: 'cmd [-option]{0,} [(option)argument]{0,}'
*
* (option)argument can be: '%{bdkm}'
*
* Command definition is parsed into argument strings of
* an argument vector with:
*
* %b replaced by the size in filesystem blocks
* %k replaced by the size in kilobytes
* %d replaced by the name of the device special of the LV
* %m replaced by the mountpoint of the filesystem
*
*/
static int _get_cmd(char *command, struct fsinfo *fsinfo)
{
const char *vfstype = fsinfo->fsent->fs_vfstype;
struct fscmd {
const char *vfstype;
const char *fsck;
const char *fsresize;
} fscmds[] = {
{ "ext2", "fsck -fy %d", "ext2resize %d %b"},
{"ext3", "fsck -fy %d", "ext2resize %d %b"},
{"reiserfs", "", "resize_reiserfs -s%k %d"},
{"xfs", "", "xfs_growfs -D %b %m"}, /* simple xfs grow */
{NULL, NULL, NULL},
}, *p = &fscmds[0];
for (; p->vfstype; p++) {
if (!strcmp(vfstype, p->vfstype)) {
if (!strcmp(command, "resize"))
fsinfo->cmd = p->fsresize;
else if (!strcmp(command, "check"))
fsinfo->cmd = p->fsck;
else {
log_error("Unrecognised command: %s", command);
return EINVAL;
}
break;
}
}
if (!fsinfo->cmd) {
log_error("%s: Unrecognised filesystem type", vfstype);
return EINVAL;
}
return 0;
}
/* Collapse multiple slashes */
static char *_collapse_slashes(char *path)
{
char *s = path;
/* Slight overhead but short ;) */
while ((s = strchr(s, '/')) && *(s + 1))
*(s + 1) == '/' ? memmove(s, s + 1, strlen(s)) : s++;
return path;
}
/* Free the argument array */
static void _free_argv(char **new_argv)
{
int i;
for (i = 0; new_argv[i]; i++)
free(new_argv[i]);
}
/*
* check/resize a filesystem
*/
int main(int argc, char **argv)
{
int ret = 0;
struct fsinfo fsinfo;
char *new_argv[MAX_ARGS];
char *command, *path;
if (argc < 3)
goto error;
command = argv[1];
path = _collapse_slashes(argv[2]);
if (!strcmp(command, "resize")) {
if (argc != 4)
goto error;
/* FIXME sanity checks */
fsinfo.new_size = strtoul(argv[3], NULL, 10);
} else if (argc != 3)
goto error;
/* Retrieve filesystem information (block size...) */
if ((ret = _get_fsinfo(path, &fsinfo))) {
log_error("Can't get filesystem information from %s", path);
return ret;
}
/* Get filesystem command info */
if ((ret = _get_cmd(command, &fsinfo))) {
log_error("Can't get filesystem command for %s", command);
return ret;
}
if (_new_argv(new_argv, &fsinfo))
return EINVAL;
if (!new_argv[0])
return 0;
execvp(new_argv[0], new_argv);
ret = errno;
log_error("%s: execvp %s failed", new_argv[0], strerror(errno));
_free_argv(new_argv);
return ret;
error:
_usage(argv[0]);
return EINVAL;
}
|