Update FSF address.
[libguestfs.git] / fish / prep_boot.c
1 /* guestfish - the filesystem interactive shell
2  * Copyright (C) 2010 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #include "fish.h"
28 #include "prepopts.h"
29
30 void
31 prep_prelaunch_bootroot (const char *filename, prep_data *data)
32 {
33   if (alloc_disk (filename, data->params[2], 0, 1) == -1)
34     prep_error (data, filename, _("failed to allocate disk"));
35 }
36
37 void
38 prep_postlaunch_bootroot (const char *filename, prep_data *data, const char *device)
39 {
40   off_t bootsize;
41   if (parse_size (data->params[3], &bootsize) == -1)
42     prep_error (data, filename, _("could not parse boot size"));
43
44   int sector = guestfs_blockdev_getss (g, device);
45   if (sector == -1)
46     prep_error (data, filename, _("failed to get sector size of disk: %s"),
47                 guestfs_last_error (g));
48
49   if (guestfs_part_init (g, device, data->params[4]) == -1)
50     prep_error (data, filename, _("failed to partition disk: %s"),
51                 guestfs_last_error (g));
52
53   off_t lastbootsect = 64 + bootsize/sector - 1;
54   if (guestfs_part_add (g, device, "primary", 64, lastbootsect) == -1)
55     prep_error (data, filename, _("failed to add boot partition: %s"),
56                 guestfs_last_error (g));
57
58   if (guestfs_part_add (g, device, "primary", lastbootsect+1, -64) == -1)
59     prep_error (data, filename, _("failed to add root partition: %s"),
60                 guestfs_last_error (g));
61
62   char *part;
63   if (asprintf (&part, "%s1", device) == -1) {
64     perror ("asprintf");
65     exit (EXIT_FAILURE);
66   }
67   if (guestfs_mkfs (g, data->params[0], part) == -1)
68     prep_error (data, filename, _("failed to create boot filesystem: %s"),
69                 guestfs_last_error (g));
70   free (part);
71
72   if (asprintf (&part, "%s2", device) == -1) {
73     perror ("asprintf");
74     exit (EXIT_FAILURE);
75   }
76   if (guestfs_mkfs (g, data->params[1], part) == -1)
77     prep_error (data, filename, _("failed to create root filesystem: %s"),
78                 guestfs_last_error (g));
79   free (part);
80 }
81
82 void
83 prep_prelaunch_bootrootlv (const char *filename, prep_data *data)
84 {
85   if (vg_lv_parse (data->params[0], NULL, NULL) == -1)
86     prep_error (data, filename, _("incorrect format for LV name, use '/dev/VG/LV'"));
87
88   if (alloc_disk (filename, data->params[3], 0, 1) == -1)
89     prep_error (data, filename, _("failed to allocate disk"));
90 }
91
92 void
93 prep_postlaunch_bootrootlv (const char *filename, prep_data *data, const char *device)
94 {
95   off_t bootsize;
96   if (parse_size (data->params[4], &bootsize) == -1)
97     prep_error (data, filename, _("could not parse boot size"));
98
99   int sector = guestfs_blockdev_getss (g, device);
100   if (sector == -1)
101     prep_error (data, filename, _("failed to get sector size of disk: %s"),
102                 guestfs_last_error (g));
103
104   if (guestfs_part_init (g, device, data->params[5]) == -1)
105     prep_error (data, filename, _("failed to partition disk: %s"),
106                 guestfs_last_error (g));
107
108   off_t lastbootsect = 64 + bootsize/sector - 1;
109   if (guestfs_part_add (g, device, "primary", 64, lastbootsect) == -1)
110     prep_error (data, filename, _("failed to add boot partition: %s"),
111                 guestfs_last_error (g));
112
113   if (guestfs_part_add (g, device, "primary", lastbootsect+1, -64) == -1)
114     prep_error (data, filename, _("failed to add root partition: %s"),
115                 guestfs_last_error (g));
116
117   char *vg;
118   char *lv;
119   if (vg_lv_parse (data->params[0], &vg, &lv) == -1)
120     prep_error (data, filename, _("incorrect format for LV name, use '/dev/VG/LV'"));
121
122   char *part;
123   if (asprintf (&part, "%s1", device) == -1) {
124     perror ("asprintf");
125     exit (EXIT_FAILURE);
126   }
127   if (guestfs_mkfs (g, data->params[1], part) == -1)
128     prep_error (data, filename, _("failed to create boot filesystem: %s"),
129                 guestfs_last_error (g));
130   free (part);
131
132   if (asprintf (&part, "%s2", device) == -1) {
133     perror ("asprintf");
134     exit (EXIT_FAILURE);
135   }
136   if (guestfs_pvcreate (g, part) == -1)
137     prep_error (data, filename, _("failed to create PV: %s: %s"),
138                 part, guestfs_last_error (g));
139
140   char *parts[] = { part, NULL };
141   if (guestfs_vgcreate (g, vg, parts) == -1)
142     prep_error (data, filename, _("failed to create VG: %s: %s"),
143                 vg, guestfs_last_error (g));
144
145   /* Create the smallest possible LV, then resize it to fill
146    * all available space.
147    */
148   if (guestfs_lvcreate (g, lv, vg, 1) == -1)
149     prep_error (data, filename, _("failed to create LV: /dev/%s/%s: %s"),
150                 vg, lv, guestfs_last_error (g));
151   if (guestfs_lvresize_free (g, data->params[0], 100) == -1)
152     prep_error (data, filename,
153                 _("failed to resize LV to full size: %s: %s"),
154                 data->params[0], guestfs_last_error (g));
155
156   if (guestfs_mkfs (g, data->params[2], data->params[0]) == -1)
157     prep_error (data, filename, _("failed to create root filesystem: %s"),
158                 guestfs_last_error (g));
159
160   free (part);
161   free (vg);
162   free (lv);
163 }