Guide to Unix/Explanations/Filesystems and Swap

< Guide to Unix < Explanations

A common feature in Unix-like filesystems is that all files appear in one file hierarchy. The filesystem storing the base of the hierarchy is referred to as the root filesystem. /, the directory containing all other directories, is itself is called the root directory. Other filesystems are mounted on directories under / which make these filesystems look like directories in the root filesystem. For example, a CD-ROM containing files might be mounted at /mnt/cdrom. In this example /mnt/cdrom is called a mountpoint. Access to devices is supplied by another filesystem mounted at /dev. Here files representing physical, and virtual devices can be accessed. Filesystems themselves are represented as files in /dev such as /dev/cdrom and can be mounted to directories in the root filesystem such as /mnt/cdrom. Each disk or storage medium may contain one or more filesystems, each of which contains files which can contain data. Some disks are also used for swap, which supplies a temporary storage space for data in memory, when memory is full. Though "swap" resides on a disk, it is not actually a filesystem.

List of Filesystem Types

The choice of Unix-like system influences the choice of filesystem. There are two kinds of filesystems:

Filesystems with full Unix support

Foreign Filesystems

Disk Partitioning

If the disk is not partitioned, it can be used for one filesystem, or entirely as swap space.

Partioning the disk allows for multiple filesystems and swap spaces.

In addition, *BSD has some extra rules:

The chapter Partitioning Disks describes the formats in more detail and how to edit the partition maps.

Mounting Filesystems and Activating Swap

The commands one uses are:

Simple mounting

Suppose /dev/hdb is a device (for example, a CD drive) and /mnt/cdrom is the place to mount. Then the mount command is:

mount /dev/hdb /mnt/cdrom

There are more mount options so look for mount somewhere in Guide to UNIX/Commands.

Mounting with fstab

The /etc/fstab file helps you mount.

  1. Create an /etc/fstab entry for /dev/hdb.
  2. Run this command
mount /dev/hdb

Unmounting

It is called umount, not unmount...

umount /dev/hdb

Mounts by Users

The user mounts are mounting and unmounting of filesystems by users other than the superuser. Unfortunately, the different Unix variants have different approaches to this task. (An alternative is to configure "sudo" to allow users to become root when mounting and unmounting.) Here are some ways that root can permit user mounts:

Union Mounts

Normally, filesystems are mounted on empty directories. If the directory was not empty, its files are hidden until the filesystem is unmounted. The union mount allows these files to show through. Each existing file from the directory only shows if there is no file in the same place on the mounted filesystem. All new files are made on the mounted filesystem. Note that union mounts are strange and might not work well on your system.

Some operating systems provide a mount(8) option -o union for this. In this example, a non-partitioned SCSI disk sd0 will be mounted on /etc (which is not normally a mount point). Any existing /etc files that we have that are not also on sd0 will still be available.

# mount -o union /dev/sd0c /etc

Disk Images

A filesystem can be stored on a file on another filesystem. Such files are called disk images and they have several applications:

This guide only describes raw disk images, which contain only the filesystem. There are other formats (such as the Mac OS X NDIF format) but these must be converted or handled specially.

Mounting Disk Images on NetBSD and OpenBSD

Normally, NetBSD and OpenBSD only allow mounting of filesystems stored on devices. Thus five steps are involved:

  1. Associate the disk image with a svnd device.
  2. Mount the svnd device.
  3. Use the disk image.
  4. Unmount the svnd device.
  5. Disassociate the disk image from the device.

Association and dissociation are handled by the vnconfig(8) command. In this example, /the/disk/image will be associated with /dev/svnd0c. This particular disk image has a disklabel with a filesystem on partition 'a', which we mount on /mnt. If there was no disklabel, we would instead mount partition 'c'.

# vnconfig -c /dev/svnd0c /the/disk/image
# mount /dev/svnd0a /mnt
...
# umount /dev/svnd0a
# vnconfig -u /dev/svnd0c

