My favorites | Sign in
Project Logo
                
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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "ddsetup.h"

// to do:
// - spinlocks in base ddlink ops
// - uuids and major/minor command forms
// - more dmsetup commands

struct token { char *text; int size; };

int get_number(struct token *token, long long *result)
{
char *next;
long long number = strtoll(token->text, &next, 10);
if (next - token->text == token->size) {
*result = number;
return 1;
}
return 0;
}

int table_line(int dd, struct token *token, int tokens)
{
long long offset, sectors;
struct ddtarget target;
int i;

if (tokens < 3 ||
!get_number(&token[0], &offset) ||
!get_number(&token[1], &sectors))
return -1;

if (dd < 0)
return 0; /* only checking syntax */

for (i = 2; i < tokens; i++)
if (write(dd, token[i].text, token[i].size) == -1)
return -1;

target.offset = offset;
target.sectors = sectors;
if ((ioctl(dd, DMTARGET, &target)) == -1)
return -1;
return 0;
}

void set_dmname(char *buffer, unsigned size, char *name)
{
snprintf(buffer, size , "%s%s", "/dev/mapper/", name);
}

int do_create(int dd, char *name, struct token *tokenvec, unsigned *linevec, int lines)
{
int line, start, devnum;
char filename[100];
struct ddresult got;
int mode = DMREAD|DMWRITE;

if (ioctl(dd, DMTABLE, &(struct ddtable){ .targets = lines, .mode = mode }) == -1)
return -1;

for (start = 0, line = 0; line < lines; start = linevec[line++])
if (table_line(dd, tokenvec + start, linevec[line] - start) == -1)
return -1;

if (write(dd, name, strlen(name)) == -1)
return -2;
if (ioctl(dd, DMCREATE) == -1)
return -2;
if (read(dd, &got, sizeof(got)) == -1)
return -2;

devnum = makedev(got.dev.major, got.dev.minor);
set_dmname(filename, sizeof(filename), name);
return mknod(filename, S_IFBLK, devnum);
}

int do_remove(int dd, char *name)
{
char filename[100];
if (write(dd, name, strlen(name)) == -1)
return -2;
if ((ioctl(dd, DMREMOVE)) == -1)
return -2;
set_dmname(filename, sizeof(filename), name);
if (unlink(filename) == -1)
return -2;
return 0;
}

char *plural(int n)
{
return n > 1 ? "s:" : n ? ":" : "";
}

char *flagged(int f)
{
return f? "y" : "n";
}

