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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
|
/* $OpenBSD: vmctl.c,v 1.48 2018/03/14 07:29:34 mlarkin Exp $ */
/*
* Copyright (c) 2014 Mike Larkin <mlarkin@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/queue.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <machine/vmmvar.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <imsg.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <util.h>
#include <pwd.h>
#include <grp.h>
#include "vmd.h"
#include "vmctl.h"
#include "atomicio.h"
extern char *__progname;
uint32_t info_id;
char info_name[VMM_MAX_NAME_LEN];
int info_console;
/*
* vm_start
*
* Request vmd to start the VM defined by the supplied parameters
*
* Parameters:
* start_id: optional ID of the VM
* name: optional name of the VM
* memsize: memory size (MB) of the VM to create
* nnics: number of vionet network interfaces to create
* nics: switch names of the network interfaces to create
* ndisks: number of disk images
* disks: disk image file names
* kernel: kernel image to load
* iso: iso image file
*
* Return:
* 0 if the request to start the VM was sent successfully.
* ENOMEM if a memory allocation failure occurred.
*/
int
vm_start(uint32_t start_id, const char *name, int memsize, int nnics,
char **nics, int ndisks, char **disks, char *kernel, char *iso)
{
struct vmop_create_params *vmc;
struct vm_create_params *vcp;
unsigned int flags = 0;
int i;
const char *s;
if (memsize)
flags |= VMOP_CREATE_MEMORY;
if (nnics)
flags |= VMOP_CREATE_NETWORK;
if (ndisks)
flags |= VMOP_CREATE_DISK;
if (kernel)
flags |= VMOP_CREATE_KERNEL;
if (iso)
flags |= VMOP_CREATE_CDROM;
if (flags != 0) {
if (memsize < 1)
memsize = VM_DEFAULT_MEMORY;
if (ndisks > VMM_MAX_DISKS_PER_VM)
errx(1, "too many disks");
else if (ndisks == 0)
warnx("starting without disks");
if (kernel == NULL && ndisks == 0)
errx(1, "no kernel or disk specified");
if (nnics == -1)
nnics = 0;
if (nnics > VMM_MAX_NICS_PER_VM)
errx(1, "too many network interfaces");
if (nnics == 0)
warnx("starting without network interfaces");
}
vmc = calloc(1, sizeof(struct vmop_create_params));
if (vmc == NULL)
return (ENOMEM);
vmc->vmc_flags = flags;
/* vcp includes configuration that is shared with the kernel */
vcp = &vmc->vmc_params;
/*
* XXX: vmd(8) fills in the actual memory ranges. vmctl(8)
* just passes in the actual memory size in MB here.
*/
vcp->vcp_nmemranges = 1;
vcp->vcp_memranges[0].vmr_size = memsize;
vcp->vcp_ncpus = 1;
vcp->vcp_ndisks = ndisks;
vcp->vcp_nnics = nnics;
vcp->vcp_id = start_id;
for (i = 0 ; i < ndisks; i++)
strlcpy(vcp->vcp_disks[i], disks[i], VMM_MAX_PATH_DISK);
for (i = 0 ; i < nnics; i++) {
vmc->vmc_ifflags[i] = VMIFF_UP;
if (strcmp(".", nics[i]) == 0) {
/* Add a "local" interface */
strlcpy(vmc->vmc_ifswitch[i], "", IF_NAMESIZE);
vmc->vmc_ifflags[i] |= VMIFF_LOCAL;
} else {
/* Add an interface to a switch */
strlcpy(vmc->vmc_ifswitch[i], nics[i], IF_NAMESIZE);
}
}
if (name != NULL) {
/* Allow VMs names with alphanumeric characters, dot, hyphen
* and underscore. But disallow dot, hyphen and underscore at
* the start.
*/
if (*name == '-' || *name == '.' || *name == '_')
errx(1, "Invalid VM name");
for (s = name; *s != '\0'; ++s) {
if (!(isalnum(*s) || *s == '.' || *s == '-' ||
*s == '_'))
errx(1, "Invalid VM name");
}
strlcpy(vcp->vcp_name, name, VMM_MAX_NAME_LEN);
}
if (kernel != NULL)
strlcpy(vcp->vcp_kernel, kernel, VMM_MAX_KERNEL_PATH);
if (iso != NULL)
strlcpy(vcp->vcp_cdrom, iso, VMM_MAX_PATH_CDROM);
imsg_compose(ibuf, IMSG_VMDOP_START_VM_REQUEST, 0, 0, -1,
vmc, sizeof(struct vmop_create_params));
free(vcp);
return (0);
}
/*
* vm_start_complete
*
* Callback function invoked when we are expecting an
* IMSG_VMDOP_START_VMM_RESPONSE message indicating the completion of
* a start vm operation.
*
* Parameters:
* imsg : response imsg received from vmd
* ret : return value
* autoconnect : open the console after startup
*
* Return:
* Always 1 to indicate we have processed the return message (even if it
* was an incorrect/failure message)
*
* The function also sets 'ret' to the error code as follows:
* 0 : Message successfully processed
* EINVAL: Invalid or unexpected response from vmd
* EIO : vm_start command failed
* ENOENT: a specified component of the VM could not be found (disk image,
* BIOS firmware image, etc)
*/
int
vm_start_complete(struct imsg *imsg, int *ret, int autoconnect)
{
struct vmop_result *vmr;
int res;
if (imsg->hdr.type == IMSG_VMDOP_START_VM_RESPONSE) {
vmr = (struct vmop_result *)imsg->data;
res = vmr->vmr_result;
if (res) {
switch (res) {
case VMD_BIOS_MISSING:
warnx("vmm bios firmware file not found.");
*ret = ENOENT;
break;
case VMD_DISK_MISSING:
warnx("could not open specified disk image(s)");
*ret = ENOENT;
break;
case VMD_DISK_INVALID:
warnx("specified disk image(s) are "
"not regular files");
*ret = ENOENT;
break;
case VMD_CDROM_MISSING:
warnx("could not find specified iso image");
*ret = ENOENT;
break;
case VMD_CDROM_INVALID:
warnx("specified iso image is not a regular "
"file");
*ret = ENOENT;
break;
default:
errno = res;
warn("start vm command failed");
*ret = EIO;
}
} else if (autoconnect) {
/* does not return */
ctl_openconsole(vmr->vmr_ttyname);
} else {
warnx("started vm %d successfully, tty %s",
vmr->vmr_id, vmr->vmr_ttyname);
*ret = 0;
}
} else {
warnx("unexpected response received from vmd");
*ret = EINVAL;
}
return (1);
}
void
send_vm(uint32_t id, const char *name)
{
struct vmop_id vid;
int fds[2], readn, writen;
long pagesz;
char *buf;
pagesz = getpagesize();
buf = malloc(pagesz);
if (buf == NULL)
errx(1, "%s: memory allocation failure", __func__);
memset(&vid, 0, sizeof(vid));
vid.vid_id = id;
if (name != NULL)
strlcpy(vid.vid_name, name, sizeof(vid.vid_name));
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fds) == -1) {
warnx("%s: socketpair creation failed", __func__);
} else {
imsg_compose(ibuf, IMSG_VMDOP_SEND_VM_REQUEST, 0, 0, fds[0],
&vid, sizeof(vid));
imsg_flush(ibuf);
while (1) {
readn = atomicio(read, fds[1], buf, pagesz);
if (!readn)
break;
writen = atomicio(vwrite, STDOUT_FILENO, buf,
readn);
if (writen != readn)
break;
}
if (vid.vid_id)
warnx("sent vm %d successfully", vid.vid_id);
else
warnx("sent vm %s successfully", vid.vid_name);
}
free(buf);
}
void
vm_receive(uint32_t id, const char *name)
{
struct vmop_id vid;
int fds[2], readn, writen;
long pagesz;
char *buf;
pagesz = getpagesize();
buf = malloc(pagesz);
if (buf == NULL)
errx(1, "%s: memory allocation failure", __func__);
memset(&vid, 0, sizeof(vid));
if (name != NULL)
strlcpy(vid.vid_name, name, sizeof(vid.vid_name));
if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, fds) == -1) {
warnx("%s: socketpair creation failed", __func__);
} else {
imsg_compose(ibuf, IMSG_VMDOP_RECEIVE_VM_REQUEST, 0, 0, fds[0],
&vid, sizeof(vid));
imsg_flush(ibuf);
while (1) {
readn = atomicio(read, STDIN_FILENO, buf, pagesz);
if (!readn) {
close(fds[1]);
break;
}
writen = atomicio(vwrite, fds[1], buf, readn);
if (writen != readn)
break;
}
}
free(buf);
}
void
pause_vm(uint32_t pause_id, const char *name)
{
struct vmop_id vid;
memset(&vid, 0, sizeof(vid));
vid.vid_id = pause_id;
if (name != NULL)
(void)strlcpy(vid.vid_name, name, sizeof(vid.vid_name));
imsg_compose(ibuf, IMSG_VMDOP_PAUSE_VM, 0, 0, -1,
&vid, sizeof(vid));
}
int
pause_vm_complete(struct imsg *imsg, int *ret)
{
struct vmop_result *vmr;
int res;
if (imsg->hdr.type == IMSG_VMDOP_PAUSE_VM_RESPONSE) {
vmr = (struct vmop_result *)imsg->data;
res = vmr->vmr_result;
if (res) {
errno = res;
warn("pause vm command failed");
*ret = EIO;
} else {
warnx("paused vm %d successfully", vmr->vmr_id);
*ret = 0;
}
} else {
warnx("unexpected response received from vmd");
*ret = EINVAL;
}
return (1);
}
void
unpause_vm(uint32_t pause_id, const char *name)
{
struct vmop_id vid;
memset(&vid, 0, sizeof(vid));
vid.vid_id = pause_id;
if (name != NULL)
(void)strlcpy(vid.vid_name, name, sizeof(vid.vid_name));
imsg_compose(ibuf, IMSG_VMDOP_UNPAUSE_VM, 0, 0, -1,
&vid, sizeof(vid));
}
int
unpause_vm_complete(struct imsg *imsg, int *ret)
{
struct vmop_result *vmr;
int res;
if (imsg->hdr.type == IMSG_VMDOP_UNPAUSE_VM_RESPONSE) {
vmr = (struct vmop_result *)imsg->data;
res = vmr->vmr_result;
if (res) {
errno = res;
warn("unpause vm command failed");
*ret = EIO;
} else {
warnx("unpaused vm %d successfully", vmr->vmr_id);
*ret = 0;
}
} else {
warnx("unexpected response received from vmd");
*ret = EINVAL;
}
return (1);
}
/*
* terminate_vm
*
* Request vmd to stop the VM indicated
*
* Parameters:
* terminate_id: ID of the vm to be terminated
* name: optional name of the VM to be terminated
*/
void
terminate_vm(uint32_t terminate_id, const char *name)
{
struct vmop_id vid;
memset(&vid, 0, sizeof(vid));
vid.vid_id = terminate_id;
if (name != NULL)
(void)strlcpy(vid.vid_name, name, sizeof(vid.vid_name));
imsg_compose(ibuf, IMSG_VMDOP_TERMINATE_VM_REQUEST, 0, 0, -1,
&vid, sizeof(vid));
}
/*
* terminate_vm_complete
*
* Callback function invoked when we are expecting an
* IMSG_VMDOP_TERMINATE_VMM_RESPONSE message indicating the completion of
* a terminate vm operation.
*
* Parameters:
* imsg : response imsg received from vmd
* ret : return value
*
* Return:
* Always 1 to indicate we have processed the return message (even if it
* was an incorrect/failure message)
*
* The function also sets 'ret' to the error code as follows:
* 0 : Message successfully processed
* EINVAL: Invalid or unexpected response from vmd
* EIO : terminate_vm command failed
*/
int
terminate_vm_complete(struct imsg *imsg, int *ret)
{
struct vmop_result *vmr;
int res;
if (imsg->hdr.type == IMSG_VMDOP_TERMINATE_VM_RESPONSE) {
vmr = (struct vmop_result *)imsg->data;
res = vmr->vmr_result;
if (res) {
switch (res) {
case VMD_VM_STOP_INVALID:
warnx("cannot stop vm that is not running");
*ret = EINVAL;
break;
case ENOENT:
warnx("vm not found");
*ret = EIO;
break;
default:
warn("terminate vm command failed");
*ret = EIO;
}
} else {
warnx("sent request to terminate vm %d", vmr->vmr_id);
*ret = 0;
}
} else {
warnx("unexpected response received from vmd");
*ret = EINVAL;
}
errno = *ret;
return (1);
}
/*
* get_info_vm
*
* Return the list of all running VMs or find a specific VM by ID or name.
*
* Parameters:
* id: optional ID of a VM to list
* name: optional name of a VM to list
* console: if true, open the console of the selected VM (by name or ID)
*
* Request a list of running VMs from vmd
*/
void
get_info_vm(uint32_t id, const char *name, int console)
{
info_id = id;
if (name != NULL)
(void)strlcpy(info_name, name, sizeof(info_name));
info_console = console;
imsg_compose(ibuf, IMSG_VMDOP_GET_INFO_VM_REQUEST, 0, 0, -1, NULL, 0);
}
/*
* check_info_id
*
* Check if requested name or ID of a VM matches specified arguments
*
* Parameters:
* name: name of the VM
* id: ID of the VM
*/
int
check_info_id(const char *name, uint32_t id)
{
if (info_id == 0 && *info_name == '\0')
return (-1);
if (info_id != 0 && info_id == id)
return (1);
if (*info_name != '\0' && name && strcmp(info_name, name) == 0)
return (1);
return (0);
}
/*
* add_info
*
* Callback function invoked when we are expecting an
* IMSG_VMDOP_GET_INFO_VM_DATA message indicating the receipt of additional
* "list vm" data, or an IMSG_VMDOP_GET_INFO_VM_END_DATA message indicating
* that no additional "list vm" data will be forthcoming.
*
* Parameters:
* imsg : response imsg received from vmd
* ret : return value
*
* Return:
* 0 : the returned data was successfully added to the "list vm" data.
* The caller can expect more data.
* 1 : IMSG_VMDOP_GET_INFO_VM_END_DATA received (caller should not call
* add_info again), or an error occurred adding the returned data
* to the "list vm" data. The caller should check the value of
* 'ret' to determine which case occurred.
*
* This function does not return if a VM is found and info_console is set.
*
* The function also sets 'ret' to the error code as follows:
* 0 : Message successfully processed
* EINVAL: Invalid or unexpected response from vmd
* ENOMEM: memory allocation failure
*/
int
add_info(struct imsg *imsg, int *ret)
{
static size_t ct = 0;
static struct vmop_info_result *vir = NULL;
if (imsg->hdr.type == IMSG_VMDOP_GET_INFO_VM_DATA) {
vir = reallocarray(vir, ct + 1,
sizeof(struct vmop_info_result));
if (vir == NULL) {
*ret = ENOMEM;
return (1);
}
memcpy(&vir[ct], imsg->data, sizeof(struct vmop_info_result));
ct++;
*ret = 0;
return (0);
} else if (imsg->hdr.type == IMSG_VMDOP_GET_INFO_VM_END_DATA) {
if (info_console)
vm_console(vir, ct);
else
print_vm_info(vir, ct);
free(vir);
*ret = 0;
return (1);
} else {
*ret = EINVAL;
return (1);
}
}
/*
* print_vm_info
*
* Prints the vm information returned from vmd in 'list' to stdout.
*
* Parameters
* list: the vm information (consolidated) returned from vmd via imsg
* ct : the size (number of elements in 'list') of the result
*/
void
print_vm_info(struct vmop_info_result *list, size_t ct)
{
struct vm_info_result *vir;
struct vmop_info_result *vmi;
size_t i, j;
char *vcpu_state, *tty;
char curmem[FMT_SCALED_STRSIZE];
char maxmem[FMT_SCALED_STRSIZE];
char user[16], group[16];
struct passwd *pw;
struct group *gr;
printf("%5s %5s %5s %7s %7s %7s %12s %s\n", "ID", "PID", "VCPUS",
"MAXMEM", "CURMEM", "TTY", "OWNER", "NAME");
for (i = 0; i < ct; i++) {
vmi = &list[i];
vir = &vmi->vir_info;
if (check_info_id(vir->vir_name, vir->vir_id)) {
/* get user name */
if ((pw = getpwuid(vmi->vir_uid)) == NULL)
(void)snprintf(user, sizeof(user),
"%d", vmi->vir_uid);
else
(void)strlcpy(user, pw->pw_name,
sizeof(user));
/* get group name */
if (vmi->vir_gid != -1) {
if (vmi->vir_uid == 0)
*user = '\0';
if ((gr = getgrgid(vmi->vir_gid)) == NULL)
(void)snprintf(group, sizeof(group),
":%lld", vmi->vir_gid);
else
(void)snprintf(group, sizeof(group),
":%s", gr->gr_name);
(void)strlcat(user, group, sizeof(user));
}
(void)strlcpy(curmem, "-", sizeof(curmem));
(void)strlcpy(maxmem, "-", sizeof(maxmem));
(void)fmt_scaled(vir->vir_memory_size * 1024 * 1024,
maxmem);
if (vir->vir_creator_pid != 0 && vir->vir_id != 0) {
if (*vmi->vir_ttyname == '\0')
tty = "-";
/* get tty - skip /dev/ path */
else if ((tty = strrchr(vmi->vir_ttyname,
'/')) == NULL || ++tty == '\0')
tty = list[i].vir_ttyname;
(void)fmt_scaled(vir->vir_used_size, curmem);
/* running vm */
printf("%5u %5u %5zd %7s %7s %7s %12s %s\n",
vir->vir_id, vir->vir_creator_pid,
vir->vir_ncpus, maxmem, curmem,
tty, user, vir->vir_name);
} else {
/* disabled vm */
printf("%5u %5s %5zd %7s %7s %7s %12s %s\n",
vir->vir_id, "-",
vir->vir_ncpus, maxmem, curmem,
"-", user, vir->vir_name);
}
}
if (check_info_id(vir->vir_name, vir->vir_id) > 0) {
for (j = 0; j < vir->vir_ncpus; j++) {
if (vir->vir_vcpu_state[j] ==
VCPU_STATE_STOPPED)
vcpu_state = "STOPPED";
else if (vir->vir_vcpu_state[j] ==
VCPU_STATE_RUNNING)
vcpu_state = "RUNNING";
else
vcpu_state = "UNKNOWN";
printf(" VCPU: %2zd STATE: %s\n",
j, vcpu_state);
}
}
}
}
/*
* vm_console
*
* Connects to the vm console returned from vmd in 'list'.
*
* Parameters
* list: the vm information (consolidated) returned from vmd via imsg
* ct : the size (number of elements in 'list') of the result
*/
__dead void
vm_console(struct vmop_info_result *list, size_t ct)
{
struct vmop_info_result *vir;
size_t i;
for (i = 0; i < ct; i++) {
vir = &list[i];
if ((check_info_id(vir->vir_info.vir_name,
vir->vir_info.vir_id) > 0) &&
(vir->vir_ttyname[0] != '\0')) {
/* does not return */
ctl_openconsole(vir->vir_ttyname);
}
}
errx(1, "console not found");
}
/*
* create_imagefile
*
* Create an empty imagefile with the specified path and size.
*
* Parameters:
* imgfile_path: path to the image file to create
* imgsize : size of the image file to create (in MB)
*
* Return:
* EEXIST: The requested image file already exists
* 0 : Image file successfully created
* Exxxx : Various other Exxxx errno codes due to other I/O errors
*/
int
create_imagefile(const char *imgfile_path, long imgsize)
{
int fd, ret;
/* Refuse to overwrite an existing image */
fd = open(imgfile_path, O_RDWR | O_CREAT | O_TRUNC | O_EXCL,
S_IRUSR | S_IWUSR);
if (fd == -1)
return (errno);
/* Extend to desired size */
if (ftruncate(fd, (off_t)imgsize * 1024 * 1024) == -1) {
ret = errno;
close(fd);
unlink(imgfile_path);
return (ret);
}
ret = close(fd);
return (ret);
}
|