Subject: How to get your partitions right
To: None <port-i386@NetBSD.ORG>
From: Martin Husemann <martin@laurin.teuto.de>
List: port-i386
Date: 04/28/1995 18:35:19
After I spent several days trying to partition my new disk for DOS and
NetBSD with both booting from hard disk I wrote a quick guide to help
others through this. I would appreciate comments, hints, additions, 
improvements, thaughts, anything...


Martin

-- 
"These are the people who popularized Hungarian notation, yet their own API
is a Freudian stream-of-consciousness."   -- Paul DiLascia


How to Partition Your Post-1.0 NetBSD/i386 System
=================================================

1. Intro

   Partitioning disks is an extreamly dangerous action - any mistake will
destroy all data already on that disk. It is an action you will not practice
very often in sysadmin live. This short guide should help you through the
proccess without shooting yourself in the foot. If you do shoot yourself in
the foot although following this guide, I don't take any guarantees. You
better back up your valuable data before starting!

2. What partitions are we talking about

   On the i386 there are two kind's of partitions. In most situations you
have to get both right to get the system working. We'll have a look at the
standard i386 bootstrap to understand the interaction.

   After power on self tests are completed by the BIOS it searches for a boot
device. On a bootable hard disk (on most system only the first HD will be
bootable) it loads the first sector of the disk into memory.  This sector is
called master boot record (MBR) and contains a table of four partitions.
Exactly one of these should be marked 'active'. The BIOS jumps to the code in
the MBR, which looks at the four partitions and finds the active one. It the
loads the first sector of the active partition into memory and jumps to that
code. In the case of NetBSD this first sector contains the program found in
/usr/mdec/sdboot or /usr/mdec/wdboot. This code loads a few more sectors from
the start of the partition, which contain /usr/mdec/bootsd or
/usr/mdec/bootwd. Together (appended in memory) this gives the NetBSD boot
program, found in /usr/src/sys/arch/i386/boot/boot. It asks you for the
kernel to boot, waits for your input and after a little timeout boots the
kernel from the first NetBSD partition of the disks it's been loaded from (or
the kernel you requested).

   But here we are already talking about the second kind of partitions: 
NetBSD partitons are defined in a special block of the hard disk called the
disklabel. There are up to eight NetBSD partitions.

3. Tools

   There are several tools to manipulate the MBR partition table. Both
NetBSD and DOS have a program called "fdisk". The DOS version has some
limitations when working with non-DOS partitions and it can't create NetBSD
partitions. There are other DOS programs which are able to work with
arbitrary partition types, "pfdisk" is the best known. You can find it
on ftp.NetBSD.ORG. All fdisk programs work interactive.

   Disklabel partitions are described in the file /etc/disktab and may
be edited with any texteditor. To transfer them to disk the "disklabel"
tool is used. This program optionaly installs the NetBSD boot code too.
(The DOS boot code is installed by "format" when the "/s" option is given.)

4. Geometry

   To make live still more complicated, some drives show different geometry
on DOS and NetBSD. This is always true for SCSI disks and sometimes for
IDE disks. You have to find out what DOS thinks is the right geometry for
your drive. Some programs show you these data, "mst" and "sysinfo" are
two. If you have IDE disks, look in the BIOS setup. If you have SCSI
disks and an Adaptec controller, the geometry will be 32 sectors by
64 heads giving 2048 blocks per cylinder.

  Write down your disks geometry. You will need it.

  Use the same geometry for NetBSD, ignoring what the kernel tells you
at boot time. It will make your life far easier! You can explicitly tell
NetBSD's fdisk the geometry and you describe the disk to disklabel giving
it the geometry of the disk.

5. Partition layout and partitioning

   Now that you know the basics and the tools you have to decide how to
divide your disk into partitions. If you are devoting your complete disk
to NetBSD you can stop reading now - simply let the install disk create
a disklabel for you and forget about MBR partitions. Disklabel will fake
a MBR partition table with completely wrong data in it - but enough to
let the NetBSD boot code take control and bring your system up.

   If you would like to have other OS on the same disk, make up your mind how
much space each should get. You will need at least one MBR partition for each
OS. NetBSD can split it's MBR partition further into up to eight disklabel
partitions, other OS (DOS or Linux for example) can't - they need one MBR
partition for every of their partitions. Usualy this doesn't matter, but if
you want Linux on your disk reserve one MBR partitions for it's swap space!

   Now lets assume you want DOS and NetBSD and you have nothing on your disk.
