dostype.c 5.99 KB
Newer Older
1 2
/*
 * Copyright (c) 2005 University of Utah and the Flux Group.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * 
 * {{{EMULAB-LICENSE
 * 
 * This file is part of the Emulab network testbed software.
 * 
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 * 
 * This file is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this file.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * }}}
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
 */

/*
 * Simple hack to set the type of a DOS partition in the MBR partition table.
 * Both Linux and BSD versions of fdisk have enough idiosyncratic behaviours
 * that it got to be a pain to work around them, just to set the type field
 * for mkextrafs.pl.
 *
 * In Linux, fdisk is interactive only and sfdisk had a habit of trashing BSD
 * boot blocks in partition 1 when asked to change the type of partition 4.
 *
 * In FreeBSD, fdisk wants to validate and correct the size and alignment of
 * all the partitions when asked to add the 4th partition.  Since FBSD4 and
 * FBSD5 have differing ideas about BIOS geometry, the size of the 4th part
 * as created by growdisk under the frisbee MFS depended on whether it was
 * a FBSD4 or 5 MFS.  If the system was then booted under the other flavor
 * of FBSD and fdisk were run (from mkextrafs), fdisk would want to tweak
 * the size of the final partition.  It would do this to the on disk copy
 * of the MBR, but not the "in core" version, creating an inconsistancy
 * (e.g. with disklabel) that I could not resolve.
 */

#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#ifndef __CYGWIN__
#include <sys/types.h>
#include <inttypes.h>
#if __FreeBSD__ >= 5
#include <sys/diskmbr.h>
#endif
#endif

#define BOOT_MAGIC	0xAA55

#define DOSBBSECTOR	0
#define DOSPARTOFF	446
#define NDOSPART	4

struct dospart {
	uint8_t		dp_flag;	/* bootstrap flags */
	uint8_t		dp_shd;		/* starting head */
	uint8_t		dp_ssect;	/* starting sector */
	uint8_t		dp_scyl;	/* starting cylinder */
	uint8_t		dp_typ;		/* partition type */
	uint8_t		dp_ehd;		/* end head */
	uint8_t		dp_esect;	/* end sector */
	uint8_t		dp_ecyl;	/* end cylinder */
	uint32_t	dp_start;	/* absolute starting sector number */
	uint32_t	dp_size;	/* partition size in sectors */
};

struct doslabel {
	int8_t		align[sizeof(short)];	/* Force alignment */
	int8_t		pad2[DOSPARTOFF];
	struct dospart	parts[NDOSPART];
	uint16_t	magic;
};
#define DOSLABELSIZE \
	(DOSPARTOFF + NDOSPART*sizeof(struct dospart) + sizeof(uint16_t))

static int force = 0;
static int verbose = 0;
static int fdw = -1;

int fixmbr(int outfd, int slice, int dtype);

static void
usage(void)
{
	fprintf(stderr, "usage: "
		"dostype [-fv] <diskdev> <DOS_part> <DOS_type>\n"
		" -f          force setting of type even if already non-zero\n"
		" -v          verbose output\n"
		" <diskdev>   disk special file to operate on\n"
		" <DOS_part>  DOS partition number to change (1-4)\n"
		" <DOS_type>  DOS partition type to set.\n");
	exit(1);
}	

int
main(int argc, char **argv)
{
	char *disk;
	int pnum, ptype;
	int ch, fd;

	while ((ch = getopt(argc, argv, "fv")) != -1)
		switch(ch) {
		case 'f':
			force++;
			break;
		case 'v':
			verbose++;
			break;
		case '?':
		default:
			usage();
		}
	argc -= optind;
	argv += optind;
	if (argc < 3)
		usage();

	disk = argv[0];
	pnum = atoi(argv[1]);
	if (pnum < 1 || pnum > 4) {
		fprintf(stderr, "Invalid partition number '%s'\n", argv[1]);
		exit(1);
	}
	ptype = (int)strtol(argv[2], 0, 0);
	if (ptype > 255) {
		fprintf(stderr, "Invalid partition type '%s'\n", argv[2]);
		exit(1);
	}

	fd = open(disk, O_RDWR);
#ifdef DIOCSMBR
	/*
	 * Deal with FreeBSD5 funkyness for writing the MBR.  You have to use
	 * an ioctl on the disk to do it.  But, you apparently cannot perform
	 * the ioctl on the "whole disk" device, you have to do it on a slice
	 * device.  So we try opening slice devices until we get one.
	 *
	 * This code was derived from fdisk.
	 */
	if (fd < 0 && errno == EPERM) {
		fd = open(disk, O_RDONLY);
		if (fd >= 0) {
			char sstr[64];
			int p;

			for (p = 1; p <= 4; p++) {
				snprintf(sstr, sizeof sstr, "%ss%d", disk, p);
159
				fdw = open(sstr, O_RDWR);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
				if (fdw >= 0)
					break;
			}
			if (fdw < 0)
				fd = -1;
		}
	}
#endif
	if (fd < 0) {
		perror(disk);
		exit(1);
	}

	return fixmbr(fd, pnum, ptype);
}

int
fixmbr(int outfd, int slice, int dtype)
{
#ifdef __CYGWIN__
	fprintf(stderr, "Does't work under Windows yet\n");
	return 1;
#else
	struct doslabel doslabel;
	int		cc;

	if (lseek(outfd, (off_t)0, SEEK_SET) < 0) {
		perror("Could not seek to DOS label");
		return 1;
	}
	if ((cc = read(outfd, doslabel.pad2, DOSLABELSIZE)) < 0) {
		perror("Could not read DOS label");
		return 1;
	}
	if (cc != DOSLABELSIZE) {
		fprintf(stderr, "Could not get the entire DOS label\n");
 		return 1;
	}
	if (doslabel.magic != BOOT_MAGIC) {
		fprintf(stderr, "Wrong magic number in DOS partition table\n");
 		return 1;
	}

	if (doslabel.parts[slice-1].dp_typ != dtype) {
		if (doslabel.parts[slice-1].dp_typ != 0 && !force) {
			fprintf(stderr, "part%d: type already set, "
				"use '-f' to override\n", slice);
			return 1;
		}
		doslabel.parts[slice-1].dp_typ = dtype;
		if (fdw < 0) {
			if (lseek(outfd, (off_t)0, SEEK_SET) < 0) {
				perror("Could not seek to DOS label");
				return 1;
			}
			cc = write(outfd, doslabel.pad2, DOSLABELSIZE);
			if (cc != DOSLABELSIZE) {
				perror("Could not write DOS label");
				return 1;
			}
		}
#ifdef DIOCSMBR
		else {
			if (ioctl(fdw, DIOCSMBR, doslabel.pad2) < 0) {
				perror("Could not write DOS label");
				return 1;
			}
		}
#endif
		if (verbose)
			printf("Set type of DOS partition %d to %d\n",
			       slice, dtype);
	}
	return 0;
#endif
}