Peter Robins, his website

Partitions and filesystems

In order to have 2 or more operating systems on one hard disk, you divide the disk into different sections, which in the Linux world are called partitions; Windows sits in one partition and Linux in another, and the two are entirely separate. Each partition contains a filesystem formated in a particular way: DOS systems and the early MS Windows systems use FAT; Windows NT, 2000 and XP use NTFS; the commonest Linux format is Ext2, though there are various other journaling systems, Ext3 being Ext2 with a journal (journals are logs used for automatically recovering the status in the event of power failure or other outage). Windows cannot normally read Linux partitions, but Linux can normally read Windows partitions without problem.

So, if you want to install a Linux-based system on your hard disk, you first partition the disk using a partitioning program; distros normally provide this. If there is already a Windows system on the disk that you want to keep, the basic steps are: 1) resize the existing Windows partition (FAT or NTFS) to a smaller size (the program does this without losing any data); 2) create one of more extra partitions for Linux; 3) load your Linux system into this new partition. Windows doesn’t know about the Linux partition, so if you leave Windows in command of booting, you will never be able to load your Linux system. So, you have to write a boot loader into the boot sector on the disk. You do this by setting up the lilo/grub configuration file, and running lilo/grub to write this configuration to the boot sector, which is also known as the master boot record (MBR). Then, the next time you boot from the hard disk, the boot loader will determine which partition to use based on this configuration; for example, you might tell it to always load Linux, or you might tell it to always prompt so you can pick whether you want Windows or Linux this time.

In Linux, devices are identified by nodes in the /dev directory, ide ones being /dev/hda, /dev/hdb etc. The partitions within each device are identified by a further number, e.g /dev/hda1 or /dev/hdb5. The files in the filesystem are in a directory/folder hierarchy. Unix systems (including Linux) use / to indicate the different levels in the hierarchy, so the top level, aka root, is / and the letter I have just written in my home directory might be stored in /home/pr/letter. As mentioned above, the boot loader passes on the root filesystem to the Linux kernel, which makes it available by ‘mounting’ it at the root directory /. So, say you created your /dev/hda1 partition in ext2 format, and it should be used as the root filesystem; then the mount command will be something like ‘mount /dev/hda1 / -t ext2′, i.e. mount partition /dev/hda1, ext2 type, at the top level of the directory hierarchy ‘/’.

How do you access the Windows files on the other partition? You mount it in its own place in the hierarchy, for example ‘mount /dev/hda5 /windows -t vfat’ would mount a vfat filesystem under /windows in the directory hierarchy. Windows uses \ with the drive id, so a letter you had stored in the Windows filesystem in c:\MyDocuments\letter would be accessed from your Linux system mounted at /windows by /windows/MyDocuments/letter. If you mounted the Windows partition at /systems/other/windows, you would find the letter at /systems/other/windows/MyDocuments/letter. (Note that NTFS support in Linux includes only limited support for writing; if you want to write files that can be used in Windows, it’s better to create a separate FAT partition that can be accessed by both Windows and Linux.)

Such mount commands are one-offs. If you want your Windows partition mounted every time you boot, you can set up the mount in the configuration file /etc/fstab - the filesystem table. This will contain lines like
/dev/hda1 / ext3 defaults 0 0
/dev/hda5 /windows vfat defaults 0 0
/dev/hda6 /opt ext2 noauto,user,exec 1 2
which lists 3 partitions, 1 and 5 are always mounted, 6 not, but can be mounted by any user. You can see which partitions are currently mounted with ‘mount -l’, though this includes some extra pseudo-devices mounted by the kernel.

That is the simple setup with just 2 partitions, but you can have as many partitions as you like. For example, in the server world, it’s common to have programs and data in separate partitions: programs only change when a new version is installed and so do not need to be backed up; on the other hand, data files that change frequently need to be backed up regularly (or even mirrored) and it’s easier to do this if they are separated out from the programs. Conversely, you could have 2 different versions of Linux accessing the same data; you might then have 3 partitions for the 2 versions and the data. Both would mount the data in the same place but use whichever system partition as the root filesystem.

A filesystem does not have to be in its own partition. If you have one in a file, you can mount that by using a special pseudo-device called a loop device. For example, if you download a CD-ROM filesystem image in ISO9660 format (a common way of distributing Linux distros), instead of burning this to a physical CD and reading it from the CD drive, you can mount it directly with something like ‘mount download.iso /mnt/cd -t iso9660 -o loop’ where ‘download.iso’ is the name of the file. You can then access it from the mountpoint /mnt/cd exactly as if you had put a physical CD in the CD drive. Alternatively, you can put it in its own hard-disk partition (assuming you have an empty one) by ‘cat download.iso > /dev/hdaxx’ or something similar, and loading from there.

A further twist to this is that, if you have enough RAM, you can have filesystems in RAM too, either as so-called ramdisks (/dev/ram0 etc), which simulate a hard-disk partition, or as filesystems (ramfs, tmpfs - the latter being variable-sized ramfs with swapping). Of course, this is not for permanent data, as everything in RAM is deleted when you switch off or reboot, but it can be used for temporary storage. A common use is the so-called initrd (initial ramdisk) where part of RAM is booted as the root filesystem, performs some initial setup - over a network, for example - and then passes control on to the normal root filesystem. A variation of this in the newest kernels is the initramfs, where you can compile a compressed filesystem into the kernel and use that as the initial root filesystem.

Note that support for the different types of filesystem you want to use must be available in the Linux kernel. If for instance you have an NTFS partition, but NTFS is not available in the kernel you are using, you will not be able to read it.

For beginners, this no doubt seems complicated and it’s much easier to rely on somebody else to do the dirty work. As always with Linux, you have a choice and choosing always involves finding out about the different choices available to you.

For further info, see the manpages for lilo/grub and mount. There are various howtos available too (see Linux Documentation Project), though they tend to be rather out-of-date. See Wikipedia for info on the different filesystems.

December 15, 2005