kernel - jails - undo minor prisoncount atomic ops
[dragonfly.git] / sbin / gpt / migrate.c
blob3c5a6c92b5685ea8f385c469d8cf4dee83cf7fd2
1 /*-
2 * Copyright (c) 2002 Marcel Moolenaar
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
26 * $FreeBSD: src/sbin/gpt/migrate.c,v 1.16 2005/09/01 02:42:52 marcel Exp $
27 * $DragonFly: src/sbin/gpt/migrate.c,v 1.3 2007/06/18 05:13:39 dillon Exp $
30 #include <sys/types.h>
31 #include <sys/disklabel32.h>
32 #include <sys/dtype.h>
34 #include <err.h>
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
41 #include "map.h"
42 #include "gpt.h"
45 * Allow compilation on platforms that do not have a BSD label.
46 * The values are valid for x86_64, i386 and ia64 disklabels.
48 #ifndef LABELOFFSET
49 #define LABELOFFSET 0
50 #endif
51 #ifndef LABELSECTOR
52 #define LABELSECTOR 1
53 #endif
55 static int force;
56 static int slice;
58 static void
59 usage_migrate(void)
62 fprintf(stderr,
63 "usage: %s [-fs] device ...\n", getprogname());
64 exit(1);
67 static struct gpt_ent*
68 migrate_disklabel(int fd, off_t start, struct gpt_ent *ent)
70 char *buf;
71 struct disklabel32 *dl;
72 off_t ofs, rawofs;
73 int i;
75 buf = gpt_read(fd, start + LABELSECTOR32, 1);
76 dl = (void*)(buf + LABELOFFSET32);
78 if (le32toh(dl->d_magic) != DISKMAGIC32 ||
79 le32toh(dl->d_magic2) != DISKMAGIC32) {
80 warnx("%s: warning: FreeBSD slice without disklabel",
81 device_name);
82 return (ent);
85 rawofs = le32toh(dl->d_partitions[RAW_PART].p_offset) *
86 le32toh(dl->d_secsize);
87 for (i = 0; i < le16toh(dl->d_npartitions); i++) {
88 if (dl->d_partitions[i].p_fstype == FS_UNUSED)
89 continue;
90 ofs = le32toh(dl->d_partitions[i].p_offset) *
91 le32toh(dl->d_secsize);
92 if (ofs < rawofs)
93 rawofs = 0;
95 rawofs /= secsz;
97 for (i = 0; i < le16toh(dl->d_npartitions); i++) {
98 switch (dl->d_partitions[i].p_fstype) {
99 case FS_UNUSED:
100 continue;
101 case FS_SWAP: {
102 uuid_t swap = GPT_ENT_TYPE_FREEBSD_SWAP;
103 le_uuid_enc(&ent->ent_type, &swap);
104 utf8_to_utf16("FreeBSD swap partition",
105 ent->ent_name, 36);
106 break;
108 case FS_BSDFFS: {
109 uuid_t ufs = GPT_ENT_TYPE_FREEBSD_UFS;
110 le_uuid_enc(&ent->ent_type, &ufs);
111 utf8_to_utf16("FreeBSD UFS partition",
112 ent->ent_name, 36);
113 break;
115 case FS_VINUM: {
116 uuid_t vinum = GPT_ENT_TYPE_FREEBSD_VINUM;
117 le_uuid_enc(&ent->ent_type, &vinum);
118 utf8_to_utf16("FreeBSD vinum partition",
119 ent->ent_name, 36);
120 break;
122 default:
123 warnx("%s: warning: unknown FreeBSD partition (%d)",
124 device_name, dl->d_partitions[i].p_fstype);
125 continue;
128 ofs = (le32toh(dl->d_partitions[i].p_offset) *
129 le32toh(dl->d_secsize)) / secsz;
130 ofs = (ofs > 0) ? ofs - rawofs : 0;
131 ent->ent_lba_start = htole64(start + ofs);
132 ent->ent_lba_end = htole64(start + ofs +
133 le32toh(dl->d_partitions[i].p_size) - 1LL);
134 ent++;
137 return (ent);
140 static void
141 migrate(int fd)
143 uuid_t uuid;
144 off_t blocks, last;
145 map_t *gpt, *tpg;
146 map_t *tbl, *lbt;
147 map_t *map;
148 struct gpt_hdr *hdr;
149 struct gpt_ent *ent;
150 struct mbr *mbr;
151 uint32_t start, size;
152 unsigned int i;
154 last = mediasz / secsz - 1LL;
156 map = map_find(MAP_TYPE_MBR);
157 if (map == NULL || map->map_start != 0) {
158 warnx("%s: error: no partitions to convert", device_name);
159 return;
162 mbr = map->map_data;
164 if (map_find(MAP_TYPE_PRI_GPT_HDR) != NULL ||
165 map_find(MAP_TYPE_SEC_GPT_HDR) != NULL) {
166 warnx("%s: error: device already contains a GPT", device_name);
167 return;
170 /* Get the amount of free space after the MBR */
171 blocks = map_free(1LL, 0LL);
172 if (blocks == 0LL) {
173 warnx("%s: error: no room for the GPT header", device_name);
174 return;
177 /* Don't create more than parts entries. */
178 if ((uint64_t)(blocks - 1) * secsz > parts * sizeof(struct gpt_ent)) {
179 blocks = (parts * sizeof(struct gpt_ent)) / secsz;
180 if ((parts * sizeof(struct gpt_ent)) % secsz)
181 blocks++;
182 blocks++; /* Don't forget the header itself */
185 /* Never cross the median of the device. */
186 if ((blocks + 1LL) > ((last + 1LL) >> 1))
187 blocks = ((last + 1LL) >> 1) - 1LL;
190 * Get the amount of free space at the end of the device and
191 * calculate the size for the GPT structures.
193 map = map_last();
194 if (map->map_type != MAP_TYPE_UNUSED) {
195 warnx("%s: error: no room for the backup header", device_name);
196 return;
199 if (map->map_size < blocks)
200 blocks = map->map_size;
201 if (blocks == 1LL) {
202 warnx("%s: error: no room for the GPT table", device_name);
203 return;
206 blocks--; /* Number of blocks in the GPT table. */
207 gpt = map_add(1LL, 1LL, MAP_TYPE_PRI_GPT_HDR, calloc(1, secsz));
208 tbl = map_add(2LL, blocks, MAP_TYPE_PRI_GPT_TBL,
209 calloc(blocks, secsz));
210 if (gpt == NULL || tbl == NULL)
211 return;
213 lbt = map_add(last - blocks, blocks, MAP_TYPE_SEC_GPT_TBL,
214 tbl->map_data);
215 tpg = map_add(last, 1LL, MAP_TYPE_SEC_GPT_HDR, calloc(1, secsz));
217 hdr = gpt->map_data;
218 memcpy(hdr->hdr_sig, GPT_HDR_SIG, sizeof(hdr->hdr_sig));
219 hdr->hdr_revision = htole32(GPT_HDR_REVISION);
221 * XXX struct gpt_hdr is not a multiple of 8 bytes in size and thus
222 * contains padding we must not include in the size.
224 hdr->hdr_size = htole32(offsetof(struct gpt_hdr, padding));
225 hdr->hdr_lba_self = htole64(gpt->map_start);
226 hdr->hdr_lba_alt = htole64(tpg->map_start);
227 hdr->hdr_lba_start = htole64(tbl->map_start + blocks);
228 hdr->hdr_lba_end = htole64(lbt->map_start - 1LL);
229 uuid_create(&uuid, NULL);
230 le_uuid_enc(&hdr->hdr_uuid, &uuid);
231 hdr->hdr_lba_table = htole64(tbl->map_start);
232 hdr->hdr_entries = htole32((blocks * secsz) / sizeof(struct gpt_ent));
233 if (le32toh(hdr->hdr_entries) > parts)
234 hdr->hdr_entries = htole32(parts);
235 hdr->hdr_entsz = htole32(sizeof(struct gpt_ent));
237 ent = tbl->map_data;
238 for (i = 0; i < le32toh(hdr->hdr_entries); i++) {
239 uuid_create(&uuid, NULL);
240 le_uuid_enc(&ent[i].ent_uuid, &uuid);
243 /* Mirror partitions. */
244 for (i = 0; i < 4; i++) {
245 start = le16toh(mbr->mbr_part[i].part_start_hi);
246 start = (start << 16) + le16toh(mbr->mbr_part[i].part_start_lo);
247 size = le16toh(mbr->mbr_part[i].part_size_hi);
248 size = (size << 16) + le16toh(mbr->mbr_part[i].part_size_lo);
250 switch (mbr->mbr_part[i].part_typ) {
251 case 0:
252 continue;
253 case 165: { /* FreeBSD */
254 if (slice) {
255 uuid_t freebsd = GPT_ENT_TYPE_FREEBSD;
256 le_uuid_enc(&ent->ent_type, &freebsd);
257 ent->ent_lba_start = htole64((uint64_t)start);
258 ent->ent_lba_end = htole64(start + size - 1LL);
259 utf8_to_utf16("FreeBSD disklabel partition",
260 ent->ent_name, 36);
261 ent++;
262 } else
263 ent = migrate_disklabel(fd, start, ent);
264 break;
266 case 239: { /* EFI */
267 uuid_t efi_slice = GPT_ENT_TYPE_EFI;
268 le_uuid_enc(&ent->ent_type, &efi_slice);
269 ent->ent_lba_start = htole64((uint64_t)start);
270 ent->ent_lba_end = htole64(start + size - 1LL);
271 utf8_to_utf16("EFI system partition",
272 ent->ent_name, 36);
273 ent++;
274 break;
276 default:
277 if (!force) {
278 warnx("%s: error: unknown partition type (%d)",
279 device_name, mbr->mbr_part[i].part_typ);
280 return;
284 ent = tbl->map_data;
286 hdr->hdr_crc_table = htole32(crc32(ent, le32toh(hdr->hdr_entries) *
287 le32toh(hdr->hdr_entsz)));
288 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
290 gpt_write(fd, gpt);
291 gpt_write(fd, tbl);
294 * Create backup GPT.
296 memcpy(tpg->map_data, gpt->map_data, secsz);
297 hdr = tpg->map_data;
298 hdr->hdr_lba_self = htole64(tpg->map_start);
299 hdr->hdr_lba_alt = htole64(gpt->map_start);
300 hdr->hdr_lba_table = htole64(lbt->map_start);
301 hdr->hdr_crc_self = 0; /* Don't ever forget this! */
302 hdr->hdr_crc_self = htole32(crc32(hdr, le32toh(hdr->hdr_size)));
304 gpt_write(fd, lbt);
305 gpt_write(fd, tpg);
307 map = map_find(MAP_TYPE_MBR);
308 mbr = map->map_data;
310 * Turn the MBR into a Protective MBR.
312 bzero(mbr->mbr_part, sizeof(mbr->mbr_part));
313 mbr->mbr_part[0].part_shd = 0xff;
314 mbr->mbr_part[0].part_ssect = 0xff;
315 mbr->mbr_part[0].part_scyl = 0xff;
316 mbr->mbr_part[0].part_typ = 0xee;
317 mbr->mbr_part[0].part_ehd = 0xff;
318 mbr->mbr_part[0].part_esect = 0xff;
319 mbr->mbr_part[0].part_ecyl = 0xff;
320 mbr->mbr_part[0].part_start_lo = htole16(1);
321 if (last > 0xffffffff) {
322 mbr->mbr_part[0].part_size_lo = htole16(0xffff);
323 mbr->mbr_part[0].part_size_hi = htole16(0xffff);
324 } else {
325 mbr->mbr_part[0].part_size_lo = htole16(last);
326 mbr->mbr_part[0].part_size_hi = htole16(last >> 16);
328 gpt_write(fd, map);
332 cmd_migrate(int argc, char *argv[])
334 int ch, fd;
336 /* Get the migrate options */
337 while ((ch = getopt(argc, argv, "fs")) != -1) {
338 switch(ch) {
339 case 'f':
340 force = 1;
341 break;
342 case 's':
343 slice = 1;
344 break;
345 default:
346 usage_migrate();
350 if (argc == optind)
351 usage_migrate();
353 while (optind < argc) {
354 fd = gpt_open(argv[optind++]);
355 if (fd == -1) {
356 warn("unable to open device '%s'", device_name);
357 continue;
360 migrate(fd);
362 gpt_close(fd);
365 return (0);