Red Hat axp-list: Linux on Alpha (1995-2022)
 help / color / mirror / Atom feed
From: Richard Henderson <rth@twiddle.net>
To: axp-list@redhat.com
Subject: disklabel converter [Re: Installed RedHat 5.2 on Alphaserver 1200]
Date: Sat, 8 May 1999 16:58:23 -0700	[thread overview]
Message-ID: <19990508165823.A3663@twiddle.net> (raw)
In-Reply-To: <Pine.LNX.4.10.9905061255380.10846-100000@andercheran.aiind.upv.es>; from Jose Maria Lopez Lagunas on Thu, May 06, 1999 at 01:12:55PM +0200

[-- Attachment #1: Type: text/plain, Size: 565 bytes --]

On Thu, May 06, 1999 at 01:12:55PM +0200, Jose Maria Lopez Lagunas wrote:
> The disk has several partitions. The program fdisk prints the following
> information (I'm not sure):
> 
> sda1    MS-DOS 16bit
> sda2    Extended
> sda5    Linux
> sda6    id. 
> sda7    id.
> sda8    id.
> sda9    Linux swap

You have an MSDOS partition table instaed of a BSD disklabel.

Last week I got fed up with install programs that won't set up a
bsd disklabel correctly and wrote an in-place conversion widget.

It's been used exactly once, but it may save you a reinstall.


r~

[-- Attachment #2: labelcvt.c --]
[-- Type: text/plain, Size: 11898 bytes --]

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>

#define MAXPARTITIONS		16

#define MSDOS_LABEL_OFFSET	446
#define MSDOS_DISKMAGIC		0xAA55

#define MSDOS_FS_EMPTY		0
#define MSDOS_FS_DOS_12		1
#define MSDOS_FS_XENIX_ROOT	2
#define MSDOS_FS_XENIX_USR	3
#define MSDOS_FS_DOS_16		4
#define MSDOS_FS_DOS_EXT	5
#define MSDOS_FS_DOS_16_BIG	6
#define MSDOS_FS_OS2		7
#define MSDOS_FS_AIX		8
#define MSDOS_FS_AIX_BOOTABLE	9
#define MSDOS_FS_OS2_BOOTMAN	10
#define MSDOS_FS_WIN95_FAT32	11
#define MSDOS_FS_WIN98_EXT	15
#define MSDOS_FS_GNU_HURD	0x63
#define MSDOS_FS_MINIX		0x81
#define MSDOS_FS_LINUX_SWAP	0x82
#define MSDOS_FS_LINUX		0x83
#define MSDOS_FS_LINUX_EXT	0x85
#define MSDOS_FS_BSDI		0xb7
#define MSDOS_FS_BSDI_SWAP	0xb8
#define MSDOS_FS_UNKNOWN	0xf0

struct dos_partition
{
  u_int8_t boot_ind;         /* 0x80 - active */
  u_int8_t head;             /* starting head */
  u_int8_t sector;           /* starting sector */
  u_int8_t cyl;              /* starting cylinder */
  u_int8_t sys_ind;          /* What partition type */
  u_int8_t end_head;         /* end head */
  u_int8_t end_sector;       /* end sector */
  u_int8_t end_cyl;          /* end cylinder */
  u_int32_t start_sect;      /* starting sector counting from 0 */
  u_int32_t nr_sects;        /* nr of sectors in partition */
};

struct dos_disklabel
{
  struct dos_partition d_partitions[4];
  u_int16_t magic;
};

#define BSD_LABEL_OFFSET	64
#define BSD_DISKMAGIC		0x82564557

#define BSD_FS_UNUSED		0
#define BSD_FS_SWAP		1
#define BSD_FS_V6		2
#define BSD_FS_V7		3
#define BSD_FS_SYSV		4
#define BSD_FS_V71K		5
#define	BSD_FS_V8		6
#define BSD_FS_BSDFFS		7
#define BSD_FS_EXT2		8
#define BSD_FS_ADVFS		16
#define BSD_FS_LSMpubl		17
#define BSD_FS_LSMpriv		18
#define BSD_FS_LSMsimp		19
#define BSD_FS_UNKNOWN		2

#define BSD_DTYPE_SMD		1
#define BSD_DTYPE_MSCP		2
#define BSD_DTYPE_DEC		3
#define BSD_DTYPE_SCSI		4
#define BSD_DTYPE_ESDI		5
#define BSD_DTYPE_ST506		6

struct bsd_partition
{
  u_int32_t  p_size;               /* number of sectors in partition */
  u_int32_t  p_offset;             /* starting sector */
  u_int32_t  p_fsize;              /* filesystem basic fragment size */
  u_int8_t   p_fstype;             /* filesystem type, see below */
  u_int8_t   p_frag;               /* filesystem fragments per block */
  u_int16_t  p_cpg;                /* filesystem cylinders per group */
};

struct bsd_disklabel {
  u_int32_t  d_magic;                /* the magic number */
  int16_t    d_type;                 /* drive type */
  int16_t    d_subtype;              /* controller/d_type specific */
  char       d_typename[16];         /* type name, e.g. "eagle" */
  char       d_packname[16];                 /* pack identifier */ 
  u_int32_t  d_secsize;              /* # of bytes per sector */
  u_int32_t  d_nsectors;             /* # of data sectors per track */
  u_int32_t  d_ntracks;              /* # of tracks per cylinder */
  u_int32_t  d_ncylinders;           /* # of data cylinders per unit */
  u_int32_t  d_secpercyl;            /* # of data sectors per cylinder */
  u_int32_t  d_secperunit;           /* # of data sectors per unit */
  u_int16_t  d_sparespertrack;       /* # of spare sectors per track */
  u_int16_t  d_sparespercyl;         /* # of spare sectors per cylinder */
  u_int32_t  d_acylinders;           /* # of alt. cylinders per unit */
  u_int16_t  d_rpm;                  /* rotational speed */
  u_int16_t  d_interleave;           /* hardware sector interleave */
  u_int16_t  d_trackskew;            /* sector 0 skew, per track */
  u_int16_t  d_cylskew;              /* sector 0 skew, per cylinder */
  u_int32_t  d_headswitch;           /* head switch time, usec */
  u_int32_t  d_trkseek;              /* track-to-track seek, usec */
  u_int32_t  d_flags;                /* generic flags */
  u_int32_t  d_drivedata[5];         /* drive-type specific information */
  u_int32_t  d_spare[5];             /* reserved for future use */
  u_int32_t  d_magic2;               /* the magic number (again) */
  u_int16_t  d_checksum;             /* xor of data incl. partitions */

  /* filesystem and partition information: */
  u_int16_t  d_npartitions;          /* number of partitions in following */
  u_int32_t  d_bbsize;               /* size of boot area at sn0, bytes */
  u_int32_t  d_sbsize;               /* max size of fs superblock, bytes */
  struct bsd_partition d_partitions[MAXPARTITIONS]; /* actually may be more */
};

struct canonical_partition
{
  u_int64_t start, size;
  int type;
};

static struct canonical_partition partitions[MAXPARTITIONS];

static int
read_dos_disklabel(int fd, u_int64_t disk_size)
{
  struct dos_disklabel disklabel;
  u_int64_t first_sector = 0;
  u_int64_t extended_offset = 0;
  int current_part = 0;

  while (1)
    {
      int i, extended = -1;

      if (lseek(fd, first_sector*512+MSDOS_LABEL_OFFSET, SEEK_SET) < 0)
	return -1;
      if (read(fd, &disklabel, sizeof(disklabel)) != sizeof(disklabel))
	return -1;

      if (disklabel.magic != MSDOS_DISKMAGIC)
	return -2;

      for (i = 0; i < 4; ++i)
	{
	  u_int32_t p_start_sect, p_nr_sects, p_sys_ind;

	  p_start_sect = disklabel.d_partitions[i].start_sect;
	  p_nr_sects = disklabel.d_partitions[i].nr_sects;
	  p_sys_ind = disklabel.d_partitions[i].sys_ind;

	  if (p_nr_sects == 0)
	    continue;

	  if (p_sys_ind == MSDOS_FS_DOS_EXT
	      || p_sys_ind == MSDOS_FS_WIN98_EXT
	      || p_sys_ind == MSDOS_FS_LINUX_EXT)
	    {
	      if (extended == -1)
		extended = i;
	      continue;
	    }

	  /* Check the 3rd and 4th entries -- these sometimes contain
	     random garbage.  */
	  if (i >= 2
	      && (p_start_sect + p_nr_sects > disk_size
		  || first_sector + p_start_sect < first_sector
		  || first_sector + p_start_sect + p_nr_sects < first_sector))
	    continue;

	  partitions[current_part].start = first_sector + p_start_sect;
	  partitions[current_part].size = p_nr_sects;
	  partitions[current_part].type = p_sys_ind;
	  current_part++;
	}

      if (extended < 0)
	break;
      if (current_part < 4)
	current_part = 4;

      first_sector =
	extended_offset + disklabel.d_partitions[extended].start_sect;
      if (extended_offset == 0)
	{
          disk_size = disklabel.d_partitions[extended].nr_sects;
	  extended_offset = first_sector;
	}
    }

  return 0;
}

static int
read_bsd_disklabel(int fd, u_int64_t disk_size)
{
  struct bsd_disklabel disklabel;
  int max_partitions, i, j;

  if (lseek(fd, BSD_LABEL_OFFSET, SEEK_SET) < 0)
    return -1;
  if (read(fd, &disklabel, sizeof(disklabel)) != sizeof(disklabel))
    return -1;

  if (disklabel.d_magic != BSD_DISKMAGIC
      || disklabel.d_magic2 != BSD_DISKMAGIC)
    return -2;

  max_partitions = disklabel.d_npartitions;
  if (max_partitions > MAXPARTITIONS)
    max_partitions = MAXPARTITIONS;

  for (i = 0; i < max_partitions; ++i)
    {
      u_int64_t start, size;
      int type;

      if (disklabel.d_partitions[i].p_fstype == BSD_FS_UNUSED)
	continue;

      start = disklabel.d_partitions[i].p_offset;
      size = disklabel.d_partitions[i].p_size;

      for (j = 0; j < i; ++j)
	{
	  if (partitions[j].type == 0)
	    continue;

	  /* No relationship -- try again.  */
	  if (partitions[j].start + partitions[j].size <= start
	      || partitions[j].start >= start + size)
	    continue;

	  /* Equal -- no need to add.  */
	  if (partitions[j].start == start
	      && partitions[j].size == size)
	    goto no_add;

	  /* BSD living in dos partition.  */
	  if (partitions[j].start <= start
	      && partitions[j].start + partitions[j].size >= start + size)
	    goto add;

	  /* Overlap.  Don't try to add.  */
	  goto no_add;
	}

      add:
	partitions[i].start = start;
	partitions[i].size = size;

	switch (disklabel.d_partitions[i].p_fstype)
	  {
	  case BSD_FS_SWAP:
	    type = MSDOS_FS_LINUX_SWAP;
	    break;
	  case BSD_FS_EXT2:
	    type = MSDOS_FS_LINUX;
	    break;
	  case BSD_FS_BSDFFS:
	    type = MSDOS_FS_BSDI;
	    break;
	  default:
	    type = MSDOS_FS_UNKNOWN;
	    break;
	  }
	partitions[i].type = type;
      no_add:;
    }

  return 0;
}

static int
write_dos_disklabel(int fd)
{
  return -2;
}

static int
write_bsd_disklabel(int fd)
{
  struct bsd_disklabel disklabel;
  int i, num_partitions;

  memset(&disklabel, 0, sizeof(disklabel));
  disklabel.d_magic = BSD_DISKMAGIC;
  disklabel.d_magic2 = BSD_DISKMAGIC;
  disklabel.d_type = BSD_DTYPE_SCSI;
  memcpy(disklabel.d_typename, "SCSI", 4);
  disklabel.d_secsize = 512;

  num_partitions = 0;
  for (i = 0; i < MAXPARTITIONS; ++i)
    {
      int type = partitions[i].type;

      if (type == 0)
	continue;
      switch (type)
	{
	case MSDOS_FS_LINUX:
	  type = BSD_FS_EXT2;
	  break;
	case MSDOS_FS_LINUX_SWAP:
	  type = BSD_FS_SWAP;
	  break;
	case MSDOS_FS_BSDI:
	  type = BSD_FS_BSDFFS;
	  break;
	default:
	  type = BSD_FS_UNKNOWN;
	  break;
	}

      disklabel.d_partitions[i].p_offset = partitions[i].start;
      disklabel.d_partitions[i].p_size = partitions[i].size;
      disklabel.d_partitions[i].p_fstype = type;
      num_partitions = i+1;
    }
  disklabel.d_npartitions = num_partitions;

  if (lseek(fd, BSD_LABEL_OFFSET, SEEK_SET) < 0)
    return -1;
  if (write(fd, &disklabel, sizeof(disklabel)) != sizeof(disklabel))
    return -1;

  return 0;
}

static void
print_partitions(void)
{
  int i;
  for (i = 0; i < MAXPARTITIONS; ++i)
    {
      if (partitions[i].type == 0)
	continue;
      printf("%2d %9ld %9ld %02x\n", i+1, partitions[i].start,
	     partitions[i].size, partitions[i].type);
    }
}

static void __attribute__((noreturn))
usage(void)
{
  fprintf(stderr, "\
Usage: labelcvt <option> <input device> [<output device>]\n\
  Where option is one of\n\
	--dos		Convert to MSDOS disklabel\n\
	--bsd		Convert to BSD disklabel\n\
	--print		Print the current disklabel\n");
  exit(1);
}


int
main(int ac, char **av)
{
  int action, fd, outfd;
  u_int64_t disk_size;

  if (ac != 3 && ac != 4)
    usage();

  if (strcmp(av[1], "--dos") == 0)
    action = 0;
  else if (strcmp(av[1], "--bsd") == 0)
    action = 1;
  else if (strcmp(av[1], "--print") == 0)
    action = 2;
  else
    usage();

  fd = open(av[2], O_RDONLY);
  if (fd < 0)
    {
      perror(av[2]);
      exit(1);
    }

  if (action != 2)
    {
      outfd = open(av[ac-1], O_WRONLY|O_CREAT);
      if (outfd < 0)
	{
	  perror(av[ac-1]);
	  exit(1);
	}
    }

  disk_size = -1;

  switch (action)
    {
    case 0:
      switch (read_bsd_disklabel(fd, disk_size))
	{
	case -1:
	  perror("read bsd disklabel");
	  exit(1);
	case -2:
	  fprintf(stderr, "Not a BSD disklabel.\n");
	  exit(1);
	}
      switch (write_dos_disklabel(outfd))
	{
	case -1:
	  perror("write dos disklabel");
	  exit(1);
	case -2:
	  fprintf(stderr, "Error writing DOS disklabel.\n");
	  exit(1);
	}
      break;

    case 1:
      switch (read_dos_disklabel(fd, disk_size))
	{
	case -1:
	  perror("read dos disklabel");
	  exit(1);
	case -2:
	  fprintf(stderr, "Not a DOS disklabel.\n");
	  exit(1);
	}
      switch (write_bsd_disklabel(outfd))
	{
	case -1:
	  perror("write bsd disklabel");
	  exit(1);
	case -2:
	  fprintf(stderr, "Error writing BSD disklabel.\n");
	  exit(1);
	}
      break;

    case 2:
      {
	const char *current_type;

	errno = 0;
	switch (read_dos_disklabel(fd, disk_size))
	  {
	  case -1:
	    if (errno != 0)
	      {
		perror("read dos disklabel");
		exit(1);
	      }
	    /* FALLTHRU */
	  case -2:
	    switch (read_bsd_disklabel(fd, disk_size))
	      {
	      case -1:
		perror("read bsd disklabel");
		exit(1);
	      case -2:
		fprintf(stderr, "Unknown disklabel format.\n");
		exit(1);
	      default:
		current_type = "BSD";
		break;
	      }
	    break;
	  default:
	    current_type = "DOS";
	    break;
	  }

	printf("Current format: %s\n", current_type);
	print_partitions();
	break;
      }
    }

  exit(0); 
}

  parent reply	other threads:[~1999-05-09  0:32 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-05-06 11:12 Installed RedHat 5.2 on Alphaserver 1200 Jose Maria Lopez Lagunas
1999-05-06 11:59 ` Rich Payne
1999-05-06 15:06 ` DEC-f77 to Linux Duccio Medini
1999-05-07 13:12   ` Matti Aarnio
1999-05-07 14:10     ` Craig P Prescott
1999-05-07 14:42       ` Todd Henderson
1999-05-07 16:24         ` Greg Lindahl
1999-05-07 20:44       ` Jiann-Ming Su
1999-05-07 22:24         ` Greg Lindahl
1999-05-07 23:09           ` Jiann-Ming Su
1999-05-07 23:16             ` Greg Lindahl
1999-05-07 23:21             ` mgraffam
1999-05-09 18:44               ` Jim Gettys
1999-05-11  3:45   ` Aron Skrypeck
1999-05-11  6:48     ` Helge Kreutzmann
1999-05-10 23:59       ` ORBS problem khans
1999-05-06 15:17 ` Installed RedHat 5.2 on Alphaserver 1200 Edwin Hakkennes
1999-05-07 10:37   ` Jose Maria Lopez Lagunas
1999-05-08 23:58 ` Richard Henderson [this message]
1999-05-09  1:48   ` disklabel converter [Re: Installed RedHat 5.2 on Alphaserver 1200] Larry Snyder
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox