FreeBSD Operating System

Disks

Разбить на страницы
Показывать лекцию целиком

One of the most important parts of running any computer system is handling data on disk. We have already looked at UNIX file handling in Chapter 10. In this chapter, we'll look at two ways to add another disk to your system, and what you should put on them. In addition.

Adding a hard disk

When you installed FreeBSD, you created file systems on at least one hard disk. At a later point, you may want to install additional drives. There are two ways to do this: with sysinstall and with the traditional UNIX command-line utilities.

There was a time when it was dangerous to use sysinstall after the system had been installed: there was a significant chance of shooting yourself in the foot. There's always a chance of doing something wrong when initializing disks, but sysinstall has become a lot better, and now it's the tool of choice. It's good to know the alternatives, though. In this section we'll look at sysinstall, and on page 209 we'll see how to do it manually if sysinstall won't cooperate.

We've been through all the details of disk layout and slices and partitions in Chapter 2, so I won't repeat them here. Basically, to add a new disk to the system, you need to:

  • Install the disk physically. This usually involves power cycling the machine.
  • Barely possibly, format the disk. Without exception, modern disks come preformatted, and you only need to format a disk if it has defects or if it's ancient. In many cases the so-called "format" program doesn't really format at all.
  • If you want to share with other operating systems, create a PC style partition table on the disk.
  • Define a FreeBSD slice (which the PC BIOS calls a "partition").
  • Define the partitions in the FreeBSD slice.
  • Tell the system about the file systems and where to mount them.
  • Create the file systems.
  • These are the same operations that we performed in Chapter 5.

    Disk hard ware installation

    Before you can do anything with the disk, you have to install it in the system. To do this, you must normally shut down the system and turn the power off, though high-end SCSI enclosures allow hot-swapping, changing disks in a running system. If the disk is IDE, and you already have an IDE disk on the controller, you need to set the second disk as "slave" drive. And you may have to set the first disk as "master" drive: if you only have one drive, you don't set any jumpers, but if you have two drives, some disks require you to set jumpers on both disks. If you don't do this, the system will appear to hang during the power-on self test, and will finally report some kind of disk error.

    Adding a SCSI disk is more complicated. You can connect up to 15 SCSI devices to a host adapter, depending on the interface. Many systems restrict the number to 7 for compatibility with older SCSI interfaces. Typically, your first SCSI disk will have the SCSI ID 0, and the host adapter will have the SCSI ID 7. Traditionally, the IDs 4, 5, and 6 are reserved for tape and CD-ROM drives, and the IDs 0 to 3 are reserved for disks, though FreeBSD doesn't impose any restrictions on what goes where.

    What ever kind of disk you're adding, look at the boot messages, which you can retrieve with the dmesg command. For example, if you're planning to add a SCSI device, you might see:

    sym0: <875> port 0xc400-0xc4ff mem 0xec002000-0xec002fff,0xec003000-0xec0030ff irq 10
    at device 9.0 on pci0
    sym0: Symbios NVRAM, ID 7, Fast-20, SE, NO parity
    sym0: open drain IRQ line driver, using on-chip SRAM
    sym0: using LOAD/STORE-based firmware.
    sym0: SCAN FOR LUNS disabled for targets 0.
    sym1: <875> port 0xc800-0xc8ff mem 0xec001000-0xec001fff,0xec000000-0xec0000ff irq 9
    at device 13.0 on pci0
    sym1: No NVRAM, ID 7, Fast-20, SE, parity checking
    further down...
    Waiting 3 seconds for SCSI devices to settle sa0
    at sym0 bus 0 target 3 lun 0
    sa0: <EXABYTE EXB-8505SMBANSH2 0793> Removable Sequential Access SCSI-2 device
    sa0: 5.000MB/s transfers (5.000MHz, offset 11) sa1 at sym0 bus 0 target 4 lun 0
    sa1: <ARCHIVE Python 28849-XXX 4.CM> Removable Sequential Access SCSI-2 device
    sa1: 5.000MB/s transfers (5.000MHz, offset 15) sa2 at sym0 bus 0 target 5 lun 0
    sa2: <TANDBERG TDC 3800 -03:> Removable Sequential Access SCSI-CCS device
    sa2: 3.300MB/s transfers
    pass4 at sym0 bus 0 target 4 lun 1
    pass4: <ARCHIVE Python 28849-XXX 4.CM> Removable Changer SCSI-2 device
    pass4: 5.000MB/s transfers (5.000MHz, offset 15) cd0 at sym0 bus 0 target 6 lun 0
    cd0: <NRC MBR-7 110> Removable CD-ROM SCSI-2 device
    cd0: 3.300MB/s transfers
    cd0: cd present [322265 x 2048 byte records]
    da0 at sym1 bus 0 target 3 lun 0
    da0: <SEAGATE ST15230W SUN4.2G 0738> Fixed Direct Access SCSI-2 device
    da0: 20.000MB/s transfers (10.000MHz, offset 15, 16bit), Tagged Queueing Enabled
    da0: 4095MB (8386733 512 byte sectors: 255H 63s/t 522C)
    

    This output shows two Sym bios SCSI host adapters /dev/(sym0 and /dev/syml), three tape drives/dev/(sa0, /dev/sal and /dev/sa2), a CD-ROM drive /dev/(cd0), a tape changer /dev/(pass4), and also a disk drive /dev/da0 on ID 3, which is called a target in these messages. The disk is connected to the second host adapter, and the other devices are connected to the first host adapter.

    Installing an external SCSI device

    External SCSI devices have two cable connectors: one goes towards the host adapter, and the other towards the next device. The order of the devices in the chain does not have to have anything to do with the SCSI ID. This method is called daisy chaining. At the end of the chain, the spare connector may be plugged with a terminator, a set of resistors designed to keep noise off the bus. Some devices have internal terminators, however. When installing an external device, you will have to do one of the following:

  • If you are installing a first external device (one connected directly to the cable connector on the backplane of the host adapter), you will have to ensure that the device provides termination. If you already have atl east one internal device, the host adapter will no longer be at one end of the chain, so you will also have to stop it from providing termination. Modern SCSI host adapters can decide whether they need to terminate, but older host adapters have resistor packs. In the latter case, remove these resistor packs.
  • If you are adding an additional external device, you have two choices: you can remove a cable in the middle of the daisy chain and plug it into your new device. You then connect a new cable from your device to the device from which you removed the original cable.

    Alternatively, you can add the device at the end of the chain. Remove the terminator or turn off the termination, and plug your cable into the spare socket. Insert the terminator in your device (or turn termination on).

  • You can add external SCSI devices to a running system if they're hot-pluggable. It might even work if they're not hot-pluggable, but it's not strictly the correct thing to do, and there's the risk that you might damage something, possibly irreparably. After connecting the devices, powering them up and waiting for them to come ready, run camcontrol rescan. For example, if you added a second disk drive to the second host adapter in the example above, you might see:

    # camcontrol rescan 1
    dal at syml bus 0 target 0 lun 0
    dal: <SEAGATE ST15230W SUN4.2G 0738> Fixed Direct Access SCSI-2 device
    dal: 20.000MB/s transfers (10.000MHz, offset 15, 16bit), Tagged Queueing Enabled
    da1: 4095MB (8386733 512 byte sectors: 255H 63s/t 522C)
    Re-scan of bus 1 was successful
    

    There's a problem with this approach: note that /dev/dal has ID 0, and the already present /dev/da0 has ID 3. If you now reboot the system, they will come up with the device names the other way round. We'll look at this issue in more detail in the next section.

    Installing an internal SCSI device

    Installing an internal SCSI device is much the same as installing an external device. Instead of daisy chains, you have a tat band cable with a number of connectors. Find one that suits you, and plug it into the device. Again, you need to think about termination:

  • If you are installing the device at the end of the chain, it should have termination enabled. You should also disable termination for the device that was previously at the end of the chain. Depending on the device, this may involve removing the physical terminators or setting a jumper.
  • If you are installing the device in the middle of the chain, make sure it does not have termination enabled.
  • In this chapter, we'll look at two ways of installing a drive in an existing SCSI chain. We could be in for a surprise: the device ID we get for the new drive depends on what is currently on the chain. For example, consider our example above, where we have a chain with a single drive on it:

    da0 at sym1 bus 0 target 3 lun 0
    da0: <SEAGATE ST15230W SUN4.2G 0738> Fixed Direct Access SCSI-2 device
    da0: 20.000MB/s transfers (10.000MHz, offset 15, 16bit), Tagged Queueing Enabled
    da0: 4095MB (8386733 512 byte sectors: 255H 63S/T 522C)
    

    This drive on target (ID) 2. If we put our new drive on target 0 and reboot, we see:

    da0 at sym1 bus 0 target 0 lun 0
    da0: <SEAGATE ST15230W SUN4.2G 0738> Fixed Direct Access SCSI-2 device
    da0: 20.000MB/s transfers (10.000MHz, offset 15, 16bit), Tagged Queueing Enabled
    da0: 4095MB (8386733 512 byte sectors: 255H 63S/T 522C)
    da1 at sym1 bus 0 target 3 lun 0
    da1: <SEAGATE ST15230W SUN4.2G 0738> Fixed Direct Access SCSI-2 device
    da1: 20.000MB/s transfers (10.000MHz, offset 15, 16bit), Tagged Queueing Enabled
    da1: 4095MB (8386733 512 byte sectors: 255H 63S/T 522C)
    

    At first glance, this looks reasonable, but that's only because both disks are of the same type. If you look at the target numbers, you'll notice that the new disk is /dev/da0,not /dev/dal. The target ID of the new disk is lower than the target ID of the old disk, so the system recognizes the new disk as /dev/da0, and our previous /dev/da0 has become /dev/dal .

    This change of disk ID can be a problem. One of the first things you do with a new disk is to create new disk labels and file systems. Both offer excellent opportunities to shoot yourself in the foot if you choose the wrong disk: the result would almost certainly be the complete loss of data on that disk. Even apart from such catastrophes, you'll have to edit /etc/fstab before you can mount any file systems that are on the disk. The alternatives are to wire down the device names, or to change the SCSI IDs. In FreeBSD 5.0, you wire down device names and busses by adding entries to the boot configuration file /boot/device.hints. We'll look at that on page 575.

    Formatting the disk

    Formatting is the process of rewriting every sector on the disk with a specific data pattern, one that the electronics find most difficult to reproduce: if they can read this pattern, they can read anything. Microsoft calls this a Microsoft also uses the term high-level format for what we call creating a file system. Obviously it destroys any existing data, so

    If you have anything you want to keep, back it up before formatting. Most modern disks don't need formatting unless they're damaged. In particular, formatting will not help if you're having configuration problems, if you can't get PPP to work or you're running out of disk space. Well, it will solve the disk space problem, but not in the manner you probably desire.

    If you do need to format a SCSI disk, use camcontrol. camcontrol is a control program for SCSI devices, and it includes a lot of useful functions that you can read about in the man page. To format a disk, use the following syntax:

    # camcontrol format dal
    

    Remember that formatting a disk destroys all data on the disk. Before using the command, make sure that you need to do so: there are relatively few cases that call for formatting a disk. About the only reasons are if you want to change the physical sector size of the disk, or if you are getting "medium format corrupted" errors from the disk in response to read and write requests.

    FreeBSD can format only floppies and SCSI disks. In general it is no longer possible to reformat ATA (IDE) disks, though some manufacturers have programs that can recover from some data problems. In most cases, though, in sufficient to write zeros to the entire disk:

    # dd if=/dev/zero of=/dev/ad1 bs=128k
    

    If this doesn't work, you may find formatting programs on the manufacturer's web site. You'll probably need to run them under a Microsoft platform.

    Using sysinstall

    If you can, use sysinstall to partition your disk. Looking at the dmesg output for our new disk, we see:

    dal at syml bus 0 target 0 lun 0
    dal: <SEAGATE ST15230W SUN4.2G 0738> Fixed Direct Access SCSI-2 device
    dal: 20.000MB/s transfers (10.000MHz, offset 15, 16bit), Tagged Queueing Enabled
    da1: 4095MB (8386733 512 byte sectors: 255H 63s/t 522C)
    

    You see the standard installation screen (see Chapter 5, page 60). Select Index, then Partition, and you see the following screen:

    (рис 11.1) Disk selection menu

    In this case, we want to partition /dev/dai, so we position the cursor on da1 (as shown) and press Enter. We see the disk partition menu, which shows that the disk currently contains three partitions:

  • The first starts at offset 0, and has a length of 63. This is not unused, no matter what the description says. It's the partition table, padded to the length of a "track."
  • The next partition takes up the bulk of the drive and is a Microsoft partition.
  • Finally, we have 803 sectors left over as a result of the partitioning scheme. Sometimes this can be much larger—I have seen values as high as 35 MB. This is the price we pay for compatibility with PC BIOS partitioning.
  • We want a FreeBSD partition, not a Microsoft partition. At this point, we have a number of choices:

    (рис 11.2) Disk partition menu
  • We can change the partition type (called "Sub type" in the menu). It's currently 6, and we would need to change it to 165. Do this with the t command.
  • We could delete the partition by positioning the cursor on the partition information and pressing d, then create a new partition, either with a if we want a single partition, or with c if we want more than one partition.
  • If we're using this disk for FreeBSD only, we don't have to waste even this much space. There is an option "use whole disk for FreeBSD," the so-called "dangerously dedicated" mode. This term comes partially from superstition and partially because some BlOSes expect to find a partition table on the first sector of a disk, and they can't access the disk if they don't find one. If your BIOS has this bug, you'll find this one out pretty quickly when you try to boot. If it doesn't fail on the first boot, it won't fail, though it's barely possible that you might have trouble if you move it to a system with a different BIOS. If you want to use this method, use the undocumented f command.
  • To use the whole disk, we first delete the current partition: we press the cursor down key until it highlights the FreeBSD partition. Then we press d, and the three partitions are joined into one, marked unused.

    The next step is to create a new partition using the entire disk. If we press f, we get the following message:

    We don't get this message if we use the a command: it just automatically assumes Yes. In this case we've decided to use the whole disk, so we move the cursor right to No and press Enter. That gives us a boot manager selection screen:

    This isn't a boot disk, so we don't need any boot record, and it doesn't make any difference what we select. It's tidier, though, to select None as indicated. Then we press q to exit the partition editor, get back to the function index, and select Label. We see:

    The important information on this rather empty looking menu is the information at the top about the free space available. We want to create two partitions: first, a swap partition of 512 MB, and then a file system taking up the rest of the disk. We press C, and are shown a submenu offering us all 8386733 blocks on the disk. We erase that and enter 512m, which represents 512 MB. Then we press Enter, and another submenu appears, asking us what kind of slice it is. We move the cursor down to select A swap partition :

    Next, we press c again to create a new partition. This time, we accept the offer of the rest of the space on the disk, 7338157 sectors, we select A file system , and we are presented with yet another menu asking for the name of the file system. We enter the name, in this case /S:

    After pressing Enter, we see:

    Finally, we press W to tell the disk label editor to perform the function. We get an additional warning screen:

    We're doing this online, so that's OK. We select Yes, and sysinstall creates the file system and mounts both it and the swap partition. This can take quite a while. Don't try to do anything with the drive until it's finished.

    Doing it the hard way

    Unfortunately, sometimes you may not be able to use the sysinstall method. You may not have access to sysinstall, or you may want to use options that sysinstall doesn't offer. That leaves us with the old way to add disks. The only difference is that this time we need to use different tools. In the following sections, we'll look at what we have to do to install this same 4 GB Seagate drive manually. This time we'll change the partitioning to contain the following partitions:

  • A Microsoft file system.
  • The /newhome file system for our FreeBSD system.
  • Additional swap for the FreeBSD system.
  • We've called this file system /newhome to use it as an example of moving file systems to new disks.

    Creating a partition table

    The first step is to create a PC BIOS style partition table on the disk. As in Microsoft, the partitioning program is called fdisk. In the following discussion, you'll find a pocket calculator indispensable.

    If the disk is not brand new, it will have existing data of some kind on it. Depending on the nature of that data, fdisk could get sufficiently confused to not work correctly. If you don't format the disk, it's a good idea to overwrite the beginning of the disk with dd:

    # dd if=/dev/zero of=/dev/da1 count=100
    100+0 records in 100+0 records out
    51200 bytes transferred in 1 sees (51200 bytes/sec)
    

    We'll as sign 1 GB for Microsoft and use the remaining approximately 3 GB for FreeBSD. Our resulting partition table should look like:

    (рис 11.3) Partition table on second FreeBSD disk

    The Master Boot Record and the Partition Table take up the first sector of the disk, but many of the allocations are track oriented, so the entire first track of the disk is not available for allocation. The rest, up to the end of the last entire cylinder, can be divided between the partitions. It's easy to make a mistake in specifying the parameters, and fdisk performs as good as no checking. You can easily create a partition table that has absolutely no relationship with reality, so it's a good idea to calculate them in advance. For each partition, we need to know three things:

  • The partition type, which fdisk calls sysid. This is a number describing what the partition is used for. FreeBSD partitions have partition type 165, and modern (MS-DOS Release 4 and later) Microsoft partitions have type 6.
  • The start sector, the first sector in the partition.
  • The end sector for the partition.
  • In addition, we need to decide which partition is the active partition, the partition from which we want to boot. In this case, it doesn't make any difference, because we won't be booting from the disk, but it's always a good idea to set it anyway.

    We specify the partitions we don't want by giving them a type, start sector and end sector of 0. Our disk has 8386733 sectors, numbered 0 to 8386732. Partitions should start and end on a cylinder boundary, and we want the Microsoft partition to be about 1 GB. 1GB is 1024 MB, and 1 MB is 2048 sectors of 512 bytes each, so theoretically we want 1024 x 2048, or 2197152 sectors. Because of the requirement that partitions begin and end on a "cylinder" boundary, we need to find the closest number of "cylinders " to this vaflue. First we need to find out how big a "cylinder "is. We can do this by running /fdisk without any options:

    # fdisk dal
    ******* Working on device /dev/dal *******
    parameters extracted from in-core disklabel are:
    cylinders=13726 heads=13 sectors/track=47 (611 blks/cyl)
    
    Figures below won't work with BIOS for partitions not in cyl 1 
    parameters to be used for BIOS calculations are:
    cylinders=13726 heads=13 sectors/track=47 (611 blks/cyl)
    
    fdisk: invalid fdisk partition table found
    Media sector size is 512
    Warning: BIOS sector numbering starts with sector 1 
    Information from DOS bootblock is:
    The data for partition 1 is:
    <UNUSED>
    The data for partition 2 is:
    <UNUSED>
    The data for partition 3 is:
    <UNUSED>
    The data for partition 4 is:
    sysid 165,(FreeBSD/NetBSD/386BSD)
       start 47, size 8386539 (4094 Meg), flag 80 (active)
           beg: cyl 0/ head 1/ sector 1;
           end: cyl 413/ head 12/ sector 47
    

    You'll notice that /fdisk has decided that there is a FreeBSD partition in partition 4. That happens even if the disk is brand new. In fact, this is a less desirable feature of /fdisk: it "suggests" this partition, it's not really there, which can be really confusing. This printout does, however, tell us that /fdisk thinks there are 611 sectors per cylinder, sowe divide 2197152 by 611 and get 3423.327 cylinders. We round down to 3423 cylinders, which provestobe2091453 sectors. This is the length we give to the first partition.

    We use the remaining space for the FreeBSD partition. How much? Well, dmesg tells us that there are 8386733 sectors, but if you look at the geometry that /fdisk outputs, there are 13726 cylinders with 13 heads (tracks) per cylinder and 47 sectors per track. 13726 x 13 x 47 is 8386586. This rounding down is the explanation for the missing data at the end of the disk that we saw on page 204. The best way to calculate the size of the FreeBSD partition is to take the number of cylinders and multiply by the number of tracks per cylinder. The FreeBSD partition starts behind the Microsoft partition, so it goes from cylinder 3423 to cylinder 13725 inclusive or 10303 cylinders. At 611 sectors per cylinder, we have a total of 6295133 sectors in the partition. Our resulting information is:

    sample fdisk parameters
    Partition numberPartitionStartSize
    1612091453
    216520914536295133
    300
    400

    Next we run fdisk in earnest by specifying the -i option. During this time, you may see messages on the console:

    da1: invalid primary partition table: no magic
    

    The message no magic doesn't mean that fdisk is out of purple smoke. It refers to the fact that it didn't find the so-called magic number, which identifies the partition table. We don't have a partition table yet, so this message isn't surprising. It's also completely harmless.

    fdisk prompts interactively when you specify the -i flag:

    # fdisk -i dal
    ******* Working on device /dev/da1 *******
    parameters extracted from in-core disklabel are:
    cylinders=13726 heads=13 sectors/track=47 (611 blks/cyl)
    
    Figures below won't work with BIOS for partitions not in cyl 1
    parameters to be used for BIOS calculations are:
    cylinders=13726 heads=13 sectors/track=47 (611 blks/cyl)
    
    Do you want to change our idea of what BIOS thinks ? [n] Enter pressed Media sector size is 512
    Warning: BIOS sector numbering starts with sector 1
    Information from DOS bootblack is:
    The data for partition 1 is:
    sysid 165,(FreeBSD/NetBSD/386BSD)
       start 0, size 8386733 (4095 Meg), flag 80 (active)
           beg: cyl 0/ head 0/ sector 1; 
           end: cyl 522/ head 12/ sector 47
    Do you want to change it?  [n] y
    Supply a decimal value for "sysid (165=FreeBSD)"  [0] 6
    Supply a decimal value for "start"  [0]    Enter pressed Supply a decimal value for "size"  [0] 2091453
    Explicitly specify beg/end address ?  [n]    Enter pressed
    sysid 6,(Primary 'big' DOS (> 32MB))
       start 0, size 2091453 (1021 Meg), flag 0 
          beg: cyl 0/ head 0/ sector 1;
          end: cyl 350/ head 12/ sector 47
    Are we happy with this entry?  [n] y
    The data for partition 2 is:
    <UNUSED>
    Do you want to change it?  [n] y
    Supply a decimal value for "sysid (165=FreeBSD)"  [0] 165 Supply a decimal value for "start"  [0] 2091453 
    Supply a decimal value for "size"  [0] 6295133 
    Explicitly specify beg/end address ?  [n]    Enter pressed
    sysid 165,(FreeBSD/NetBSD/386BSD)
    start 2091453, size 6295133  (3073 Meg), flag 0
       beg: cyl 351/ head 0/ sector 1;
       end: cyl 413/ head 12/ sector 47
    Are we happy with this entry?  [n] y
    The data for partition 3 is:
    <UNUSED>
    Do you want to change it?  [n] Enter pressed
    The data for partition 4 is:
    sysid 165,(FreeBSD/NetBSD/386BSD)
        start 47, size 8386539 (4094 Meg), flag 80 (active)
          beg: cyl 0/ head 1/ sector 1;
          end: cyl 413/ head 12/ sector 47 
    Do you want to change it?  [n] y
    The static data for the DOS partition 4 has been reinitialized to:
    sysid 165,(FreeBSD/NetBSD/386BSD)
    start 47, size 8386539 (4094 Meg), flag 80 (active)
        beg: cyl 0/ head 1/ sector 1;
        end: cyl 413/ head 12/ sector 47
    Supply a decimal value for "sysid (165=FreeBSD)"  [165] 0 Supply a decimal value for "start"  [47] 0 
    Supply a decimal value for "size"  [8386539] 0 
    Explicitly specify beg/end address ?  [n] Enter pressed 
    <UNUSED>
    Are we happy with this entry?  [n] y 
    Do you want to change the active partition?  [n] y 
    Supply a decimal value for "active partition"  [1] 2 
    Are you happy with this choice [n] y
    
    We haven't changed the partition table yet.   This is your last chance, parameters extracted from in-core disklabel are: 
    cylinders=13726 heads=13 sectors/track=47 (611 blks/cyl)
    
    Figures below won't work with BIOS for partitions not in cyl 1 parameters to be used for BIOS calculations are:
    cylinders=13726 heads=13 sectors/track=47 (611 blks/cyl)
    
    Information from DOS bootblack is:
    1: sysid 6,(Primary 'big' DOS (> 32MB))
        start 0, size 2091453 (1021 Meg), flag 0
          beg: cyl 0/ head 0/ sector 1;
          end: cyl 350/ head 12/ sector 47 
    2: sysid 165,(FreeBSD/NetBSD/386BSD)
        start 2091453, size 6295133  (3073 Meg), flag 80 (active)
          beg: cyl 351/ head 0/ sector 1;
          end: cyl 413/ head 12/ sector 47 
    3: <UNUSED> 
    4: <UNUSED>
    Should we write new partition table?  [n] y
    

    You'll notice a couple of things here:

  • Even though we created valid partitions 1 and 2, which cover the entire drive, fdisk gave us the phantom partition 4 which covered the whole disk, and we had to remove it.
  • The cylinder numbers in the summary at the end don't make any sense. We've already calculated that the Microsoft partition goes from cylinder 0 to cylinder 3422 inclusive, and the FreeBSD partition goes from cylinder 3423 to cylinder 13725. But fdisk says that the Microsoft partition goes from cylinder 0 to cylinder 350 inclusive, and the FreeBSD partition goes from cylinder 351 to cylinder 413. What's that all about?

    The problem here is overflow: once upon a time, the maximum cylinder value was 1023, and /fdisk still thinks this is the case. The numbers we're seeing here are the remainder left by dividing the real cylinder numbers by 1024.

  • Labeling the disk

    Once we have a valid PC BIOS partition table, we need to create the file systems. We won't look at the Microsoft partition in anymore detail, but we still need to do some more work on our FreeBSD slice (slice or PC BIOS partition 2). It'll make life easier here to remember a couple of things:

  • From now on, we're just looking at the slice, which we can think of as a logical disk. Names like disk label really refer to the slice, but many standard terms use the word disk, so we'll continue to use them.
  • All offsets are relative to the beginning of the slice, not the beginning of the disk. Sizes also refer to the slice and not the disk.
  • The first thing we need is the disk (slice) label, which supplies general information about the slice:

  • The fact that it's a FreeBSD slice.
  • The size of the slice.
  • The sizes, types and layout of the file systems.
  • Some obsolete information about details like rotational speed of the disk and the track-to-track switching time. This is still here for historical reasons only. It may go away soon.
  • The only information we need to input is the kind, size and locations of the partitions. In this case, we have decided to create a file system on partition h (/dev/da1s2h) and swap space on partition b (/dev/dalslb). The swap space will be 512 MB, and the file system will take up the rest of the slice. This is mainly tradition: traditionally data disks use the h partition and not the a partition, so we'll stick to that tradition, though there's nothing to stop you from using the a partition if you prefer. In addition, we need to define the c partition, which represents the whole slice. In summary, the FreeBSD slice we want to create looks like:

    TABLE FreeBSD slice on second disk
    /dev/da1s2b: FreeBSD swap, 512 MB
    /dev/da1s2h: /newhome file system, 2.5 GB

    bsdlabel

    The program that writes the disk label used to be called disklabel As FreeBSD migrated to multiple platforms, this proved to be too generic: many hardware platforms have their own disk label formats. For example, FreeBSD on SPARC64 uses the Sun standard labels. On platforms which use the old BSD labels, such as the PC, the name was changed to bsdlabel. On SPARC64 it is called sunlabel. On each platform, the appropriate file is linked to the name disklabel, but some of the options have changed. In addition, the output format now normally ignores a number of historical relics. It's not as warty as fdisk, but it can still give you a run for your money. You can usually ignore most of the complexity, though. You can normally create a disk label with the single command:

    # bsdlabel -w /dev/da1s2 auto
    

    This creates the label with a single partition c. You can look at the label with bsdlabel without options:

    #bsdlabel /dev/da1s2
    #/dev/da0s2:
    8partitions:
    #       size  offset  fstype  [fsize bsize bps/cpg]
     c:  6295133       0  unused     0     0          # "raw" part, don't edit
    

    At this point, the only partition you have is the "whole disk" partition c. You still need to create partitions b and h and specify their location and size. Do this with bsdlabel -e, which starts an editor with the output you see above. Simply add additional partitions:

    8partitions:
    #       size   offset  fstype   [fsize bsize bps/cpg]
     c:  6295133        0  unused      0     0  # "raw" part, don't edit
     b:  1048576        0  swap        0     0
     h:  5246557  1048576  unused      0     0
    

    You don't need to maintain any particular order, and you don't need to specify that partition h will be a file system. In the next step , newfs does that for you automatically.

    Problems running biddable

    Using the old disklabel program used to be like walking through a minefield. Things have got a lot better, but it's possible that some problems are still hiding. Here are some of the problems that have been encountered in the past, along with some suggestions about what to do if you experience them:

  • When writing a label (the –w option), you may find:
    # bsdlabel -w da1s2
    bsdlabel: /dev/da1s2c: Undefined error: 0
    

    This message may be the result of the kernel having out-of-date information about the slice in memory. If this is the case, a reboot may help.

  • No disk label on disk is straightforward enough. You tried to use bsdlabel to look at the label before you had a label to look at.
  • Label magic number or checksum is wrong! tells you that bsdlabel thinks it has a label, but it's invalid. This could be the result of an incorrect previous attempt to label the disk. It can be difficult to get rid of an incorrect label. The best thing to do is to repartition the disk with the label in a different position, and then copy /dev/zero to where the label used to be:
    # dd if=/dev/zero of=/dev/da1 bs=128k count=
    

    Then you can repartition again the way you want to have it.

  • Open partition would move or shrink probably means that you have specified incorrect values in your slice definitions. Check particularly that the c partition corresponds with the definition in the partition table.
  • write: Read-only file system means that you are trying to do something invalid with a valid disk label. FreeBSD write protects the disk label, which is why you get this message.
  • In addition, you might get kernel messages like:
    fixlabel: raw partition size > slice size
    or
    fixlabel: raw partitions offset != slice offset
    

    The meanings of these messages should be obvious.

  • Creating file systems

    Once we have a valid label, we need to create the file systems. In this case, there's only one file system, on /dev/da1s2/z. Mercifully, this is easier:

    # newfs -U /dev/da1s2h
    /dev/vinum/da1s2h: 2561.8MB (5246556 sectors) block size 16384, fragment size 2048 
        using 14 cylinder groups of 183.77MB, 11761 blks, 23552 inodes.
        with soft updates
    super-block backups (for fsck -b #)at:
    160, 376512, 752864, 1129216, 1505568, 1881920, 2258272, 2634624, 3010976, 3387328,
    3763680, 4140032, 4516384, 4892736
    

    The -U flag tells newfs to enable soft updates, which we looked at on page 191.

    Mounting the file systems

    Finally the job is done. Well, almost. You still need to mount the file system, and to tell the system that it has more swap. But that's not much of a problem:

    # mkdir /newhome             make sure we have a directory to mount on
    # mount /dev/da1s2h/newhome  and mount it
    # swapon /dev/da1s2b
    # df                         show free capacity and mounted file systems
    Filesystem    1024-blocks     Used    Avail  Capacity  Mounted on
    /dev/ad0s1a         19966    17426      944     95%    /
    /dev/ad0s1e       1162062   955758   113340     89%    /usr
    procfs                  4        4        0    100%    /proc
    presto:/            15823     6734     8297     45%    /presto/root
    presto:/usr        912271   824927    41730     95%    /presto/usr
    presto:/home      1905583  1193721   521303     70%    /presto/home
    presto:/S         4065286  3339635   563039     86%    /S
    /dev/da1s2h       2540316        2  2337090      0%    /newhome
    # pstat -s                   show  swap usage
    Device          1K-blocks     Used    Avail  Capacity  Type
    /dev/ad0s4b        524160        0   524160      0%    Interleaved
    /dev/da1s2b        524160        0   524160      0%    Interleaved
    Total             1048320        0  1048320      0%
    

    This looks fine, but when you reboot the system, /newhome and the additional swap will be gone. To ensure that they get mounted after booting, you need to add the following lines to /etc/fstab:

    /dev/da1s2b  none      swap  sw  0  0
    /dev/da1s2h  /newhome  ufs   rw  0  0
    

    Moving file systems

    Very frequently, you add a new disk to a system because existing disks have run out of space. Let's consider the disk we have just added and assume that currently the files in /home are physically located on the /usr file system, and that /home is a symbolic link to /usr/home. We want to move them to the new file system and then rename it to /home. Here's what to do:

  • Copy the files:
    #cd /home
    #tar cf - .  |   (cd /newhome; tar xvf – 2>/var/tmp/tarerrors)
    

    This writes any error messages to the file /var/tmp/tarerrors. if you don't do this, any errors will get lost.

  • Check/var/tmp/tarerrors and make sure that the files really made it to the right place!
  • Remove the old files:
    #  rm -rf /usr/home
    
  • In this case, /home was a symbolic link, so we need to remove it and create a directory called /home:
    #rm /home
    #mkdir /home
    

    You don't need to do this if /home was already a directory (for example, if you're moving a complete file system).

  • Modify /etc/fstab to contain a line like:
    /dev/da1s2h  /home  ufs  rw  0  0
    
  • Un mount the /newhome directory and mount it as /home:
    #umount /newhome
    #mount /home
    
  • Recovering from disk data errors

    Modern hard disks are a miracle in evolution. Today you can buy a 200 GB hard disk for under $200, and it will fit in your shirt pocket. Thirty years ago, a typical disk drive was the size of a washing machine and stored 20 MB. You would need 10,000 of them to store 200 GB.

    At the same time, reliability has gone up, but disks are still relatively unreliable devices. You can achieve maximum reliability by keeping them cool, but sooner or later you are going to run into some kind of problem. One kind is due to surface irregularities: the disk can't read a specific part of the surface.

    Modern disks make provisions for recovering from such errors by allocating an alternate sector for the data. IDE drives do this automatically, but with SCSI drives you have the option of enabling or disabling reallocation. Usually reallocation is enabled when you buy the disk, but occasionally it is not. When installing a new disk, you should check that the parameters ARRE (Auto Read Reallocation Enable) and AWRE (Auto Write Reallocation Enable) are turned on. For example, to check and set the values for disk /dev/da1 , you would enter:

    #  camcontrol modepage dal -m 1 -e
    

    This command will start up your favorite editor (either the one specified in the EDITOR environment variable, or vi by default) with the following data:

    AWRE (Auto Write Reallocation Enbld): 0
    ARRE (Auto Read Reallocation Enbld): 0
    TB (Transfer Block): 1
    EER (Enable Early Recovery): 0
    PER (Post Error): 1
    DTE (Disable Transfer on Error): 0
    DCR (Disable Correction): 0
    Read Retry Count: 41
    Write Retry Count: 24
    

    The values for AWRE and ARRE should both be 1. If they aren't, as in this case, where AWRE is 0, change the data with the editor, write it back, and exit. camcontrol writes the data back to the disk and enables the option.

    Note the last two lines in this example. They give the number of actual retries that this drive has performed. You can reset these values too if you want; they will be updated if the drive performs any additional retries.

    Страницы:

    One of the most important parts of running any computer system is handling data on disk. We have already looked at UNIX file handling in Chapter 10. In this chapter, we'll look at two ways to add another disk to your system, and what you should put on them. In addition.

    Adding a hard disk

    When you installed FreeBSD, you created file systems on at least one hard disk. At a later point, you may want to install additional drives. There are two ways to do this: with sysinstall and with the traditional UNIX command-line utilities.

    There was a time when it was dangerous to use sysinstall after the system had been installed: there was a significant chance of shooting yourself in the foot. There's always a chance of doing something wrong when initializing disks, but sysinstall has become a lot better, and now it's the tool of choice. It's good to know the alternatives, though. In this section we'll look at sysinstall, and on page 209 we'll see how to do it manually if sysinstall won't cooperate.

    We've been through all the details of disk layout and slices and partitions in Chapter 2, so I won't repeat them here. Basically, to add a new disk to the system, you need to:

  • Install the disk physically. This usually involves power cycling the machine.
  • Barely possibly, format the disk. Without exception, modern disks come preformatted, and you only need to format a disk if it has defects or if it's ancient. In many cases the so-called "format" program doesn't really format at all.
  • If you want to share with other operating systems, create a PC style partition table on the disk.
  • Define a FreeBSD slice (which the PC BIOS calls a "partition").
  • Define the partitions in the FreeBSD slice.
  • Tell the system about the file systems and where to mount them.
  • Create the file systems.
  • These are the same operations that we performed in Chapter 5.

    Disk hard ware installation

    Before you can do anything with the disk, you have to install it in the system. To do this, you must normally shut down the system and turn the power off, though high-end SCSI enclosures allow hot-swapping, changing disks in a running system. If the disk is IDE, and you already have an IDE disk on the controller, you need to set the second disk as "slave" drive. And you may have to set the first disk as "master" drive: if you only have one drive, you don't set any jumpers, but if you have two drives, some disks require you to set jumpers on both disks. If you don't do this, the system will appear to hang during the power-on self test, and will finally report some kind of disk error.

    Adding a SCSI disk is more complicated. You can connect up to 15 SCSI devices to a host adapter, depending on the interface. Many systems restrict the number to 7 for compatibility with older SCSI interfaces. Typically, your first SCSI disk will have the SCSI ID 0, and the host adapter will have the SCSI ID 7. Traditionally, the IDs 4, 5, and 6 are reserved for tape and CD-ROM drives, and the IDs 0 to 3 are reserved for disks, though FreeBSD doesn't impose any restrictions on what goes where.

    What ever kind of disk you're adding, look at the boot messages, which you can retrieve with the dmesg command. For example, if you're planning to add a SCSI device, you might see:

    sym0: <875> port 0xc400-0xc4ff mem 0xec002000-0xec002fff,0xec003000-0xec0030ff irq 10
    at device 9.0 on pci0
    sym0: Symbios NVRAM, ID 7, Fast-20, SE, NO parity
    sym0: open drain IRQ line driver, using on-chip SRAM
    sym0: using LOAD/STORE-based firmware.
    sym0: SCAN FOR LUNS disabled for targets 0.
    sym1: <875> port 0xc800-0xc8ff mem 0xec001000-0xec001fff,0xec000000-0xec0000ff irq 9
    at device 13.0 on pci0
    sym1: No NVRAM, ID 7, Fast-20, SE, parity checking
    further down...
    Waiting 3 seconds for SCSI devices to settle sa0
    at sym0 bus 0 target 3 lun 0
    sa0: <EXABYTE EXB-8505SMBANSH2 0793> Removable Sequential Access SCSI-2 device
    sa0: 5.000MB/s transfers (5.000MHz, offset 11) sa1 at sym0 bus 0 target 4 lun 0
    sa1: <ARCHIVE Python 28849-XXX 4.CM> Removable Sequential Access SCSI-2 device
    sa1: 5.000MB/s transfers (5.000MHz, offset 15) sa2 at sym0 bus 0 target 5 lun 0
    sa2: <TANDBERG TDC 3800 -03:> Removable Sequential Access SCSI-CCS device
    sa2: 3.300MB/s transfers
    pass4 at sym0 bus 0 target 4 lun 1
    pass4: <ARCHIVE Python 28849-XXX 4.CM> Removable Changer SCSI-2 device
    pass4: 5.000MB/s transfers (5.000MHz, offset 15) cd0 at sym0 bus 0 target 6 lun 0
    cd0: <NRC MBR-7 110> Removable CD-ROM SCSI-2 device
    cd0: 3.300MB/s transfers
    cd0: cd present [322265 x 2048 byte records]
    da0 at sym1 bus 0 target 3 lun 0
    da0: <SEAGATE ST15230W SUN4.2G 0738> Fixed Direct Access SCSI-2 device
    da0: 20.000MB/s transfers (10.000MHz, offset 15, 16bit), Tagged Queueing Enabled
    da0: 4095MB (8386733 512 byte sectors: 255H 63s/t 522C)
    

    This output shows two Sym bios SCSI host adapters /dev/(sym0 and /dev/syml), three tape drives/dev/(sa0, /dev/sal and /dev/sa2), a CD-ROM drive /dev/(cd0), a tape changer /dev/(pass4), and also a disk drive /dev/da0 on ID 3, which is called a target in these messages. The disk is connected to the second host adapter, and the other devices are connected to the first host adapter.

    Installing an external SCSI device

    External SCSI devices have two cable connectors: one goes towards the host adapter, and the other towards the next device. The order of the devices in the chain does not have to have anything to do with the SCSI ID. This method is called daisy chaining. At the end of the chain, the spare connector may be plugged with a terminator, a set of resistors designed to keep noise off the bus. Some devices have internal terminators, however. When installing an external device, you will have to do one of the following:

  • If you are installing a first external device (one connected directly to the cable connector on the backplane of the host adapter), you will have to ensure that the device provides termination. If you already have atl east one internal device, the host adapter will no longer be at one end of the chain, so you will also have to stop it from providing termination. Modern SCSI host adapters can decide whether they need to terminate, but older host adapters have resistor packs. In the latter case, remove these resistor packs.
  • If you are adding an additional external device, you have two choices: you can remove a cable in the middle of the daisy chain and plug it into your new device. You then connect a new cable from your device to the device from which you removed the original cable.

    Alternatively, you can add the device at the end of the chain. Remove the terminator or turn off the termination, and plug your cable into the spare socket. Insert the terminator in your device (or turn termination on).

  • You can add external SCSI devices to a running system if they're hot-pluggable. It might even work if they're not hot-pluggable, but it's not strictly the correct thing to do, and there's the risk that you might damage something, possibly irreparably. After connecting the devices, powering them up and waiting for them to come ready, run camcontrol rescan. For example, if you added a second disk drive to the second host adapter in the example above, you might see:

    # camcontrol rescan 1
    dal at syml bus 0 target 0 lun 0
    dal: <SEAGATE ST15230W SUN4.2G 0738> Fixed Direct Access SCSI-2 device
    dal: 20.000MB/s transfers (10.000MHz, offset 15, 16bit), Tagged Queueing Enabled
    da1: 4095MB (8386733 512 byte sectors: 255H 63s/t 522C)
    Re-scan of bus 1 was successful
    

    There's a problem with this approach: note that /dev/dal has ID 0, and the already present /dev/da0 has ID 3. If you now reboot the system, they will come up with the device names the other way round. We'll look at this issue in more detail in the next section.

    Installing an internal SCSI device

    Installing an internal SCSI device is much the same as installing an external device. Instead of daisy chains, you have a tat band cable with a number of connectors. Find one that suits you, and plug it into the device. Again, you need to think about termination:

  • If you are installing the device at the end of the chain, it should have termination enabled. You should also disable termination for the device that was previously at the end of the chain. Depending on the device, this may involve removing the physical terminators or setting a jumper.
  • If you are installing the device in the middle of the chain, make sure it does not have termination enabled.
  • In this chapter, we'll look at two ways of installing a drive in an existing SCSI chain. We could be in for a surprise: the device ID we get for the new drive depends on what is currently on the chain. For example, consider our example above, where we have a chain with a single drive on it:

    da0 at sym1 bus 0 target 3 lun 0
    da0: <SEAGATE ST15230W SUN4.2G 0738> Fixed Direct Access SCSI-2 device
    da0: 20.000MB/s transfers (10.000MHz, offset 15, 16bit), Tagged Queueing Enabled
    da0: 4095MB (8386733 512 byte sectors: 255H 63S/T 522C)
    

    This drive on target (ID) 2. If we put our new drive on target 0 and reboot, we see:

    da0 at sym1 bus 0 target 0 lun 0
    da0: <SEAGATE ST15230W SUN4.2G 0738> Fixed Direct Access SCSI-2 device
    da0: 20.000MB/s transfers (10.000MHz, offset 15, 16bit), Tagged Queueing Enabled
    da0: 4095MB (8386733 512 byte sectors: 255H 63S/T 522C)
    da1 at sym1 bus 0 target 3 lun 0
    da1: <SEAGATE ST15230W SUN4.2G 0738> Fixed Direct Access SCSI-2 device
    da1: 20.000MB/s transfers (10.000MHz, offset 15, 16bit), Tagged Queueing Enabled
    da1: 4095MB (8386733 512 byte sectors: 255H 63S/T 522C)
    

    At first glance, this looks reasonable, but that's only because both disks are of the same type. If you look at the target numbers, you'll notice that the new disk is /dev/da0,not /dev/dal. The target ID of the new disk is lower than the target ID of the old disk, so the system recognizes the new disk as /dev/da0, and our previous /dev/da0 has become /dev/dal .

    This change of disk ID can be a problem. One of the first things you do with a new disk is to create new disk labels and file systems. Both offer excellent opportunities to shoot yourself in the foot if you choose the wrong disk: the result would almost certainly be the complete loss of data on that disk. Even apart from such catastrophes, you'll have to edit /etc/fstab before you can mount any file systems that are on the disk. The alternatives are to wire down the device names, or to change the SCSI IDs. In FreeBSD 5.0, you wire down device names and busses by adding entries to the boot configuration file /boot/device.hints. We'll look at that on page 575.

    Formatting the disk

    Formatting is the process of rewriting every sector on the disk with a specific data pattern, one that the electronics find most difficult to reproduce: if they can read this pattern, they can read anything. Microsoft calls this a Microsoft also uses the term high-level format for what we call creating a file system. Obviously it destroys any existing data, so

    If you have anything you want to keep, back it up before formatting. Most modern disks don't need formatting unless they're damaged. In particular, formatting will not help if you're having configuration problems, if you can't get PPP to work or you're running out of disk space. Well, it will solve the disk space problem, but not in the manner you probably desire.

    If you do need to format a SCSI disk, use camcontrol. camcontrol is a control program for SCSI devices, and it includes a lot of useful functions that you can read about in the man page. To format a disk, use the following syntax:

    # camcontrol format dal
    

    Remember that formatting a disk destroys all data on the disk. Before using the command, make sure that you need to do so: there are relatively few cases that call for formatting a disk. About the only reasons are if you want to change the physical sector size of the disk, or if you are getting "medium format corrupted" errors from the disk in response to read and write requests.

    FreeBSD can format only floppies and SCSI disks. In general it is no longer possible to reformat ATA (IDE) disks, though some manufacturers have programs that can recover from some data problems. In most cases, though, in sufficient to write zeros to the entire disk:

    # dd if=/dev/zero of=/dev/ad1 bs=128k
    

    If this doesn't work, you may find formatting programs on the manufacturer's web site. You'll probably need to run them under a Microsoft platform.

    Using sysinstall

    If you can, use sysinstall to partition your disk. Looking at the dmesg output for our new disk, we see:

    dal at syml bus 0 target 0 lun 0
    dal: <SEAGATE ST15230W SUN4.2G 0738> Fixed Direct Access SCSI-2 device
    dal: 20.000MB/s transfers (10.000MHz, offset 15, 16bit), Tagged Queueing Enabled
    da1: 4095MB (8386733 512 byte sectors: 255H 63s/t 522C)
    

    You see the standard installation screen (see Chapter 5, page 60). Select Index, then Partition, and you see the following screen:

    (рис 11.1) Disk selection menu

    In this case, we want to partition /dev/dai, so we position the cursor on da1 (as shown) and press Enter. We see the disk partition menu, which shows that the disk currently contains three partitions:

  • The first starts at offset 0, and has a length of 63. This is not unused, no matter what the description says. It's the partition table, padded to the length of a "track."
  • The next partition takes up the bulk of the drive and is a Microsoft partition.
  • Finally, we have 803 sectors left over as a result of the partitioning scheme. Sometimes this can be much larger—I have seen values as high as 35 MB. This is the price we pay for compatibility with PC BIOS partitioning.
  • We want a FreeBSD partition, not a Microsoft partition. At this point, we have a number of choices:

    (рис 11.2) Disk partition menu
  • We can change the partition type (called "Sub type" in the menu). It's currently 6, and we would need to change it to 165. Do this with the t command.
  • We could delete the partition by positioning the cursor on the partition information and pressing d, then create a new partition, either with a if we want a single partition, or with c if we want more than one partition.
  • If we're using this disk for FreeBSD only, we don't have to waste even this much space. There is an option "use whole disk for FreeBSD," the so-called "dangerously dedicated" mode. This term comes partially from superstition and partially because some BlOSes expect to find a partition table on the first sector of a disk, and they can't access the disk if they don't find one. If your BIOS has this bug, you'll find this one out pretty quickly when you try to boot. If it doesn't fail on the first boot, it won't fail, though it's barely possible that you might have trouble if you move it to a system with a different BIOS. If you want to use this method, use the undocumented f command.
  • To use the whole disk, we first delete the current partition: we press the cursor down key until it highlights the FreeBSD partition. Then we press d, and the three partitions are joined into one, marked unused.

    The next step is to create a new partition using the entire disk. If we press f, we get the following message:

    We don't get this message if we use the a command: it just automatically assumes Yes. In this case we've decided to use the whole disk, so we move the cursor right to No and press Enter. That gives us a boot manager selection screen:

    This isn't a boot disk, so we don't need any boot record, and it doesn't make any difference what we select. It's tidier, though, to select None as indicated. Then we press q to exit the partition editor, get back to the function index, and select Label. We see:

    The important information on this rather empty looking menu is the information at the top about the free space available. We want to create two partitions: first, a swap partition of 512 MB, and then a file system taking up the rest of the disk. We press C, and are shown a submenu offering us all 8386733 blocks on the disk. We erase that and enter 512m, which represents 512 MB. Then we press Enter, and another submenu appears, asking us what kind of slice it is. We move the cursor down to select A swap partition :

    Next, we press c again to create a new partition. This time, we accept the offer of the rest of the space on the disk, 7338157 sectors, we select A file system , and we are presented with yet another menu asking for the name of the file system. We enter the name, in this case /S:

    After pressing Enter, we see:

    Finally, we press W to tell the disk label editor to perform the function. We get an additional warning screen:

    We're doing this online, so that's OK. We select Yes, and sysinstall creates the file system and mounts both it and the swap partition. This can take quite a while. Don't try to do anything with the drive until it's finished.

    Doing it the hard way

    Unfortunately, sometimes you may not be able to use the sysinstall method. You may not have access to sysinstall, or you may want to use options that sysinstall doesn't offer. That leaves us with the old way to add disks. The only difference is that this time we need to use different tools. In the following sections, we'll look at what we have to do to install this same 4 GB Seagate drive manually. This time we'll change the partitioning to contain the following partitions:

  • A Microsoft file system.
  • The /newhome file system for our FreeBSD system.
  • Additional swap for the FreeBSD system.
  • We've called this file system /newhome to use it as an example of moving file systems to new disks.

    Creating a partition table

    The first step is to create a PC BIOS style partition table on the disk. As in Microsoft, the partitioning program is called fdisk. In the following discussion, you'll find a pocket calculator indispensable.

    If the disk is not brand new, it will have existing data of some kind on it. Depending on the nature of that data, fdisk could get sufficiently confused to not work correctly. If you don't format the disk, it's a good idea to overwrite the beginning of the disk with dd:

    # dd if=/dev/zero of=/dev/da1 count=100
    100+0 records in 100+0 records out
    51200 bytes transferred in 1 sees (51200 bytes/sec)
    

    We'll as sign 1 GB for Microsoft and use the remaining approximately 3 GB for FreeBSD. Our resulting partition table should look like:

    (рис 11.3) Partition table on second FreeBSD disk

    The Master Boot Record and the Partition Table take up the first sector of the disk, but many of the allocations are track oriented, so the entire first track of the disk is not available for allocation. The rest, up to the end of the last entire cylinder, can be divided between the partitions. It's easy to make a mistake in specifying the parameters, and fdisk performs as good as no checking. You can easily create a partition table that has absolutely no relationship with reality, so it's a good idea to calculate them in advance. For each partition, we need to know three things:

  • The partition type, which fdisk calls sysid. This is a number describing what the partition is used for. FreeBSD partitions have partition type 165, and modern (MS-DOS Release 4 and later) Microsoft partitions have type 6.
  • The start sector, the first sector in the partition.
  • The end sector for the partition.
  • In addition, we need to decide which partition is the active partition, the partition from which we want to boot. In this case, it doesn't make any difference, because we won't be booting from the disk, but it's always a good idea to set it anyway.

    We specify the partitions we don't want by giving them a type, start sector and end sector of 0. Our disk has 8386733 sectors, numbered 0 to 8386732. Partitions should start and end on a cylinder boundary, and we want the Microsoft partition to be about 1 GB. 1GB is 1024 MB, and 1 MB is 2048 sectors of 512 bytes each, so theoretically we want 1024 x 2048, or 2197152 sectors. Because of the requirement that partitions begin and end on a "cylinder" boundary, we need to find the closest number of "cylinders " to this vaflue. First we need to find out how big a "cylinder "is. We can do this by running /fdisk without any options:

    # fdisk dal
    ******* Working on device /dev/dal *******
    parameters extracted from in-core disklabel are:
    cylinders=13726 heads=13 sectors/track=47 (611 blks/cyl)
    
    Figures below won't work with BIOS for partitions not in cyl 1 
    parameters to be used for BIOS calculations are:
    cylinders=13726 heads=13 sectors/track=47 (611 blks/cyl)
    
    fdisk: invalid fdisk partition table found
    Media sector size is 512
    Warning: BIOS sector numbering starts with sector 1 
    Information from DOS bootblock is:
    The data for partition 1 is:
    <UNUSED>
    The data for partition 2 is:
    <UNUSED>
    The data for partition 3 is:
    <UNUSED>
    The data for partition 4 is:
    sysid 165,(FreeBSD/NetBSD/386BSD)
       start 47, size 8386539 (4094 Meg), flag 80 (active)
           beg: cyl 0/ head 1/ sector 1;
           end: cyl 413/ head 12/ sector 47
    

    You'll notice that /fdisk has decided that there is a FreeBSD partition in partition 4. That happens even if the disk is brand new. In fact, this is a less desirable feature of /fdisk: it "suggests" this partition, it's not really there, which can be really confusing. This printout does, however, tell us that /fdisk thinks there are 611 sectors per cylinder, sowe divide 2197152 by 611 and get 3423.327 cylinders. We round down to 3423 cylinders, which provestobe2091453 sectors. This is the length we give to the first partition.

    We use the remaining space for the FreeBSD partition. How much? Well, dmesg tells us that there are 8386733 sectors, but if you look at the geometry that /fdisk outputs, there are 13726 cylinders with 13 heads (tracks) per cylinder and 47 sectors per track. 13726 x 13 x 47 is 8386586. This rounding down is the explanation for the missing data at the end of the disk that we saw on page 204. The best way to calculate the size of the FreeBSD partition is to take the number of cylinders and multiply by the number of tracks per cylinder. The FreeBSD partition starts behind the Microsoft partition, so it goes from cylinder 3423 to cylinder 13725 inclusive or 10303 cylinders. At 611 sectors per cylinder, we have a total of 6295133 sectors in the partition. Our resulting information is:

    sample fdisk parameters
    Partition numberPartitionStartSize
    1612091453
    216520914536295133
    300
    400

    Next we run fdisk in earnest by specifying the -i option. During this time, you may see messages on the console:

    da1: invalid primary partition table: no magic
    

    The message no magic doesn't mean that fdisk is out of purple smoke. It refers to the fact that it didn't find the so-called magic number, which identifies the partition table. We don't have a partition table yet, so this message isn't surprising. It's also completely harmless.

    fdisk prompts interactively when you specify the -i flag:

    # fdisk -i dal
    ******* Working on device /dev/da1 *******
    parameters extracted from in-core disklabel are:
    cylinders=13726 heads=13 sectors/track=47 (611 blks/cyl)
    
    Figures below won't work with BIOS for partitions not in cyl 1
    parameters to be used for BIOS calculations are:
    cylinders=13726 heads=13 sectors/track=47 (611 blks/cyl)
    
    Do you want to change our idea of what BIOS thinks ? [n] Enter pressed Media sector size is 512
    Warning: BIOS sector numbering starts with sector 1
    Information from DOS bootblack is:
    The data for partition 1 is:
    sysid 165,(FreeBSD/NetBSD/386BSD)
       start 0, size 8386733 (4095 Meg), flag 80 (active)
           beg: cyl 0/ head 0/ sector 1; 
           end: cyl 522/ head 12/ sector 47
    Do you want to change it?  [n] y
    Supply a decimal value for "sysid (165=FreeBSD)"  [0] 6
    Supply a decimal value for "start"  [0]    Enter pressed Supply a decimal value for "size"  [0] 2091453
    Explicitly specify beg/end address ?  [n]    Enter pressed
    sysid 6,(Primary 'big' DOS (> 32MB))
       start 0, size 2091453 (1021 Meg), flag 0 
          beg: cyl 0/ head 0/ sector 1;
          end: cyl 350/ head 12/ sector 47
    Are we happy with this entry?  [n] y
    The data for partition 2 is:
    <UNUSED>
    Do you want to change it?  [n] y
    Supply a decimal value for "sysid (165=FreeBSD)"  [0] 165 Supply a decimal value for "start"  [0] 2091453 
    Supply a decimal value for "size"  [0] 6295133 
    Explicitly specify beg/end address ?  [n]    Enter pressed
    sysid 165,(FreeBSD/NetBSD/386BSD)
    start 2091453, size 6295133  (3073 Meg), flag 0
       beg: cyl 351/ head 0/ sector 1;
       end: cyl 413/ head 12/ sector 47
    Are we happy with this entry?  [n] y
    The data for partition 3 is:
    <UNUSED>
    Do you want to change it?  [n] Enter pressed
    The data for partition 4 is:
    sysid 165,(FreeBSD/NetBSD/386BSD)
        start 47, size 8386539 (4094 Meg), flag 80 (active)
          beg: cyl 0/ head 1/ sector 1;
          end: cyl 413/ head 12/ sector 47 
    Do you want to change it?  [n] y
    The static data for the DOS partition 4 has been reinitialized to:
    sysid 165,(FreeBSD/NetBSD/386BSD)
    start 47, size 8386539 (4094 Meg), flag 80 (active)
        beg: cyl 0/ head 1/ sector 1;
        end: cyl 413/ head 12/ sector 47
    Supply a decimal value for "sysid (165=FreeBSD)"  [165] 0 Supply a decimal value for "start"  [47] 0 
    Supply a decimal value for "size"  [8386539] 0 
    Explicitly specify beg/end address ?  [n] Enter pressed 
    <UNUSED>
    Are we happy with this entry?  [n] y 
    Do you want to change the active partition?  [n] y 
    Supply a decimal value for "active partition"  [1] 2 
    Are you happy with this choice [n] y
    
    We haven't changed the partition table yet.   This is your last chance, parameters extracted from in-core disklabel are: 
    cylinders=13726 heads=13 sectors/track=47 (611 blks/cyl)
    
    Figures below won't work with BIOS for partitions not in cyl 1 parameters to be used for BIOS calculations are:
    cylinders=13726 heads=13 sectors/track=47 (611 blks/cyl)
    
    Information from DOS bootblack is:
    1: sysid 6,(Primary 'big' DOS (> 32MB))
        start 0, size 2091453 (1021 Meg), flag 0
          beg: cyl 0/ head 0/ sector 1;
          end: cyl 350/ head 12/ sector 47 
    2: sysid 165,(FreeBSD/NetBSD/386BSD)
        start 2091453, size 6295133  (3073 Meg), flag 80 (active)
          beg: cyl 351/ head 0/ sector 1;
          end: cyl 413/ head 12/ sector 47 
    3: <UNUSED> 
    4: <UNUSED>
    Should we write new partition table?  [n] y
    

    You'll notice a couple of things here:

  • Even though we created valid partitions 1 and 2, which cover the entire drive, fdisk gave us the phantom partition 4 which covered the whole disk, and we had to remove it.
  • The cylinder numbers in the summary at the end don't make any sense. We've already calculated that the Microsoft partition goes from cylinder 0 to cylinder 3422 inclusive, and the FreeBSD partition goes from cylinder 3423 to cylinder 13725. But fdisk says that the Microsoft partition goes from cylinder 0 to cylinder 350 inclusive, and the FreeBSD partition goes from cylinder 351 to cylinder 413. What's that all about?

    The problem here is overflow: once upon a time, the maximum cylinder value was 1023, and /fdisk still thinks this is the case. The numbers we're seeing here are the remainder left by dividing the real cylinder numbers by 1024.

  • Labeling the disk

    Once we have a valid PC BIOS partition table, we need to create the file systems. We won't look at the Microsoft partition in anymore detail, but we still need to do some more work on our FreeBSD slice (slice or PC BIOS partition 2). It'll make life easier here to remember a couple of things:

  • From now on, we're just looking at the slice, which we can think of as a logical disk. Names like disk label really refer to the slice, but many standard terms use the word disk, so we'll continue to use them.
  • All offsets are relative to the beginning of the slice, not the beginning of the disk. Sizes also refer to the slice and not the disk.
  • The first thing we need is the disk (slice) label, which supplies general information about the slice:

  • The fact that it's a FreeBSD slice.
  • The size of the slice.
  • The sizes, types and layout of the file systems.
  • Some obsolete information about details like rotational speed of the disk and the track-to-track switching time. This is still here for historical reasons only. It may go away soon.
  • The only information we need to input is the kind, size and locations of the partitions. In this case, we have decided to create a file system on partition h (/dev/da1s2h) and swap space on partition b (/dev/dalslb). The swap space will be 512 MB, and the file system will take up the rest of the slice. This is mainly tradition: traditionally data disks use the h partition and not the a partition, so we'll stick to that tradition, though there's nothing to stop you from using the a partition if you prefer. In addition, we need to define the c partition, which represents the whole slice. In summary, the FreeBSD slice we want to create looks like:

    TABLE FreeBSD slice on second disk
    /dev/da1s2b: FreeBSD swap, 512 MB
    /dev/da1s2h: /newhome file system, 2.5 GB

    bsdlabel

    The program that writes the disk label used to be called disklabel As FreeBSD migrated to multiple platforms, this proved to be too generic: many hardware platforms have their own disk label formats. For example, FreeBSD on SPARC64 uses the Sun standard labels. On platforms which use the old BSD labels, such as the PC, the name was changed to bsdlabel. On SPARC64 it is called sunlabel. On each platform, the appropriate file is linked to the name disklabel, but some of the options have changed. In addition, the output format now normally ignores a number of historical relics. It's not as warty as fdisk, but it can still give you a run for your money. You can usually ignore most of the complexity, though. You can normally create a disk label with the single command:

    # bsdlabel -w /dev/da1s2 auto
    

    This creates the label with a single partition c. You can look at the label with bsdlabel without options:

    #bsdlabel /dev/da1s2
    #/dev/da0s2:
    8partitions:
    #       size  offset  fstype  [fsize bsize bps/cpg]
     c:  6295133       0  unused     0     0          # "raw" part, don't edit
    

    At this point, the only partition you have is the "whole disk" partition c. You still need to create partitions b and h and specify their location and size. Do this with bsdlabel -e, which starts an editor with the output you see above. Simply add additional partitions:

    8partitions:
    #       size   offset  fstype   [fsize bsize bps/cpg]
     c:  6295133        0  unused      0     0  # "raw" part, don't edit
     b:  1048576        0  swap        0     0
     h:  5246557  1048576  unused      0     0
    

    You don't need to maintain any particular order, and you don't need to specify that partition h will be a file system. In the next step , newfs does that for you automatically.

    Problems running biddable

    Using the old disklabel program used to be like walking through a minefield. Things have got a lot better, but it's possible that some problems are still hiding. Here are some of the problems that have been encountered in the past, along with some suggestions about what to do if you experience them:

  • When writing a label (the –w option), you may find:
    # bsdlabel -w da1s2
    bsdlabel: /dev/da1s2c: Undefined error: 0
    

    This message may be the result of the kernel having out-of-date information about the slice in memory. If this is the case, a reboot may help.

  • No disk label on disk is straightforward enough. You tried to use bsdlabel to look at the label before you had a label to look at.
  • Label magic number or checksum is wrong! tells you that bsdlabel thinks it has a label, but it's invalid. This could be the result of an incorrect previous attempt to label the disk. It can be difficult to get rid of an incorrect label. The best thing to do is to repartition the disk with the label in a different position, and then copy /dev/zero to where the label used to be:
    # dd if=/dev/zero of=/dev/da1 bs=128k count=
    

    Then you can repartition again the way you want to have it.

  • Open partition would move or shrink probably means that you have specified incorrect values in your slice definitions. Check particularly that the c partition corresponds with the definition in the partition table.
  • write: Read-only file system means that you are trying to do something invalid with a valid disk label. FreeBSD write protects the disk label, which is why you get this message.
  • In addition, you might get kernel messages like:
    fixlabel: raw partition size > slice size
    or
    fixlabel: raw partitions offset != slice offset
    

    The meanings of these messages should be obvious.

  • Creating file systems

    Once we have a valid label, we need to create the file systems. In this case, there's only one file system, on /dev/da1s2/z. Mercifully, this is easier:

    # newfs -U /dev/da1s2h
    /dev/vinum/da1s2h: 2561.8MB (5246556 sectors) block size 16384, fragment size 2048 
        using 14 cylinder groups of 183.77MB, 11761 blks, 23552 inodes.
        with soft updates
    super-block backups (for fsck -b #)at:
    160, 376512, 752864, 1129216, 1505568, 1881920, 2258272, 2634624, 3010976, 3387328,
    3763680, 4140032, 4516384, 4892736
    

    The -U flag tells newfs to enable soft updates, which we looked at on page 191.

    Mounting the file systems

    Finally the job is done. Well, almost. You still need to mount the file system, and to tell the system that it has more swap. But that's not much of a problem:

    # mkdir /newhome             make sure we have a directory to mount on
    # mount /dev/da1s2h/newhome  and mount it
    # swapon /dev/da1s2b
    # df                         show free capacity and mounted file systems
    Filesystem    1024-blocks     Used    Avail  Capacity  Mounted on
    /dev/ad0s1a         19966    17426      944     95%    /
    /dev/ad0s1e       1162062   955758   113340     89%    /usr
    procfs                  4        4        0    100%    /proc
    presto:/            15823     6734     8297     45%    /presto/root
    presto:/usr        912271   824927    41730     95%    /presto/usr
    presto:/home      1905583  1193721   521303     70%    /presto/home
    presto:/S         4065286  3339635   563039     86%    /S
    /dev/da1s2h       2540316        2  2337090      0%    /newhome
    # pstat -s                   show  swap usage
    Device          1K-blocks     Used    Avail  Capacity  Type
    /dev/ad0s4b        524160        0   524160      0%    Interleaved
    /dev/da1s2b        524160        0   524160      0%    Interleaved
    Total             1048320        0  1048320      0%
    

    This looks fine, but when you reboot the system, /newhome and the additional swap will be gone. To ensure that they get mounted after booting, you need to add the following lines to /etc/fstab:

    /dev/da1s2b  none      swap  sw  0  0
    /dev/da1s2h  /newhome  ufs   rw  0  0
    

    Moving file systems

    Very frequently, you add a new disk to a system because existing disks have run out of space. Let's consider the disk we have just added and assume that currently the files in /home are physically located on the /usr file system, and that /home is a symbolic link to /usr/home. We want to move them to the new file system and then rename it to /home. Here's what to do:

  • Copy the files:
    #cd /home
    #tar cf - .  |   (cd /newhome; tar xvf – 2>/var/tmp/tarerrors)
    

    This writes any error messages to the file /var/tmp/tarerrors. if you don't do this, any errors will get lost.

  • Check/var/tmp/tarerrors and make sure that the files really made it to the right place!
  • Remove the old files:
    #  rm -rf /usr/home
    
  • In this case, /home was a symbolic link, so we need to remove it and create a directory called /home:
    #rm /home
    #mkdir /home
    

    You don't need to do this if /home was already a directory (for example, if you're moving a complete file system).

  • Modify /etc/fstab to contain a line like:
    /dev/da1s2h  /home  ufs  rw  0  0
    
  • Un mount the /newhome directory and mount it as /home:
    #umount /newhome
    #mount /home
    
  • Recovering from disk data errors

    Modern hard disks are a miracle in evolution. Today you can buy a 200 GB hard disk for under $200, and it will fit in your shirt pocket. Thirty years ago, a typical disk drive was the size of a washing machine and stored 20 MB. You would need 10,000 of them to store 200 GB.

    At the same time, reliability has gone up, but disks are still relatively unreliable devices. You can achieve maximum reliability by keeping them cool, but sooner or later you are going to run into some kind of problem. One kind is due to surface irregularities: the disk can't read a specific part of the surface.

    Modern disks make provisions for recovering from such errors by allocating an alternate sector for the data. IDE drives do this automatically, but with SCSI drives you have the option of enabling or disabling reallocation. Usually reallocation is enabled when you buy the disk, but occasionally it is not. When installing a new disk, you should check that the parameters ARRE (Auto Read Reallocation Enable) and AWRE (Auto Write Reallocation Enable) are turned on. For example, to check and set the values for disk /dev/da1 , you would enter:

    #  camcontrol modepage dal -m 1 -e
    

    This command will start up your favorite editor (either the one specified in the EDITOR environment variable, or vi by default) with the following data:

    AWRE (Auto Write Reallocation Enbld): 0
    ARRE (Auto Read Reallocation Enbld): 0
    TB (Transfer Block): 1
    EER (Enable Early Recovery): 0
    PER (Post Error): 1
    DTE (Disable Transfer on Error): 0
    DCR (Disable Correction): 0
    Read Retry Count: 41
    Write Retry Count: 24
    

    The values for AWRE and ARRE should both be 1. If they aren't, as in this case, where AWRE is 0, change the data with the editor, write it back, and exit. camcontrol writes the data back to the disk and enables the option.

    Note the last two lines in this example. They give the number of actual retries that this drive has performed. You can reset these values too if you want; they will be updated if the drive performs any additional retries.

    Вернуться к учебному плану