Mounting Disk Images on FreeBSD 5

In FreeBSD 5, one uses mdconfig(8) with a vnode backed storage.

Mounting Disk Images on Linux

The mount(8) option -o loop can be used to mount disk images instead of devices.

# mount -o loop /the/disk/image /mnt
...
# umount /mnt

Some Linux kernels can mount a cloop (compressed loop), which is a compressed disk image. Such an image must be mounted read-only. One first prepares the image and then compresses it how? Then one mounts it how? The Knoppix livecd uses a cloop to fit more programs to the CD.

Creating Disk Images

Creating a new, empty disk image involves two steps before you mount it and start using it:

  1. Use dd(1) to make a new file of zeros with the correct size.
  2. Format the disk image with partitions and filesystems.

Here is an example. We use dd and an unlimited supply of zeros, /dev/zero, to create a 1440 kilobyte disk image (1440 blocks of 1024 bytes). Then we use mkfs.ext3(8) to create an unpartitioned ext3 filesystem. Note that mkfs.ext3 does no mounting, so it cares not if it formats a device or a disk image. (FIXME is this correct?)

$ dd bs=1024 count=1440 if=/dev/zero of=example
$ mkfs.ext3 example

The *BSD newfs(8) and newfs_msdos(8) utilities insist on formatting devices only. This is solved with vnconfig(8). If we wanted to partition the disk image, we would also have made a disklabel. One can ignore the imaginary disk geometry.

$ dd bs=1024 count=1440 if=/dev/zero of=example
$ sudo vnconfig -c /dev/svnd0c example
$ sudo newfs /dev/rsvnd0c
...
$ sudo vnconfig -u /dev/svnd0c

In fact, on *BSD it is a good idea to always have a disklabel, especially if when creating an ffs filesystem, so that the filesystem will be correctly listed in the label, and the letter will be different from 'c'.

$ dd bs=1024 count=1440 if=/dev/zero of=example
$ sudo vnconfig -c /dev/svnd0c example
$ sudo disklabel -E svnd0

Initial label editor (enter '?' for help at any prompt)
> a
partition: [a]
offset: [0]
size: [2880]
FS type: [4.2BSD]
> p
device: /dev/rsvnd0c
type: SCSI
disk: vnd device
label: fictitious
bytes/sector: 512
sectors/track: 100
tracks/cylinder: 1
sectors/cylinder: 100
cylinders: 28
total sectors: 2880
free sectors: 0
rpm: 3600

16 partitions:
#             size        offset  fstype [fsize bsize  cpg]
  a:          2880             0  4.2BSD   2048 16384   32 # Cyl     0 -    28*
  c:          2880             0  unused      0     0      # Cyl     0 -    28*
> q
Write new label?: [y]
$ sudo newfs /dev/rsvnd0a
...
$ sudo vnconfig -u /dev/svnd0c

Encrypted Filesystems

Encrypted Filesystems on OpenBSD

Because OpenBSD has integrated crypto, one would expect that there is a way to have an encrypted filesystem. OpenBSD only provides Blowfish encryption for disk images. This is possible with the -k flag to vnconfig(8), used with -c.

In this example, we will mount the encrypted disk image called /secrets, which contains a single partion letter 'a':

# vnconfig -ck /dev/svnd0c /secrets
Encryption key:
# mount /dev/svnd0a
...
# umount /dev/svnd0a
# vnconfig -u /dev/svnd0c

Note that if you entered the wrong encryption key, vnconfig will still succeed. The mount will fail because the disklabel and filesystem header are not readable. In this case, you must vnconfig -u and then vnconfig -ck with the correct key. Make sure to use the correct key when initially formatting a new disk image.

To have an entire encrypted disk partition, create a normal unencrypted disk image, mount it, then fill the partition with one large file to serve as the disk image.

Encrypted Filesystems on Linux

Some Linux kernel configurations let you use losetup to encrypt disk images. Think of lo(opback) setup, not of lose tup.

References

This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.