docs: Add virt-resize(1) link to guestfish(1) manpage.
[libguestfs.git] / fish / alloc.c
1 /* guestfish - the filesystem interactive shell
2  * Copyright (C) 2009 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <inttypes.h>
27 #include <errno.h>
28
29 #include "fish.h"
30
31 static int parse_size (const char *str, off_t *size_rtn);
32
33 int
34 do_alloc (const char *cmd, int argc, char *argv[])
35 {
36   off_t size;
37   int fd;
38
39   if (argc != 2) {
40     fprintf (stderr, _("use 'alloc file size' to create an image\n"));
41     return -1;
42   }
43
44   if (parse_size (argv[1], &size) == -1)
45     return -1;
46
47   if (!guestfs_is_config (g)) {
48     fprintf (stderr, _("can't allocate or add disks after launching\n"));
49     return -1;
50   }
51
52   fd = open (argv[0], O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666);
53   if (fd == -1) {
54     perror (argv[0]);
55     return -1;
56   }
57
58 #ifdef HAVE_POSIX_FALLOCATE
59   int err = posix_fallocate (fd, 0, size);
60   if (err != 0) {
61     errno = err;
62     perror ("fallocate");
63     close (fd);
64     unlink (argv[0]);
65     return -1;
66   }
67 #else
68   /* Slow emulation of posix_fallocate on platforms which don't have it. */
69   char buffer[BUFSIZ];
70   memset (buffer, 0, sizeof buffer);
71
72   size_t remaining = size;
73   while (remaining > 0) {
74     size_t n = remaining > sizeof buffer ? sizeof buffer : remaining;
75     ssize_t r = write (fd, buffer, n);
76     if (r == -1) {
77       perror ("write");
78       close (fd);
79       unlink (argv[0]);
80       return -1;
81     }
82     remaining -= r;
83   }
84 #endif
85
86   if (close (fd) == -1) {
87     perror (argv[0]);
88     unlink (argv[0]);
89     return -1;
90   }
91
92   if (guestfs_add_drive (g, argv[0]) == -1) {
93     unlink (argv[0]);
94     return -1;
95   }
96
97   return 0;
98 }
99
100 int
101 do_sparse (const char *cmd, int argc, char *argv[])
102 {
103   off_t size;
104   int fd;
105   char c = 0;
106
107   if (argc != 2) {
108     fprintf (stderr, _("use 'sparse file size' to create a sparse image\n"));
109     return -1;
110   }
111
112   if (parse_size (argv[1], &size) == -1)
113     return -1;
114
115   if (!guestfs_is_config (g)) {
116     fprintf (stderr, _("can't allocate or add disks after launching\n"));
117     return -1;
118   }
119
120   fd = open (argv[0], O_WRONLY|O_CREAT|O_NOCTTY|O_TRUNC, 0666);
121   if (fd == -1) {
122     perror (argv[0]);
123     return -1;
124   }
125
126   if (lseek (fd, size-1, SEEK_SET) == (off_t) -1) {
127     perror ("lseek");
128     close (fd);
129     unlink (argv[0]);
130     return -1;
131   }
132
133   if (write (fd, &c, 1) != 1) {
134     perror ("write");
135     close (fd);
136     unlink (argv[0]);
137     return -1;
138   }
139
140   if (close (fd) == -1) {
141     perror (argv[0]);
142     unlink (argv[0]);
143     return -1;
144   }
145
146   if (guestfs_add_drive (g, argv[0]) == -1) {
147     unlink (argv[0]);
148     return -1;
149   }
150
151   return 0;
152 }
153
154 static int
155 parse_size (const char *str, off_t *size_rtn)
156 {
157   uint64_t size;
158   char type;
159
160   /* Note that the parsing here is looser than what is specified in the
161    * help, but we may tighten it up in future so beware.
162    */
163   if (sscanf (str, "%"SCNu64"%c", &size, &type) == 2) {
164     switch (type) {
165     case 'k': case 'K': size *= 1024ULL; break;
166     case 'm': case 'M': size *= 1024ULL * 1024ULL; break;
167     case 'g': case 'G': size *= 1024ULL * 1024ULL * 1024ULL; break;
168     case 't': case 'T': size *= 1024ULL * 1024ULL * 1024ULL * 1024ULL; break;
169     case 'p': case 'P': size *= 1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL; break;
170     case 'e': case 'E': size *= 1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL; break;
171     case 's': size *= 512; break;
172     default:
173       fprintf (stderr, _("could not parse size specification '%s'\n"), str);
174       return -1;
175     }
176   }
177   else if (sscanf (str, "%"SCNu64, &size) == 1)
178     size *= 1024ULL;
179   else {
180     fprintf (stderr, _("could not parse size specification '%s'\n"), str);
181     return -1;
182   }
183
184   /* XXX 32 bit file offsets, if anyone uses them?  GCC should give
185    * a warning here anyhow.
186    */
187   *size_rtn = size;
188
189   return 0;
190 }