From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (qmail 8287 invoked from network); 9 May 1999 00:32:30 -0000 Received: from mail.redhat.com (199.183.24.239) by lists.redhat.com with SMTP; 9 May 1999 00:32:30 -0000 Received: from piglet.twiddle.net (root@piglet.twiddle.net [207.104.6.26]) by mail.redhat.com (8.8.7/8.8.7) with ESMTP id TAA14879 for ; Sat, 8 May 1999 19:58:24 -0400 Received: (from rth@localhost) by piglet.twiddle.net (8.8.7/8.8.7) id QAA03855 for axp-list@redhat.com; Sat, 8 May 1999 16:58:23 -0700 Message-ID: <19990508165823.A3663@twiddle.net> Date: Sat, 8 May 1999 16:58:23 -0700 From: Richard Henderson To: axp-list@redhat.com Subject: disklabel converter [Re: Installed RedHat 5.2 on Alphaserver 1200] References: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="n8g4imXOkfNTN/H1" X-Mailer: Mutt 0.93.2 In-Reply-To: ; from Jose Maria Lopez Lagunas on Thu, May 06, 1999 at 01:12:55PM +0200 --n8g4imXOkfNTN/H1 Content-Type: text/plain; charset=us-ascii 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~ --n8g4imXOkfNTN/H1 Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="labelcvt.c" #include #include #include #include #include #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