Home | History | Annotate | Line # | Download | only in gpt
migrate.c revision 1.25
      1   1.1  christos /*-
      2   1.1  christos  * Copyright (c) 2002 Marcel Moolenaar
      3   1.1  christos  * All rights reserved.
      4   1.1  christos  *
      5   1.1  christos  * Redistribution and use in source and binary forms, with or without
      6   1.1  christos  * modification, are permitted provided that the following conditions
      7   1.1  christos  * are met:
      8   1.1  christos  *
      9   1.1  christos  * 1. Redistributions of source code must retain the above copyright
     10   1.1  christos  *    notice, this list of conditions and the following disclaimer.
     11   1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     13   1.1  christos  *    documentation and/or other materials provided with the distribution.
     14   1.1  christos  *
     15   1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16   1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     17   1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     18   1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     19   1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     20   1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     21   1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     22   1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23   1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     24   1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25   1.1  christos  */
     26   1.1  christos 
     27  1.15  christos #if HAVE_NBTOOL_CONFIG_H
     28  1.15  christos #include "nbtool_config.h"
     29  1.15  christos #endif
     30  1.15  christos 
     31   1.1  christos #include <sys/cdefs.h>
     32   1.2  christos #ifdef __FBSDID
     33   1.1  christos __FBSDID("$FreeBSD: src/sbin/gpt/migrate.c,v 1.16 2005/09/01 02:42:52 marcel Exp $");
     34   1.2  christos #endif
     35   1.2  christos #ifdef __RCSID
     36  1.25  christos __RCSID("$NetBSD: migrate.c,v 1.25 2015/12/01 19:25:24 christos Exp $");
     37   1.2  christos #endif
     38   1.1  christos 
     39   1.1  christos #include <sys/types.h>
     40   1.3        he #include <sys/param.h>
     41  1.17  christos #ifdef HAVE_NBTOOL_CONFIG_H
     42  1.17  christos #include <nbinclude/sys/bootblock.h>
     43  1.17  christos #include <nbinclude/sys/disklabel.h>
     44  1.17  christos #else
     45   1.9   jnemeth #include <sys/bootblock.h>
     46   1.1  christos #include <sys/disklabel.h>
     47  1.17  christos #endif
     48   1.1  christos 
     49   1.1  christos #include <err.h>
     50   1.1  christos #include <stddef.h>
     51   1.1  christos #include <stdio.h>
     52   1.1  christos #include <stdlib.h>
     53   1.1  christos #include <string.h>
     54   1.1  christos #include <unistd.h>
     55   1.1  christos 
     56   1.1  christos #include "map.h"
     57   1.1  christos #include "gpt.h"
     58  1.23  christos #include "gpt_private.h"
     59   1.1  christos 
     60   1.1  christos /*
     61   1.1  christos  * Allow compilation on platforms that do not have a BSD label.
     62   1.1  christos  * The values are valid for amd64, i386 and ia64 disklabels.
     63  1.15  christos  * XXX: use disklabel_params from disklabel.c
     64   1.1  christos  */
     65   1.1  christos #ifndef LABELOFFSET
     66   1.1  christos #define	LABELOFFSET	0
     67   1.1  christos #endif
     68   1.1  christos #ifndef LABELSECTOR
     69   1.1  christos #define	LABELSECTOR	1
     70   1.1  christos #endif
     71  1.15  christos #ifndef RAW_PART
     72  1.15  christos #define	RAW_PART	3
     73  1.15  christos #endif
     74   1.1  christos 
     75  1.10   jnemeth /* FreeBSD filesystem types that don't match corresponding NetBSD types */
     76  1.10   jnemeth #define	FREEBSD_FS_VINUM	14
     77  1.10   jnemeth #define	FREEBSD_FS_ZFS		27
     78  1.10   jnemeth 
     79   1.1  christos static int force;
     80   1.1  christos static int slice;
     81  1.23  christos static u_int parts;
     82   1.1  christos 
     83  1.24  christos static int cmd_migrate(gpt_t, int, char *[]);
     84   1.4       riz 
     85  1.24  christos static const char *migratehelp[] = {
     86  1.24  christos     "[-fs] [-p <partitions>]",
     87  1.24  christos };
     88  1.24  christos 
     89  1.24  christos struct gpt_cmd c_migrate = {
     90  1.24  christos 	"migrate",
     91  1.24  christos 	cmd_migrate,
     92  1.24  christos 	migratehelp, __arraycount(migratehelp),
     93  1.24  christos 	0,
     94  1.24  christos };
     95   1.1  christos 
     96  1.24  christos #define usage() gpt_usage(NULL, &c_migrate)
     97   1.1  christos 
     98  1.23  christos static struct gpt_ent *
     99  1.23  christos migrate_disklabel(gpt_t gpt, off_t start, struct gpt_ent *ent)
    100   1.1  christos {
    101   1.1  christos 	char *buf;
    102   1.1  christos 	struct disklabel *dl;
    103   1.1  christos 	off_t ofs, rawofs;
    104   1.1  christos 	int i;
    105   1.1  christos 
    106  1.23  christos 	buf = gpt_read(gpt, start + LABELSECTOR, 1);
    107  1.23  christos 	if (buf == NULL) {
    108  1.23  christos 		gpt_warn(gpt, "Error reading label");
    109  1.23  christos 		return NULL;
    110  1.23  christos 	}
    111   1.1  christos 	dl = (void*)(buf + LABELOFFSET);
    112   1.1  christos 
    113   1.1  christos 	if (le32toh(dl->d_magic) != DISKMAGIC ||
    114   1.1  christos 	    le32toh(dl->d_magic2) != DISKMAGIC) {
    115  1.23  christos 		gpt_warnx(gpt, "FreeBSD slice without disklabel");
    116  1.12  christos 		free(buf);
    117   1.1  christos 		return (ent);
    118   1.1  christos 	}
    119   1.1  christos 
    120   1.1  christos 	rawofs = le32toh(dl->d_partitions[RAW_PART].p_offset) *
    121   1.1  christos 	    le32toh(dl->d_secsize);
    122   1.1  christos 	for (i = 0; i < le16toh(dl->d_npartitions); i++) {
    123   1.1  christos 		if (dl->d_partitions[i].p_fstype == FS_UNUSED)
    124   1.1  christos 			continue;
    125   1.1  christos 		ofs = le32toh(dl->d_partitions[i].p_offset) *
    126   1.1  christos 		    le32toh(dl->d_secsize);
    127   1.1  christos 		if (ofs < rawofs)
    128   1.1  christos 			rawofs = 0;
    129   1.1  christos 	}
    130  1.23  christos 	rawofs /= gpt->secsz;
    131   1.1  christos 
    132   1.1  christos 	for (i = 0; i < le16toh(dl->d_npartitions); i++) {
    133   1.1  christos 		switch (dl->d_partitions[i].p_fstype) {
    134   1.1  christos 		case FS_UNUSED:
    135   1.1  christos 			continue;
    136   1.1  christos 		case FS_SWAP: {
    137  1.19  christos 			gpt_uuid_create(GPT_TYPE_FREEBSD_SWAP, ent->ent_type,
    138  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    139   1.1  christos 			break;
    140   1.1  christos 		}
    141   1.1  christos 		case FS_BSDFFS: {
    142  1.19  christos 			gpt_uuid_create(GPT_TYPE_FREEBSD_UFS, ent->ent_type,
    143  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    144   1.1  christos 			break;
    145   1.1  christos 		}
    146  1.10   jnemeth 		case FREEBSD_FS_VINUM: {
    147  1.19  christos 			gpt_uuid_create(GPT_TYPE_FREEBSD_VINUM, ent->ent_type,
    148  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    149   1.1  christos 			break;
    150   1.1  christos 		}
    151  1.10   jnemeth 		case FREEBSD_FS_ZFS: {
    152  1.19  christos 			gpt_uuid_create(GPT_TYPE_FREEBSD_ZFS, ent->ent_type,
    153  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    154   1.8   jnemeth 			break;
    155   1.8   jnemeth 		}
    156   1.1  christos 		default:
    157  1.23  christos 			gpt_warnx(gpt, "Unknown FreeBSD partition (%d)",
    158  1.23  christos 			    dl->d_partitions[i].p_fstype);
    159   1.1  christos 			continue;
    160   1.1  christos 		}
    161   1.1  christos 
    162   1.1  christos 		ofs = (le32toh(dl->d_partitions[i].p_offset) *
    163  1.23  christos 		    le32toh(dl->d_secsize)) / gpt->secsz;
    164   1.1  christos 		ofs = (ofs > 0) ? ofs - rawofs : 0;
    165   1.1  christos 		ent->ent_lba_start = htole64(start + ofs);
    166   1.1  christos 		ent->ent_lba_end = htole64(start + ofs +
    167   1.1  christos 		    le32toh(dl->d_partitions[i].p_size) - 1LL);
    168   1.1  christos 		ent++;
    169   1.1  christos 	}
    170   1.1  christos 
    171  1.12  christos 	free(buf);
    172  1.23  christos 	return ent;
    173   1.1  christos }
    174   1.1  christos 
    175   1.9   jnemeth static struct gpt_ent*
    176  1.23  christos migrate_netbsd_disklabel(gpt_t gpt, off_t start, struct gpt_ent *ent)
    177   1.9   jnemeth {
    178   1.9   jnemeth 	char *buf;
    179   1.9   jnemeth 	struct disklabel *dl;
    180   1.9   jnemeth 	off_t ofs, rawofs;
    181   1.9   jnemeth 	int i;
    182   1.9   jnemeth 
    183  1.23  christos 	buf = gpt_read(gpt, start + LABELSECTOR, 1);
    184  1.23  christos 	if (buf == NULL) {
    185  1.23  christos 		gpt_warn(gpt, "Error reading label");
    186  1.23  christos 		return NULL;
    187  1.23  christos 	}
    188   1.9   jnemeth 	dl = (void*)(buf + LABELOFFSET);
    189   1.9   jnemeth 
    190   1.9   jnemeth 	if (le32toh(dl->d_magic) != DISKMAGIC ||
    191   1.9   jnemeth 	    le32toh(dl->d_magic2) != DISKMAGIC) {
    192  1.23  christos 		gpt_warnx(gpt, "NetBSD slice without disklabel");
    193  1.12  christos 		free(buf);
    194  1.23  christos 		return ent;
    195   1.9   jnemeth 	}
    196   1.9   jnemeth 
    197   1.9   jnemeth 	rawofs = le32toh(dl->d_partitions[RAW_PART].p_offset) *
    198   1.9   jnemeth 	    le32toh(dl->d_secsize);
    199   1.9   jnemeth 	for (i = 0; i < le16toh(dl->d_npartitions); i++) {
    200   1.9   jnemeth 		if (dl->d_partitions[i].p_fstype == FS_UNUSED)
    201   1.9   jnemeth 			continue;
    202   1.9   jnemeth 		ofs = le32toh(dl->d_partitions[i].p_offset) *
    203   1.9   jnemeth 		    le32toh(dl->d_secsize);
    204   1.9   jnemeth 		if (ofs < rawofs)
    205   1.9   jnemeth 			rawofs = 0;
    206   1.9   jnemeth 	}
    207  1.23  christos 	rawofs /= gpt->secsz;
    208   1.9   jnemeth 
    209   1.9   jnemeth 	for (i = 0; i < le16toh(dl->d_npartitions); i++) {
    210   1.9   jnemeth 		switch (dl->d_partitions[i].p_fstype) {
    211   1.9   jnemeth 		case FS_UNUSED:
    212   1.9   jnemeth 			continue;
    213   1.9   jnemeth 		case FS_SWAP: {
    214  1.19  christos 			gpt_uuid_create(GPT_TYPE_NETBSD_SWAP, ent->ent_type,
    215  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    216   1.9   jnemeth 			break;
    217   1.9   jnemeth 		}
    218   1.9   jnemeth 		case FS_BSDFFS: {
    219  1.19  christos 			gpt_uuid_create(GPT_TYPE_NETBSD_FFS, ent->ent_type,
    220  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    221   1.9   jnemeth 			break;
    222   1.9   jnemeth 		}
    223   1.9   jnemeth 		case FS_BSDLFS: {
    224  1.19  christos 			gpt_uuid_create(GPT_TYPE_NETBSD_LFS, ent->ent_type,
    225  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    226   1.9   jnemeth 			break;
    227   1.9   jnemeth 		}
    228   1.9   jnemeth 		case FS_RAID: {
    229  1.19  christos 			gpt_uuid_create(GPT_TYPE_NETBSD_RAIDFRAME, ent->ent_type,
    230  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    231   1.9   jnemeth 			break;
    232   1.9   jnemeth 		}
    233   1.9   jnemeth 		case FS_CCD: {
    234  1.19  christos 			gpt_uuid_create(GPT_TYPE_NETBSD_CCD, ent->ent_type,
    235  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    236   1.9   jnemeth 			break;
    237   1.9   jnemeth 		}
    238   1.9   jnemeth 		case FS_CGD: {
    239  1.19  christos 			gpt_uuid_create(GPT_TYPE_NETBSD_CGD, ent->ent_type,
    240  1.19  christos 			    ent->ent_name, sizeof(ent->ent_name));
    241   1.9   jnemeth 			break;
    242   1.9   jnemeth 		}
    243   1.9   jnemeth 		default:
    244  1.23  christos 			gpt_warnx(gpt, "Unknown NetBSD partition (%d)",
    245  1.23  christos 			    dl->d_partitions[i].p_fstype);
    246   1.9   jnemeth 			continue;
    247   1.9   jnemeth 		}
    248   1.9   jnemeth 
    249   1.9   jnemeth 		ofs = (le32toh(dl->d_partitions[i].p_offset) *
    250  1.23  christos 		    le32toh(dl->d_secsize)) / gpt->secsz;
    251   1.9   jnemeth 		ofs = (ofs > 0) ? ofs - rawofs : 0;
    252  1.11   jnemeth 		ent->ent_lba_start = htole64(ofs);
    253  1.11   jnemeth 		ent->ent_lba_end = htole64(ofs +
    254   1.9   jnemeth 		    le32toh(dl->d_partitions[i].p_size) - 1LL);
    255   1.9   jnemeth 		ent++;
    256   1.9   jnemeth 	}
    257   1.9   jnemeth 
    258  1.12  christos 	free(buf);
    259  1.23  christos 	return ent;
    260   1.9   jnemeth }
    261   1.9   jnemeth 
    262  1.23  christos static int
    263  1.23  christos migrate(gpt_t gpt)
    264   1.1  christos {
    265  1.25  christos 	off_t last = gpt_last(gpt);
    266  1.23  christos 	map_t map;
    267   1.1  christos 	struct gpt_ent *ent;
    268   1.1  christos 	struct mbr *mbr;
    269   1.1  christos 	uint32_t start, size;
    270   1.1  christos 	unsigned int i;
    271   1.1  christos 
    272  1.23  christos 	map = map_find(gpt, MAP_TYPE_MBR);
    273   1.1  christos 	if (map == NULL || map->map_start != 0) {
    274  1.23  christos 		gpt_warnx(gpt, "No partitions to convert");
    275  1.23  christos 		return -1;
    276   1.1  christos 	}
    277   1.1  christos 
    278   1.1  christos 	mbr = map->map_data;
    279   1.1  christos 
    280  1.25  christos 	if (gpt_create(gpt, last, parts, 0) == -1)
    281  1.23  christos 		return -1;
    282   1.1  christos 
    283  1.23  christos 	ent = gpt->tbl->map_data;
    284   1.1  christos 
    285   1.1  christos 	/* Mirror partitions. */
    286   1.1  christos 	for (i = 0; i < 4; i++) {
    287   1.1  christos 		start = le16toh(mbr->mbr_part[i].part_start_hi);
    288   1.1  christos 		start = (start << 16) + le16toh(mbr->mbr_part[i].part_start_lo);
    289   1.1  christos 		size = le16toh(mbr->mbr_part[i].part_size_hi);
    290   1.1  christos 		size = (size << 16) + le16toh(mbr->mbr_part[i].part_size_lo);
    291   1.1  christos 
    292   1.1  christos 		switch (mbr->mbr_part[i].part_typ) {
    293   1.9   jnemeth 		case MBR_PTYPE_UNUSED:
    294   1.1  christos 			continue;
    295   1.9   jnemeth 		case MBR_PTYPE_386BSD: {	/* FreeBSD */
    296   1.1  christos 			if (slice) {
    297  1.19  christos 				gpt_uuid_create(GPT_TYPE_FREEBSD,
    298  1.19  christos 				    ent->ent_type, ent->ent_name,
    299  1.19  christos 				    sizeof(ent->ent_name));
    300   1.1  christos 				ent->ent_lba_start = htole64((uint64_t)start);
    301   1.1  christos 				ent->ent_lba_end = htole64(start + size - 1LL);
    302   1.1  christos 				ent++;
    303   1.1  christos 			} else
    304  1.23  christos 				ent = migrate_disklabel(gpt, start, ent);
    305   1.1  christos 			break;
    306   1.1  christos 		}
    307   1.9   jnemeth 		case MBR_PTYPE_NETBSD:
    308  1.23  christos 			ent = migrate_netbsd_disklabel(gpt, start, ent);
    309   1.9   jnemeth 			break;
    310   1.9   jnemeth 		case MBR_PTYPE_EFI: {
    311  1.19  christos 			gpt_uuid_create(GPT_TYPE_EFI,
    312  1.19  christos 			    ent->ent_type, ent->ent_name,
    313  1.19  christos 			    sizeof(ent->ent_name));
    314   1.1  christos 			ent->ent_lba_start = htole64((uint64_t)start);
    315   1.1  christos 			ent->ent_lba_end = htole64(start + size - 1LL);
    316   1.1  christos 			ent++;
    317   1.1  christos 			break;
    318   1.1  christos 		}
    319   1.1  christos 		default:
    320   1.1  christos 			if (!force) {
    321  1.23  christos 				gpt_warnx(gpt, "unknown partition type (%d)",
    322  1.23  christos 				    mbr->mbr_part[i].part_typ);
    323  1.23  christos 				return -1;
    324   1.1  christos 			}
    325  1.23  christos 			break;
    326   1.1  christos 		}
    327   1.1  christos 	}
    328   1.1  christos 
    329  1.23  christos 	if (gpt_write_primary(gpt) == -1)
    330  1.23  christos 		return -1;
    331   1.1  christos 
    332  1.23  christos 	if (gpt_write_backup(gpt) == -1)
    333  1.23  christos 		return -1;
    334   1.1  christos 
    335   1.1  christos 	/*
    336   1.1  christos 	 * Turn the MBR into a Protective MBR.
    337   1.1  christos 	 */
    338  1.16  christos 	memset(mbr->mbr_part, 0, sizeof(mbr->mbr_part));
    339  1.23  christos 	gpt_create_pmbr_part(mbr->mbr_part, last);
    340  1.25  christos 	if (gpt_write(gpt, map) == -1) {
    341  1.25  christos 		gpt_warn(gpt, "Cant write PMBR");
    342  1.25  christos 		return -1;
    343  1.25  christos 	}
    344  1.23  christos 	return 0;
    345   1.1  christos }
    346   1.1  christos 
    347  1.24  christos static int
    348  1.23  christos cmd_migrate(gpt_t gpt, int argc, char *argv[])
    349   1.1  christos {
    350  1.23  christos 	int ch;
    351  1.23  christos 
    352  1.23  christos 	parts = 128;
    353   1.1  christos 
    354   1.1  christos 	/* Get the migrate options */
    355  1.23  christos 	while ((ch = getopt(argc, argv, "fp:s")) != -1) {
    356   1.1  christos 		switch(ch) {
    357   1.1  christos 		case 'f':
    358   1.1  christos 			force = 1;
    359   1.1  christos 			break;
    360  1.23  christos 		case 'p':
    361  1.23  christos 			parts = atoi(optarg);
    362  1.23  christos 			break;
    363   1.1  christos 		case 's':
    364   1.1  christos 			slice = 1;
    365   1.1  christos 			break;
    366   1.1  christos 		default:
    367  1.24  christos 			return usage();
    368   1.1  christos 		}
    369   1.1  christos 	}
    370   1.1  christos 
    371  1.23  christos 	if (argc != optind)
    372  1.24  christos 		return usage();
    373   1.1  christos 
    374  1.23  christos 	return migrate(gpt);
    375   1.1  christos }
    376