Notes for Week 2

If you did not save your configuration last week, you must repeat steps 5, 6 and 7 from week 1.

Our task for this week is to install the Linux system from your DVD-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/sda3Windows Vista
    /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 DVD-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. +6144M
      This specifies that the new partition is to be 6 GB (6144 MB) in length.
    6. n
    7. (Enter)
    8. +4096M
      This specifies that sda12 (sda14) will be 4 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 5 GB of free space on your hard drive: the root partition will need at least 4608 MB, and you should have at least 512 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: 1024 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), +4608M, n, p, 3, (Enter), +512M, 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
    mke2fs /dev/sda11 && mke2fs /dev/sda12
    NIGHT students: enter the command
    mke2fs /dev/sda13 && mke2fs /dev/sda14
    The command "mke2fs" creates the directory structure for the root directory tree as an Ext2 filesystem. The "&&" tells bash to execute the second command only if the first ran successfully.
    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.
  3. DAY students: enter the command
    mount -t ext2 /dev/sda11 /mnt
    NIGHT students: enter the command
    mount -t ext2 /dev/sda13 /mnt
    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.
  4. cd /mnt && tar -xjpf /sources/rootfs.tar.bz2
    The tar command extracts a complete root filesystem into the newly formatted partition. We will learn more about tar in week 8.

    The /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.

  5. cd /mnt/etc/ && jpico fstab

    DAY students: add the lines

    /dev/sda11     /          ext2          ro           1 1
    /dev/sda12     /var       ext2          defaults     0 0
    /dev/sda6      swap       swap          pri=1        0 0
    
    NIGHT students: add the lines
    /dev/sda13     /          ext2          ro           1 1
    /dev/sda14     /var       ext2          defaults     0 0
    /dev/sda6      swap       swap          pri=1        0 0
    
    ALL students:

    • change hdc to hda for /media/cdrom
    • change sda1 to sdb1 for /media/usbstg
    • remove the line for fd0

    Enter Ctrl-x to save the file (write over the existing file once you make sure you entered the information correctly!).

    cd && cp /mnt/etc/fstab . && umount /mnt

    This step uses the command-line editor jpico to edit the /etc/fstab file which is used to mount /var initially.
  6. DAY students: enter the command
    mount -t ext2 /dev/sda12 /mnt
    NIGHT students: enter the command
    mount -t ext2 /dev/sda14 /mnt
    ALL students:

    cd /var && cp -a * /mnt/

    cd && cp fstab /mnt/etc/ && umount /mnt

    These commands copy your current /var onto the hard drive so that all of your configuration is kept intact. The fstab file which will be available during system operation (from /var/etc) should look just like the one on the root partition.
  7. 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:

    mount -t ext2 /dev/sda5 /mnt && cd /mnt/grub && jpico menu.lst

    DAY students: after ltwo add the lines

    title lthree
    root (hd0,10)
    kernel --no-mem-option /boot/lfskernel-2.6.16.27 root=/dev/sda11
    NIGHT students: after lthree add the lines
    title lfour
    root (hd0,12)
    kernel --no-mem-option /boot/lfskernel-2.6.16.27 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.
    ALL students:

    save the file
    cd && umount /mnt
    exit all programs, including X Windows
    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 in the Master Boot Record using the commands
    grub
    root (hd0,1)
    setup (hd0)
    quit
    This will allow booting of your new Linux partition (assumed to be on hda2 or sda2); 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".
  8. 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.

  9. 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.


©2007, 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.