Notes for Week 2

Our task for this week is to install the Linux system from your CD-ROM to your hard drive. This will involve three main steps: setting aside space for the system by partitioning the hard drive, installing the software and adjusting the boot menu to include your newly installed system.
  1. All disk media other than floppy disks (including flash drives) contain a partition table and one or more partitions. Each partition has its own independent filesystem, with its own directory structure; there are no directory links from one partition to another. There are two exceptions: partitions used for virtual memory swapping or as raw data for fast data storage do not have a directory structure or individual files; they are treated like a single file of data.

    There are numerous reasons to partition a hard drive. A primary reason is fault tolerance: damage to a disk due to system failure or head crash is almost always contained to a single partition, so partitions not in use cannot be lost except by catastrophic failure of the drive electronics. In addition, partitioning a system can be useful if large portions of the software can be made read-only (protecting them from disk failures, power outages, deletion mistakes and hacking); if you want to be able to boot one of many operating system environments; or if all files requiring backup reside in one or more separate partitions. Finally, swap partitions and raw data partitions are utilized to speed virtual memory paging and high-throughput I/O (such as the storage of captured video).

    Partitions on a drive are specified by appending a number to the drive name. For example, the fourth partition on a disk connected to the secondary slave IDE interface is /dev/hdd4. In contrast, in a Windows environment, drive letters are used for this purpose: C: is defined as the currently active primary partition on the primary master drive. This causes great confusion when a partition is added or deleted, because the drive letters may no longer mean the same thing they did before the change!

    Most Windows systems only utilize one partition. This is largely due to the fact that Microsoft only thinks in terms of one partition, and the software is not designed with the expectation that the user will need more. In fact, the "DOS format" partition table used in the PC environment supports up to

    • four "primary" partitions, or alternatively,
    • up to three primary partitions and one "extended partition", which can contain one or more "logical drive" partitions.

    In the lab, the hard drive (/dev/sda) contains three primary Windows partitions and one extended partition, which in turn contains six Linux partitions. Execute the command "fdisk -l /dev/sda" to list the partition table:

    /dev/sda1Windows 2003 (wone)
    /dev/sda2Windows XP-SP2 (wtwo)
    /dev/sda3a FAT32 utility partition for the lab manager's use only
    /dev/sda4extended
    /dev/sda5Linux /boot
    /dev/sda6Linux swap
    /dev/sda7Linux / (lone)
    /dev/sda8Linux /var (lone)
    /dev/sda9Linux / (ltwo)
    /dev/sda10Linux /var (ltwo)
    The Windows partitions contain an NTFS filesystem for use by XP or 2003, and all of the Linux partitions except the swap contain a standard Ext2 filesystem (see below). All of the Linux systems share the same boot and swap partitions. The boot partition is not normally mounted at run time; it simply contains the portion of the boot manager (GRUB - the GRand Unified Boot loader) and kernel images loaded at startup. The "lone" and "ltwo" Linux systems have each been split into a portion which is normally read-only, containing the root directory tree, and a portion which is writable, containing /var and its directory sub-tree.
    On many systems, any partition which is bootable must reside in the first 1024 cylinders of the hard drive due to an archaic limitation in the BIOS which you should be able to explain. What is it?

    Windows partitions require a DOS partition table and must be primary to be bootable. The boot manager allows you to boot from either of the Windows partitions (making the one selected active and hence identifying it as C:, and hiding the other), or either of the Linux systems.

    We will now create two additional partitions to hold the Linux system you are about to install from the CD-ROM. To do this, use the following commands.

    The DAY section will use sda11 and sda12, while the NIGHT section will use sda13 and sda14.
    Do not enter the "w" command until your partition table looks EXACTLY like the instructor's:
    1. fdisk /dev/sda
      This starts the fdisk program and tells it to operate on the partition table of the first SATA hard drive.
    2. p
      This fdisk command displays the partition table.
    3. n
      This fdisk command tells fdisk to create a new partition; it will be sda11 (DAY) or sda13 (NIGHT).
    4. (Enter)
      This specifies that sda11 (sda13) is to start immediately after sda10 (sda12).
    5. +5120M
      This specifies that the new partition is to be 5 GB (5120 MB) in length.
    6. n
    7. (Enter)
    8. +5120M
      This specifies that sda12 (sda14) will be 5 GB in length.
    9. p
      Stop here until you are sure your partition table looks exactly like the instructor's.
    10. w
      This fdisk command tells fdisk to write the new partition table to the master boot record (sector zero on the hard drive), and terminates fdisk.
    It is important to realize that the only thing you have changed at this point is a few bytes in the partition table in the master boot record. The real work starts next.
    If you wish to install to a PC at home, you must have at least 1 GB of free space on your hard drive (the root partition will need at least 900 MB, and you should have at least 100 MB for /var to be able to do any useful work; even this is a little small for some of the things we will do later in the course: 1250 MB and 250 MB would be better; you'll also want at least 64 MB for a swap partition). You can create your Linux partitions as primary partitions if you only have one Windows partition, using the following sequence of fdisk commands:
    n, p, 2, (Enter), +1250M, n, p, 3, (Enter), +250M, n, p, 4, (Enter), +64M, t, 4, 82, w
    The p commands specify the new partitions to be primary partitions, the number following the p indicates which partition you want to create, and the "t, 4, 82" sequence changes the partition type of partition number 4 to "Linux Swap". If you need to create an extended partition, use the sequence
    n, e, 2, (Enter), (Enter)
    This will create an extended partition containing the remainder of your hard drive, and you can place your Linux partitions in it as logical partitions.

    If you don't have enough room on your drive without shrinking your Windows partition, you will have to use a program like Partition Magic to make the room. That is outside the scope of this course (meaning you're on your own, but it shouldn't be too hard as long as you're careful).

  2. DAY students: enter the command
    install2hd -r sda11 -v sda12 -c hda
    NIGHT students: enter the command
    install2hd -r sda13 -v sda14 -c hda
    This command executes the script /root/bin/install2hd. It essentially copies the root directory from the CD-ROM into the sda11 (sda13) partition, and copies the current /var directory into the sda12 (sda14) partition. The "-c" parameter specifies the device file for the CD/DVD drive. The script will echo each command to stdout as it is executed. Most of the operations it performs are the straightforward creation of sub-directories and the copying of sub-directory trees from the CD-ROM onto the hard drive, but there are several operations that deserve more comment:

    • The command "mke2fs" creates the directory structure for the root directory tree as an Ext2 filesystem.
      An Ext2 filesystem allocates space in blocks which are grouped into "block groups":

      Each block group contains a copy of the "superblock", which describes the global characteristics of the entire filesystem (things like block size, number of groups, etc.). Each group also contains a copy of all of the block group descriptors, each of which contains information about its group. The group descriptors contain information which can be cross-checked against each group's block and "inode" (information node) bitmaps, which are used to control inode and block allocation. Hence an Ext2 filesystem has significant redundancy in its organizational structure, and those structures are distributed uniformly across the partition, allowing for greater fault tolerance.

      Each file has an associated inode, which contains information about the ownership, permissions, size, timestamps and blocks allocated to the file. The pointers to the data blocks for a given file are organized in a tree. Failure of the file tree structure only results in loss of the blocks below the failure point. A directory is simply a file which contains filenames and inode numbers.

      mke2fs will choose a quasi-random value for the number of mounts between mandatory e2fscks (see below) in order to stagger them, so that all filesystems do not get checked on the same startup.
    • The command "mount -t ext2 /dev/sda11 /mnt" (or "mount -t ext2 /dev/sda13 /mnt") causes the new directory tree to be attached to the current directory tree under the /mnt directory. This is called "mounting the filesystem".
      Linux uses the concept of a "mount point" instead of the DOS/Windows idea of a drive letter, as the "root" of a filesystem on a partition or disk. Any directory can be a mount point, although they are typically empty directories. A partition or disk (ie., a floppy or Zip disk) is "mounted" with the mount command, which causes the root directory of the partition or disk to be grafted onto the current directory tree at the mount point. Any files that were in the mount point before the mount are then invisible until the partition or disk is unmounted (with the umount command). For instance, a file named test.txt located in the root directory of a floppy disk would be called "A:test.txt" in Windows; in Linux, when that disk is mounted at the mount point /media/floppy, the file is called "/media/floppy/test.txt". That same file, if the disk is mounted at the mount point "/media", would be called "/media/test.txt", and while it was mounted the original "/media/floppy" directory would be invisible. After unmounting, the original contents would again be visible. Mounting and unmounting private disks, tapes, and non-root partitions is a common phenomenon on multi-user systems.
    • Several directories have been compressed to make more room on the CD-ROM. These directories are not particularly useful when running from the CD-ROM, but will be necessary for future work once we are working from the hard drive. The tar command (which we will discuss in detail in week 8) is responsible for uncompressing and installing these directories.
    • The /dev, /etc, /home, /srv, /root and /tmp directories are all places where files are modified on a continual basis. Therefore these directories are created as symbolic links to their actual counterparts in /var. This way, the root directory tree can be mounted read-only, while the /var partition (in sda12 or sda14) can be mounted writable, and the system can operate normally while most of it is protected from accidental (or malicious) modification.
    • The make-devices script creates the device files under /dev (on sda11 or sda13) and /var/dev (on sda12 or sda14) which will be used when you run from the hard drive. You can't simply copy the ones from the running system, because several of them are very different at runtime (ie., /dev/mem is a pseudo-device that "contains" all of physical memory!).
    • The permissions on a number of executable programs must be modified. These make use of the setuid and setgid bits (which we will discuss in detail in week 6), and there is no place for these in the ISO-9660 filesystem used on CD-ROMs.
    • Finally, the file /etc/fstab must be modified. fstab (which we will discuss in detail in week 5) contains the device to mount point associations used by the running system. It is important for a system running from the hard drive to not continue to look for the CD-ROM for its files!
  3. Now you have installed the software to your hard drive, but there is still no way to enter your new system from the GRUB menu at boot time. To add your system to the menu, perform the following steps:

    1. mount -t ext2 /dev/sda5 /mnt
    2. cd /mnt/grub
    3. jpico menu.lst
      jpico is a command-line editor; we use it here to edit the menu file.
    4. DAY students: after ltwo add the lines
      title lthree
      root (hd0,10)
      kernel --no-mem-option /boot/lfsgeneral root=/dev/sda11
      NIGHT students: after lthree add the lines
      title lfour
      root (hd0,12)
      kernel --no-mem-option /boot/lfsgeneral root=/dev/sda13
      The first line creates a new menu item "lthree" ("lfour"); the second specifies that the root filesystem is on /dev/sda11 (sda13) (GRUB numbering starts with zero, so sda becomes hd0 and 11 becomes 10); and the third line specifies the kernel image file and boot parameters for startup.
      From this point on, use ONLY the Linux system you just created. Students mounting or running on the systems designated for other sections will be subject to failure and academic misconduct charges.
    5. Enter Ctrl-x to save the file (write over the existing file once you make sure you entered the information correctly!).
    6. cd
    7. umount /mnt
    8. reboot
    The system should reboot, and the GRUB menu should now contain an entry for lthree (lfour) which will boot your newly-installed system.
    If you install this software at home, you can install GRUB using the command
    install2hd -r hdXn -b
    or
    install2hd -r sdXn -b
    after the previous install2hd command (substituting your root filesystem partition name for "hdXn" or "sdXn"). This will allow booting of your new Linux partition; however, if you want to dual boot, you must edit the menu.lst file (as above) to add the following lines before the first "title" line:
    title windows
    rootnoverify (hd0,0)
    chainloader +1
    This assumes that your windows partition is hda1 or sda1. If you want to uninstall grub, you can use your windows xp install disk to "repair the master boot record".
  4. Note that when you boot the system for the first time (and periodically thereafter), the system runs a program called e2fsck on each mounted filesystem. e2fsck is used to check the integrity of an ext2 filesystem. It should never be run on a filesystem which is mounted writable. This is because e2fsck examines the filesystem without expecting it to be consistent: it looks at it as a series of blocks on disk, bypassing the filesystem cache. If it were used on a filesystem whose blocks on disk were in an inconsistent state because some of them were modified in the cache, but had not yet been written to disk, it would make incorrect repairs to the disk blocks and the result would be a damaged filesystem. The file /etc/fstab contains the known filesystems and their mount points and types, and flags which determine which filesystems are checked at boot time.

    If a filesystem appears to have been trashed, run e2fsck with the "-n" option before allowing any repairs to be made. This will give you an idea of the extent of the damage; it may be more useful to mount the filesystem read-only, extract as many files as you can successfully extract, and then rebuild the filesystem from backups. If you allow e2fsck to make repairs on a badly damaged filesystem, it may end up being impossible to determine the root cause of the problem because e2fsck may preen the evidence (ie., system log file) from the system.

    If an IDE disk has a bad sector, the drive can remap the sector to a different place on the disk; because of this, a check for bad blocks may only really be necessary the first time the filesystem is created. For disks which do not do this remapping, it is always necessary to keep a file with all of the known bad blocks, so that if the disk must be reformatted, those blocks are not used again. Once a block is bad, it should never be trusted again. The badblocks program can check the disk blocks, and when a filesystem is created with mke2fs, the bad block list can be used to make sure those blocks will not be allocated in the new filesystem.

  5. Finally, use jpico on /etc/fstab to add the following line:
    /dev/sda6swapswapdefaults00
    After rebooting, the system will initialize /dev/sda6 as the swap partition and use it as needed. Since the contents of the swap partition are not retained from session to session, multiple Linux systems can share the same swap partition.
    If you set a swap partition at home, use must also execute the command "mkswap /dev/hdXn" ("mkswap /dev/sdXn"), where "hdXn" ("sdXn") is the swap partition you set up using fdisk.


©2005, Kenneth R. Koehler. All Rights Reserved. This document may be freely reproduced provided that this copyright notice is included.

Please send comments or suggestions to the author.