]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/gpt/migrate.c
This commit was generated by cvs2svn to compensate for changes in r102782,
[FreeBSD/FreeBSD.git] / sbin / gpt / migrate.c
1 /*
2  * Copyright (c) 2002 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <sys/types.h>
30 #include <sys/disklabel.h>
31 #include <sys/uuid.h>
32 #include <sys/gpt.h>
33
34 #include <err.h>
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #include "map.h"
42 #include "gpt.h"
43
44 int keep, slice;
45
46 static void
47 usage_migrate(void)
48 {
49
50         fprintf(stderr,
51             "usage: %s [-ks] device\n", getprogname());
52         exit(1);
53 }
54
55 static struct gpt_ent*
56 migrate_disklabel(int fd, off_t start, struct gpt_ent *ent)
57 {
58         char *buf;
59         struct disklabel *dl;
60         int i;
61
62         buf = gpt_read(fd, start + LABELSECTOR, 1);
63         dl = (void*)(buf + LABELOFFSET);
64
65         if (dl->d_magic != DISKMAGIC || dl->d_magic2 != DISKMAGIC) {
66                 warnx("%s: warning: FreeBSD slice without disklabel",
67                     device_name);
68                 return (ent);
69         }
70
71         for (i = 0; i < dl->d_npartitions; i++) {
72                 switch (dl->d_partitions[i].p_fstype) {
73                 case FS_SWAP: {
74                         uuid_t swap = GPT_ENT_TYPE_FREEBSD_SWAP;
75                         ent->ent_type = swap;
76                         unicode16(ent->ent_name,
77                             L"FreeBSD swap partition", 36);
78                         break;
79                 }
80                 case FS_BSDFFS: {
81                         uuid_t ufs = GPT_ENT_TYPE_FREEBSD_UFS;
82                         ent->ent_type = ufs;
83                         unicode16(ent->ent_name,
84                             L"FreeBSD UFS partition", 36);
85                         break;
86                 }
87                 case FS_VINUM: {
88                         uuid_t vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
89                         ent->ent_type = vinum;
90                         unicode16(ent->ent_name,
91                             L"FreeBSD vinum partition", 36);
92                         break;
93                 }
94                 default:
95                         continue;
96                 }
97
98                 ent->ent_lba_start = dl->d_partitions[i].p_offset;
99                 ent->ent_lba_end = ent->ent_lba_start +
100                     dl->d_partitions[i].p_size - 1LL;
101                 ent++;
102         }
103
104         return (ent);
105 }
106
107 static void
108 migrate(int fd)
109 {
110         off_t blocks, last;
111         map_t *gpt, *tpg;
112         map_t *tbl, *lbt;
113         map_t *map;
114         struct gpt_hdr *hdr;
115         struct gpt_ent *ent;
116         struct mbr *mbr;
117         uint32_t start, size;
118         unsigned int i;
119
120         last = mediasz / secsz - 1LL;
121
122         map = map_find(MAP_TYPE_MBR);
123         if (map == NULL || map_find(MAP_TYPE_MBR_PART) == NULL) {
124                 warnx("%s: error: no partitions to convert",
125                     device_name);
126                 return;
127         }
128
129         mbr = map->map_data;
130
131         if (map_find(MAP_TYPE_PRI_GPT_HDR) != NULL ||
132             map_find(MAP_TYPE_SEC_GPT_HDR) != NULL) {
133                 warnx("%s: error: device already contains a GPT", device_name);
134                 return;
135         }
136
137         /* Get the amount of free space after the MBR */
138         blocks = map_unused(1LL, 0LL);
139         if (blocks == 0LL) {
140                 warnx("%s: error: no room for the GPT header", device_name);
141                 return;
142         }
143
144         /* Don't create more than parts entries. */
145         if ((uint64_t)(blocks - 1) * secsz > parts * sizeof(struct gpt_ent)) {
146                 blocks = (parts * sizeof(struct gpt_ent)) / secsz;
147                 if ((parts * sizeof(struct gpt_ent)) % secsz)
148                         blocks++;
149                 blocks++;               /* Don't forget the header itself */
150         }
151
152         /* Never cross the median of the device. */
153         if ((blocks + 1LL) > ((last + 1LL) >> 1))
154                 blocks = ((last + 1LL) >> 1) - 1LL;
155
156         /*
157          * Get the amount of free space at the end of the device and
158          * calculate the size for the GPT structures.
159          */
160         map = map_last();
161         if (map->map_type != MAP_TYPE_UNUSED) {
162                 warnx("%s: error: no room for the backup header", device_name);
163                 return;
164         }
165
166         if (map->map_size < blocks)
167                 blocks = map->map_size;
168         if (blocks == 1LL) {
169                 warnx("%s: error: no room for the GPT table", device_name);
170                 return;
171         }
172
173         blocks--;               /* Number of blocks in the GPT table. */
174         gpt = map_add(1LL, 1LL, MAP_TYPE_PRI_GPT_HDR, calloc(1, secsz));
175         tbl = map_add(2LL, blocks, MAP_TYPE_PRI_GPT_TBL,
176             calloc(blocks, secsz));
177         if (gpt == NULL || tbl == NULL)
178                 return;
179
180         lbt = map_add(last - blocks, blocks, MAP_TYPE_SEC_GPT_TBL,
181             tbl->map_data);
182         tpg = map_add(last, 1LL, MAP_TYPE_SEC_GPT_HDR, calloc(1, secsz));
183
184         hdr = gpt->map_data;
185         memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig));
186         hdr->hdr_revision = GPT_HDR_REVISION;
187         /*
188          * XXX struct gpt_hdr is not a multiple of 8 bytes in size and thus
189          * contains padding we must not include in the size.
190          */
191         hdr->hdr_size = offsetof(struct gpt_hdr, padding);
192         hdr->hdr_lba_self = gpt->map_start;
193         hdr->hdr_lba_alt = tpg->map_start;
194         hdr->hdr_lba_start = tbl->map_start + blocks;
195         hdr->hdr_lba_end = lbt->map_start - 1LL;
196         uuidgen(&hdr->hdr_uuid, 1);
197         hdr->hdr_lba_table = tbl->map_start;
198         hdr->hdr_entries = (blocks * secsz) / sizeof(struct gpt_ent);
199         if (hdr->hdr_entries > parts)
200                 hdr->hdr_entries = parts;
201         hdr->hdr_entsz = sizeof(struct gpt_ent);
202
203         ent = tbl->map_data;
204         for (i = 0; i < hdr->hdr_entries; i++)
205                 uuidgen(&ent[i].ent_uuid, 1);
206
207         /* Mirror partitions. */
208         for (i = 0; i < 4; i++) {
209                 start = mbr->mbr_part[i].part_start_hi;
210                 start = (start << 16) + mbr->mbr_part[i].part_start_lo;
211                 size = mbr->mbr_part[i].part_size_hi;
212                 size = (size << 16) + mbr->mbr_part[i].part_size_lo;
213
214                 switch (mbr->mbr_part[i].part_typ) {
215                 case 165: {     /* FreeBSD */
216                         if (slice) {
217                                 uuid_t freebsd = GPT_ENT_TYPE_FREEBSD;
218                                 ent->ent_type = freebsd;
219                                 ent->ent_lba_start = start;
220                                 ent->ent_lba_end = start + size - 1LL;
221                                 unicode16(ent->ent_name,
222                                     L"FreeBSD disklabel partition", 36);
223                                 ent++;
224                         } else
225                                 ent = migrate_disklabel(fd, start, ent);
226                         break;
227                 }
228                 case 239: {     /* EFI */
229                         uuid_t efi_slice = GPT_ENT_TYPE_EFI;
230                         ent->ent_type = efi_slice;
231                         ent->ent_lba_start = start;
232                         ent->ent_lba_end = start + size - 1LL;
233                         unicode16(ent->ent_name, L"EFI system partition", 36);
234                         ent++;
235                         break;
236                 }
237                 default:
238                         continue;
239                 }
240         }
241         ent = tbl->map_data;
242
243         hdr->hdr_crc_table = crc32(ent, hdr->hdr_entries * hdr->hdr_entsz);
244         hdr->hdr_crc_self = crc32(hdr, hdr->hdr_size);
245
246         gpt_write(fd, gpt);
247         gpt_write(fd, tbl);
248
249         /*
250          * Create backup GPT.
251          */
252         memcpy(tpg->map_data, gpt->map_data, secsz);
253         hdr = tpg->map_data;
254         hdr->hdr_lba_self = tpg->map_start;
255         hdr->hdr_lba_alt = gpt->map_start;
256         hdr->hdr_lba_table = lbt->map_start;
257         hdr->hdr_crc_self = 0;                  /* Don't ever forget this! */
258         hdr->hdr_crc_self = crc32(hdr, hdr->hdr_size);
259
260         gpt_write(fd, lbt);
261         gpt_write(fd, tpg);
262
263         if (!keep) {
264                 map = map_find(MAP_TYPE_MBR);
265                 mbr = map->map_data;
266                 /*
267                  * Turn the MBR into a Protective MBR.
268                  */
269                 bzero(mbr->mbr_part, sizeof(mbr->mbr_part));
270                 mbr->mbr_part[0].part_shd = 0xff;
271                 mbr->mbr_part[0].part_ssect = 0xff;
272                 mbr->mbr_part[0].part_scyl = 0xff;
273                 mbr->mbr_part[0].part_typ = 0xee;
274                 mbr->mbr_part[0].part_ehd = 0xff;
275                 mbr->mbr_part[0].part_esect = 0xff;
276                 mbr->mbr_part[0].part_ecyl = 0xff;
277                 mbr->mbr_part[0].part_start_lo = 1;
278                 if (mediasz > 0xffffffff) {
279                         mbr->mbr_part[0].part_size_lo = 0xffff;
280                         mbr->mbr_part[0].part_size_hi = 0xffff;
281                 } else {
282                         mbr->mbr_part[0].part_size_lo = mediasz & 0xffff;
283                         mbr->mbr_part[0].part_size_hi = mediasz >> 16;
284                 }
285                 gpt_write(fd, map);
286         }
287 }
288
289 int
290 cmd_migrate(int argc, char *argv[])
291 {
292         int ch, fd;
293
294         /* Get the migrate options */
295         while ((ch = getopt(argc, argv, "ks")) != -1) {
296                 switch(ch) {
297                 case 'k':
298                         keep = 1;
299                         break;
300                 case 's':
301                         slice = 1;
302                         break;
303                 default:
304                         usage_migrate();
305                 }
306         }
307
308         if (argc == optind)
309                 usage_migrate();
310
311         while (optind < argc) {
312                 fd = gpt_open(argv[optind++]);
313                 if (fd == -1) {
314                         warn("unable to open device '%s'", device_name);
315                         continue;
316                 }
317
318                 migrate(fd);
319
320                 gpt_close(fd);
321         }
322
323         return (0);
324 }