(If you already have DOS installed, and it doesn't use all of the disk, just
skip the first few steps. If you have some old partitions on your disk which
you want to get rid of, use DOS fdisk to remove all partitions, reboot DOS
and say "fdisk /mbr". This will create a fresh MBR for you.) You better start
your disk with a DOS partition, since this will waste fewer sectors for  the
boot code (NetBSD's partitions need to start at cylinder boundaries, DOS may
start on any track). Use DOS fdisk to create one primary DOS partion with the
size you like and mark it active.

   If you already have NetBSD bootable from another disk or the install
floppy, boot it and use it's fdisk. Otherwise use pfdisk. Now look at the
partition table: "fdisk /dev/rsd0d" will print the MBR partition table on
disk sd0. Write down start and size of the DOS partition. Now create a MBR
partition to describe which part of the disk NetBSD might use (this includes
all filesystems and swap): "fdisk -u /dev/rsd0d" will modify the MBR
partition table on sd0. Use "165" as the partition type. Note the values you
used as start and size. Make sure your partitions don't overlap and the
NetBSD partion starts and ends at cylinder boundaries (fdisk will mark them
with * if they don't).

   Now create a disklabel description. Edit /etc/disktab and add an entry
describing your disk and the partition layout you came up with. You may
assign up to eight partitions, but the first four have special meaning (to
be exact, only "c" and "d" realy have a special meaning, "a" and "b" merely
are a convention - which you should follow anyway).

   Partition "d" describes the whole disk. It always starts at offset 0 and
has the size of the whole disk. Partition "c" needs to be the same area of
your disk as the MBR partition you create for NetBSD. Give it the same offset
and size as you gave via fdisk. Partition "b" is your swap space and "a" the
root partition. You may create additional partitions starting with letter "e"
(e.g. the dos partition or another filesystem for NetBSD). Make sure your
partitions don't overlap and start at cylinder boundaries. All NetBSD
partitions should be inside the "c" partition, all non-NetBSD partitions
outside ;-)

   The format of the disklabel entry in /etc/disktab resembles termcap entries.
Each item is separated by colons ":" and lines belonging together have the
newline escaped by a backslash "\". There are some fields describing the
whole disk: dt is the disk type (SCSI, IDE, ESDI, ...), se is the size of
one sector in bytes, nt the number of tracks per cylinder, ns the number
of sectors per track and nc the number of cylinders. Other entries describe
one partition, pa is the size of partition a, oa its offset, ta its type
(this should be 4.2BSD for NetBSD filesystems, swap for swap space and
MSDOS for DOS partitions), ba and fa (only used for NetBSD filesystems)
give allocation size an fragmentation size for the filesystem.

   Now let's look at a sample entry:

myDisk|The Disk in my computer: \
	:dt=SCSI:ty=winchester: \
	:se#512:nt#64:ns#32:nc#2049: \
	:pa#3926016:oa#335872:ta=4.2BSD:ba#4096:fa#512: \
	:pb#65536:ob#204800:tb=swap: \
	:pc#3991552:oc#204800: \
	:pd#4196352:od#0: \
	:pe#204768:oe#32:te=MSDOS:

   This Disk has 2 GB, divided into 100 MB for DOS and 1.9 GB for NetBSD.
The NetBSD part of the disk is divided into 64 MB swap space (partition b)
and 1917 MB file system (partition a). The first 32 sectors are left out by
DOS to give space for the MBR in track 0 of the disk, starting the DOS 
partition at track 1. The exact assignements are:

What		Part.	Start	End	Size (Tracks = MB)
---------------------------------------------------------------
dos		e	~0	99	100
swap		b	100	163	64
fs		a	164	2048	1917

and the derived values:

What		Part.	Start	End	Size (Tracks = MB)
---------------------------------------------------------------
all of NetBSD	c	100	2048	1949
whole disk	d	0	2048	2049

   Using the disks geometry (as DOS sees it, see above) given in the disktab
entry (each sector has 512 bytes, each track 32 sectors and there are 64
heads, giving a total of 2048 sectors or 1 MB for each cylinder) we can
translate the listed start and size values into the disklabel entry: multiply
each value by 2048. For example the start of partition a is cylinder 164,
multiplied by 2048 this gives the "oa#" entry of 335872, its size is 1917
cylinders, multiplied by 2048 we get the "pa#" entry of 3926016.

6. Problems

   There are several problems you might hit. Again: do make backups before
you start partitioning your hard disk! This list is incomplete:

 - disklabel blows away your MBR if your disktab entry doesn't match your
   MBR partitions or there is no acceptable NetBSD partition matching 
   disklabels "c" partition.

 - You probably don't have an editor to edit /etc/disktab when the drive you
   are preparing is your first and only. (You can boot from other disks if you
   have more than one, simply tell the NetBSD bootstrap to boot from something
   like "sd(1,a)/netbsd" to boot from drive sd1, partition a)