int main(int argc, char *argv[])
{
char *action = argv[1];
int dm, dd, len, num;

if (argc == 1)
goto usage;
if ((dm = open("/dev/mapper/control", O_RDWR)) == -1)
goto whoops;
if ((dd = ioctl(dm, DDLINK)) == -1)
goto whoops;
if (!strcmp(action, "create")) {
if (argc < 3)
goto usage;

struct stat stat;
if ((fstat(0, &stat)) == -1)
return -1;

if (S_ISCHR(stat.st_mode)) {
printf("No table given\n");
exit(1);
}

char text[2 << 12];
int maxtokens = 1000, maxlines = 100;
int tokens = 0, lines = 0;
struct token tokenvec[maxtokens];
unsigned linevec[maxlines]; // handle overflow!!!
unsigned len = read(0, text, sizeof(text));
char *next = text, *end = text + len;

while(tokens < maxtokens) {
while (next < end && !isgraph(*next) && *next != '\n')
next++;
if (next == end || *next == '\n') {
linevec[lines++] = tokens;
if (next == end || ++next == end)
break;
continue;
}
char *last = tokenvec[tokens].text = next ;
while (next < end && isgraph(*next))
next++;
tokenvec[tokens].size = next - last;
tokens++;
}
int line, start;
for (start = 0, line = 0; line < lines; start = linevec[line++])
if (table_line(-1, tokenvec + start, linevec[line] - start) < 0) {
printf("input table syntax error, line %i\n", line + 1);
exit(1);
}
if (do_create(dd, argv[2], tokenvec, linevec, lines))
goto whoops;
return 0;
}
if (!strcmp(action, "remove")) {
if (argc < 3)
goto usage;
switch (do_remove(dd, argv[2])) {
case 0: break;
case -2: goto report;
default: goto whoops;
}
return 0;
}
if (!strcmp(action, "suspend")) {
if (argc < 3)
goto usage;
if (write(dd, argv[2], strlen(argv[2])) == -1)
goto report;
if ((ioctl(dd, DMSUSPEND)) == -1)
goto report;
return 0;
}
if (!strcmp(action, "resume")) {
if (argc < 3)
goto usage;
if (write(dd, argv[2], strlen(argv[2])) == -1)
goto report;
if ((ioctl(dd, DMRESUME)) == -1)
goto report;
return 0;
}
if (!strcmp(action, "rename")) {
if (argc != 4)
goto usage;
if (write(dd, argv[2], strlen(argv[2])) == -1)
goto report;
if (write(dd, argv[3], strlen(argv[3])) == -1)
goto report;
if ((ioctl(dd, DMRENAME)) == -1)
goto report;
return 0;
}
if (!strcmp(action, "ls")) {
struct { struct ddname name; char space[100]; } got;
if (argc != 2)
goto usage;
if ((ioctl(dd, DMNAMES)) == -1)
goto report;
while ((len = read(dd, &got, sizeof(got)))) {
if (len == -1)
goto whoops;
printf("%.*s (%i.%i)\n",
len - sizeof got.name,
got.name.name,
got.name.dev.major,
got.name.dev.minor);
}
return 0;
}
if (!strcmp(action, "deps")) {
struct devnum got;
if (argc != 3)
goto usage;
if (write(dd, argv[2], strlen(argv[2])) == -1)
goto report;
if ((num = ioctl(dd, DMDEPS)) == -1)
goto report;
printf("%s has %i target%s", argv[2], num, plural(num));
while ((len = read(dd, &got, sizeof(got)))) {
if (len == -1)
goto report;
printf(" (%u.%u)", got.major, got.minor);
}
printf("\n");
return 0;
}
if (!strcmp(action, "info")) {
struct dmstatus got;
if (argc != 3)
goto usage;
if (write(dd, argv[2], strlen(argv[2])) == -1)
goto report;
if ((ioctl(dd, DMSTATUS)) == -1)
goto report;
if ((len = read(dd, &got, sizeof(got))) == -1)
goto report;
if (len != sizeof(got) && (errno = ENODATA))
goto whoops;
printf("%s: (%u.%u) ", argv[2], got.dev.major, got.dev.minor);
printf("targets %i, ", got.targets);
printf("opens %i, ", got.opens);
printf("event %i, ", got.event);
printf("present=%s, ", flagged(got.flags & DMFLAG_PRESENT));
printf("readonly=%s, ", flagged(got.flags & DMFLAG_READONLY));
printf("suspended=%s\n", flagged(got.flags & DMFLAG_SUSPEND));
while ((len = read(dd, &got, sizeof(got)))) {
if (len == -1)
goto report;
printf(" (%u.%u)", got.dev.major, got.dev.minor);
}
return 0;
}
if (!strcmp(action, "version")) {
struct ddversion got;
if (argc != 2)
goto usage;
if ((ioctl(dd, DMVERSION)) == -1)
goto report;
if ((len = read(dd, &got, sizeof(got))) == -1)
goto report;
if (len != sizeof(got) && (errno = ENODATA))
goto whoops;
printf("ddsetup version %i.%i.%i\n", got.major, got.minor, got.point);
return 0;
}
if (!strcmp(action, "targets")) {
struct { struct ddtype type; char space[100]; } got;
if (argc != 2)
goto usage;
if ((ioctl(dd, DMTYPES)) == -1)
goto report;
while ((len = read(dd, &got, sizeof(got)))) {
if (len == -1)
goto report;
printf("%-18.*s v%i.%i.%i\n",
len - sizeof(struct ddtype), got.type.name,
got.type.version.major,
got.type.version.minor,
got.type.version.point);
}
return 0;
}
usage:
printf("usage: %s <command> [<device>] [<options>]\n", argv[0]);
exit(1);
report:;
char text[100];
if ((len = read(dd, text, sizeof(text))) == -1 || !len)
goto whoops;
printf("%s: %s (%.*s)\n", argv[0], strerror(errno), len, text);
exit(1);
whoops:
printf("%s: %s (%i)\n", argv[0], strerror(errno), errno);
return 1;
exit(1);
}
Show details Hide details

Change log

r1427 by Daniel.Raymond.Phillips on Mar 04, 2008   Diff
Really add the ddsetup files
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 7430 bytes, 310 lines
Hosted by Google Code