Administering Filesystems - Ubuntu Linux Toolbox: 1000+ Commands for Power Users (2013)

Ubuntu Linux Toolbox: 1000+ Commands for Power Users (2013)

Chapter 7

Administering Filesystems

IN THIS CHAPTER

· Understanding Linux filesystem types

· Partitioning disks with fdisk and parted

· Working with labels with e2label and findfs

· Creating filesystems with mkfs

· Viewing filesystem info with tune2fs/dumpe2fs

· Using swap areas with mkswap, swapon, and swapoff

· Using fstab, mount, and umount to mount and unmount filesystems

· Checking filesystems with badblocks and fsck

· Creating encrypted filesystems

· Viewing RAID information with mdadm

· Checking disk space with du and df

· Logical Volume Manager (LVM)

Filesystems provide the structures in which files, directories, devices, and other elements of the system are accessed from Linux. Linux supports many different types of filesystems (ext4, VFAT, ISO9660, NTFS, and so on) as well as many different types of media on which filesystems can exist (hard disks, CDs, USB flash drives, Zip drives, network drives, and so on).

Creating and managing disk partitions and the filesystems on those partitions are among the most critical jobs in administering a Linux system. That’s because if you mess up your filesystem, you might very well lose the critical data stored on your computer’s hard disk or removable media.

This chapter contains commands for partitioning storage media, creating filesystems, mounting and unmounting partitions, and checking filesystems for errors and disk space.

Understanding Filesystem Basics

Even though many different filesystem types are available in Linux, you will probably only assign a few different filesystem types on any given Linux system. For a basic Linux system, your computer hard disk may contain only three partitions: a swap partition (used to handle the overflow of information in RAM), a boot partition that contains the boot loader and kernel, and a root filesystem partition. The boot and root filesystem partitions are usually the ext4 filesystem type.

Most of the examples in this chapter use ext4 filesystems to illustrate how a filesystem is created and managed. Ext filesystems have traditionally been used as the default filesystem type for Linux distributions. However, there are times when you might want to use other filesystem types. The following is a list of different filesystem types and descriptions of when you might want to use them.

· ext4—Adds performance and reliability features to ext3 filesystem types.

· ext3—Contains journaling features for safer data and fast reboots after unintended shutdowns.

· ext2—Predecessor of ext3, but doesn’t contain journaling.

· iso9660—Evolved from the High Sierra filesystem (which was the original standard used on CD-ROM). May contain Rock Ridge extensions to allow iso9660 filesystems to support long filenames and other information (file permissions, ownership, and links).

· Jffs2—Journaling Flash filesystem, version 2 (JFFS2), which is designed for efficient operations on USB flash drives. Successor to JFFS.

· jfs—JFS filesystem, which IBM used for OS/2 Warp. Tuned for large filesystems and high-performance environments.

· msdos—MS-DOS filesystem. Can be used to mount older MS-DOS filesystems, such as those on old floppy disks.

· ntfs—Microsoft New Technology Filesystem (NTFS). Useful when filesystems need to share files with newer Windows systems (as with dual booting or removable drives).

· reiserfs—Journaling filesystem, which used to be used by default on some SUSE, Slackware, and other Linux systems. Reiserfs is not well-supported in Ubuntu.

· squashfs—Compressed, read-only filesystem used on many Linux live CDs.

· swap—Used on swap partitions to hold data temporarily when RAM is not currently available.

· ufs—Popular filesystem on Solaris and SunOS operating systems from Sun Microsystems.

· vfat—Extended FAT (VFAT) filesystem. Useful when filesystems need to share files with older Windows systems (as with dual booting or removable drives).

· xfs—Journaling filesystem for high-performance environments. Can scale up to systems that include multiple terabytes of data that transfer data at multiple gigabytes per second.

Besides the filesystem types listed in the table, there are also what are referred to as network shared filesystems. Locally, a network shared filesystem may be an ext4, ntfs, or other normal filesystem type. However, all or part of those filesystems can be shared with network protocols such as Samba (smbfs or cifs filesystem type), NFS (nfs), and NetWare (ncpfs).

Many available filesystem types are either not useful for creating new filesystems or not fully supported in every version of Linux. For example, filesystem types such as minix (for Minix systems), befs (for BeOS systems), and affs (for Amiga systems) are mostly useful if you need to mount and access very old backup media from those systems. Even popular filesystems may not be fully supported. For example, reiserfs filesystems aren’t fully supported, as of this writing, by the Kubuntu variant of Ubuntu.

Creating and Managing Filesystems

Ubuntu gives you the option of either having the installer create a default partitioning and filesystem scheme or letting you set that all up manually when you first install Linux. The installer lets you choose to erase the entire hard disk, erase only Linux partitions, or use only free disk space to set up the partitions. To take the manual approach instead, you must choose to create a custom layout.

With the manual approach, the disk-partitioning tool lets you divide the hard disk into partitions as you choose. Later, there are a lot of command-line utilities you can use to change and work with your disk partitions and the filesystems created on those partitions.

Partitioning Hard Disks

Historically, PC hard drives have used a 32-bit PC-BIOS partition table with a Master Boot Record (MBR). This limits partition sizes to 2TB and allows only four primary partitions per drive. The use of extended partitions is a way to overcome the four primary partition limit. In order to overcome the 2TB limit, PC-BIOS partition tables are being replaced with GPT (GUID Partition Tables).

The old standard command for working with disk partitions is fdisk. Because fdisk cannot work with GPT partitions, however, it is slowly being deprecated. A more powerful and actively supported tool is the parted command.

To try examples of fdisk and parted in this section, I recommend you get a USB flash drive that you don’t mind erasing. When you insert it, the drive will show up as the next available sd device, such as /dev/sdb or /dev/sdc. The non-destructive fdisk -l command will help you find the right drive, so you don’t risk damaging the main hard disk used on your system.

Note If you prefer to use graphical tools for partitioning, resizing, and otherwise manipulating your hard disk, you can try the gparted partitioning tool. Install the gparted package to get that tool, which is not installed by default.

Changing Disk Partitions with fdisk

The fdisk command is a useful Linux tool for listing and changing disk partitions. Keep in mind that modifying or deleting partitions can cause valuable data to be removed, so be sure of your changes before writing them to disk. To use the fdisk command to list information about the partitions on your hard disk, type the following command as root user:

$ sudo fdisk -l List disk partitions for every disk

Disk /dev/sda: 82.3 GB, 82348277760 bytes

255 heads, 63 sectors/track, 10011 cylinders

Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System

/dev/sda1 * 1 13 104391 83 Linux

/dev/sda2 14 9881 79264710 83 Linux

/dev/sda3 9882 10011 1044225 82 Linux swap

This example is for an 80GB hard disk that is divided into three partitions. The first (/dev/sda1) is a small /boot partition that is configured as a Linux filesystem (Id 83), which is appropriate for any ext filesystem. Note the asterisk (*), indicating that the first partition is bootable. The next partition is assigned to the root filesystem and is also a Linux partition. The final partition is Linux swap.

If multiple disks are present, fdisk -l will list them all unless you indicate the specific disk you want:

$ sudo fdisk -l /dev/sdb List partitions for a specific disk

To work with a specific disk with the fdisk command, simply indicate the disk you want with no other options:

$ sudo fdisk /dev/sda Start interactive fdisk session

Command (m for help): m Type m to list help text as shown

Command action

a toggle a bootable flag

b edit bsd disklabel

c toggle the dos compatibility flag

d delete a partition

l list known partition types

m print this menu

n add a new partition

o create a new empty DOS partition table

p print the partition table

q quit without saving changes

s create a new empty Sun disklabel

t change a partition's system id

u change display/entry units

v verify the partition table

w write table to disk and exit

x extra functionality (experts only)

Command (m for help):

With the prompt displayed, you can use any of the commands shown to work with your hard disk. In particular, you can use p (to print the same listing as fdisk -l), n (to create a new partition), d (to delete an existing partition), l(to list known filesystem types), or t (to change the filesystem type for a partition). The following examples show some of those fdisk commands in action:

Command (m for help): d Ask to delete a partition

Partition number (1-4): 4 Type partition number to delete

...

Command (m for help): n Create a new disk partition

First cylinder (1-4983, default 1): 1 Select start (or Enter)

Last cylinder ... (default 4983): 4983 Select end (or Enter)

...

Command (m for help): a Make a partition bootable

Partition number (1-3): 1 Type bootable partition number

...

Command (m for help): t Select a filesystem type

Partition number (1-3): 3 Select partition to change

Hex code (type L to list codes): 82 Assign partition as swap

Unless you tell it otherwise, fdisk assumes the new partition is a Linux partition (83). You could have typed L to see the same listing of filesystem types and hex codes produced from the l command. As noted, 82 can assign the partition as swap. Other Linux partitions that may interest you include Linux extended (85), Linux LVM (8e), Linux software raid (fd), and EFI/GTP (ee).

For Windows partitions, you can assign a partition as HPFS/NTFS (7), Windows 95 FAT32 (b), FAT 16 (6), or Windows 95 FAT32 LBA (c). Other Unix-type filesystems include Minix (be or bf), BSD/OS (e4), FreeBSD (ee), OpenBSD (ef), NeXTSTEP (f0), Darwin UFS (f1), and NetBSD (f4). Any of these filesystem types might be useful if you have old backup media from those filesystems you want to restore.

So far, you have not made any permanent changes to your partition table. If you are now very sure that your new settings are correct, type w to write those changes to the partition table. To abandon your changes (or quit after writing your changes), type q to quit your fdisk session.

Copying Partition Tables with sfdisk

To back up or replicate a disk’s partition table, use sfdisk:

$ sudo sfdisk -d /dev/sda > sda-table Back up partition table to file

$ sudo sfdisk /dev/sda < sda-table Restore partition table from file

$ sudo sfdisk -d /dev/sda | sfdisk /dev/sdb Copy part table from a to b

Changing Disk Partitions with parted

As with fdisk, parted can be used to list or change disk partitions. However, parted has a few other useful features as well. Here’s how to list partitions for a given disk, /dev/sda, with parted:

$ sudo parted /dev/sda print

Model: ATA ST3160815AS (scsi)

Disk /dev/sda: 160GB

Sector size (logical/physical): 512B/512B

Partition Table: msdos

Number Start End Size Type File system Flags

1 1049kB 256MB 255MB primary ext2 boot

2 257MB 160GB 160GB extended

5 257MB 160GB 160GB logical lvm

This listing shows you whether you have a classic msdos disk label (partition table), or a gpt one. In this case, the partition table is msdos.

To run parted interactively, type parted followed by the name of the storage device you want to work with (such as /dev/sda). Or, if you have only one storage device, simply type parted. But, if you have a USB flash drive you don’t mind erasing, I recommend you use that to try parted for the first time. Here, the USB drive is located on /dev/sdc:

$ sudo parted /dev/sdc

GNU Parted 2.3

Using /dev/sdc

Welcome to GNU Parted! Type 'help' to view a list of commands.

(parted)

To use parted interactively, either type whole commands or start with a few letters and use the Tab key to complete the command (as you would in the bash shell). And if you’re really efficient, you can just type enough letters to allow parted to guess your input, as you would with Cisco IOS: p for print, mkl for mklabel, and so on.

Warning Unlike fdisk, parted immediately incorporates changes you make to your partitions, without explicitly writing the changes to disk. So don’t just assume you can back out of any changes by simply quitting parted.

With each command in a parted session, you also have the option to enter the command with all the arguments (for example, mkpart logical ext4 1MB 2GB) or just enter the command (mkpart) and parted will guide you interactively:

Warning The following commands will change the configuration of your disk. That’s why I use a USB flash drive that contains no data that I mind losing. If you make a mistake on your primary drive, you could make the data inaccessible and your system unbootable.

(parted) mkpart Create a new partition

Partition type? [logical]? primary

File system type? [ext2]? ext4

Start? 1MB

End? 2GB

The parted command is not recommended for resizing partitions because it can make the filesystems on those disks unusable. If you do use it to resize a partition, parted will try to resize the filesystem located on that partition as well. Because parted does not handle journaling or other features required by ext3 and ext4 filesystems, the resizing will fail for most useful cases.

Because resizing is an iffy proposition with standard disk partitions, if you suspect you will need to resize a partition at some point, consider using Logical Volume Manager to create and manage logical partitions. LVM is covered later in this chapter.

To resize NTFS partitions, you can use the ntfsresize command. In Ubuntu, that command comes with the ntfsprogs package. That package also comes with commands for creating (mkfs.ntfs), fixing (ntfsfix), and getting information about (ntfsinfo) NTFS partitions.

Working with Filesystem Labels

The term label, in terms of disk partitions, can refer to two different things. A disk label can be used as another name for a partition table, as seen in parted output. A partition labelcan also be the name of an individual partition that contains an ext filesystem.

Labels provide several advantages:

· You can use a label as the permanent name to mount a filesystem. It’s possible that /dev/sda might be detected as /dev/sdb the next time the system reboots. Mounting by label lets you give the partition a unique name.

· If a label is on a removable USB drive, when you plug in that drive, Linux will use the label name as the name of the directory to mount on. For example, if the label is mydocs, the USB drive is mounted on the /media/mydocsdirectory.

To see a partition’s label (if it has one), use the e2label command:

$ sudo e2label /dev/sda2

/home

To set the label on a partition:

$ sudo e2label /dev/sda2 mypartition

Bear in mind that /etc/fstab can be set up to use the partition label to mount the partition, as in the following example. Changing this label may render the system unbootable.

LABEL=/boot /boot ext4 defaults 1 2

To find a partition when you know only the label, type the following:

$ sudo findfs LABEL=mypartition

/dev/sdc2

Formatting a Filesystem

With your disk partitions in place, you can build a filesystem of your choice on each partition. Most Linux systems come with the commands needed to make and check filesystems that are commonly used in Linux. Commands for formatting and checking filesystems are mkfs and fsck, respectively.

The mkfs command serves as the front end for many different commands aimed at formatting particular filesystem types, such as mkfs.ext2, mkfs.ext3, mkfs.ext4, mkfs.cramfs, mkfs.msdos, mkfs.ntfs, and mkfs.vfat. By adding packages that support other filesystems, additional mkfs commands are available to seamlessly work with mkfs. These include mkfs.bfs, mkfs.minix, mkfs.xfs, and mkfs.xiafs. Use each command directly (as in mkfs.vfat /dev/sdb1) or via the mkfs command (as in mkfs -t vfat /dev/sdb1).

Creating a Filesystem on a Disk Partition

Basic software packages you need in Ubuntu to do filesystem creation and checking include util-linux (includes mkfs and other general utilities) and e2fsprogs (ext2/ext3/ext4-specific tools). Specific mkfs commands for different filesystem types are included in ntfsprogs (ntfs), dosfstools (msdos and vfat), xfsprogs (xfs), jfsutils (jfs), mtd-utils (jffs and jffs2), and reiserfs-utils (reiserfs). The basic tools get installed with Ubuntu.

Here are examples of the mkfs command to create filesystems (be sure to add the -t option first):

$ sudo mkfs -t ext4 /dev/sdb1 Create ext4 file system on sba1

$ sudo mkfs -t ext4 -v -c /dev/sdb1 More verbose/scan for bad blocks

$ sudo mkfs.ext4 -c /dev/sdb1 Same result as previous command

If you would like to add a partition label to the new partition when the filesystem is created, use the -L option:

$ sudo mkfs.ext4 -c -L mypartition /dev/sdb1 Add mypartition label

Creating a Virtual Filesystem

If you want to try out different filesystem types or simply make a filesystem that is more portable (in other words, not tied to a physical disk), you can create a virtual filesystem. A virtual filesystem is one that sits within a file on an existing filesystem. You can format it as any filesystem type you like, move it around, and use it from different computers.

Virtual filesystems are useful for such things as creating live CDs or running dedicated virtual operating systems. In the example that follows, you create a blank 1GB disk image file, format it as a filesystem, and then mount it to access data on the filesystem:

$ dd if=/dev/zero of=mydisk count=2048000 Create zero-filled 1GB file

$ du -sh mydisk Check virtual file size

1001M mydisk

$ mkfs -t ext4 mydisk Create file system on mydisk

mydisk is not a block special device

Continue (y/n): y

$ sudo mkdir /mnt/image Create a mount point

$ sudo mount -o loop mydisk /mnt/image Mount mydisk on /mnt/image

In this procedure, the dd command creates an empty disk image file of 2048000 blocks (about 1GB). The mkfs command can create any filesystem type you choose (ext4 is done here). Because the file is not a block special device, as is the case when formatting disk partitions, mkfs will warn you before starting to make the filesystem. The only other trick, after creating the mount point, is to indicate that you are mounting the file (mydisk) as a loop device (-o loop). Note that the mount command is the only command shown in the preceding code that requires root privilege.

When the virtual filesystem is mounted, in this example under /mnt/image, you can access it as you would any filesystem. When you are done with the filesystem, leave it and unmount it:

$ sudo cd /mnt/image Change to the mount point

$ sudo mkdir test Create a directory on the file system

$ sudo cp /etc/hosts . Copy a file to the file system

$ cd Leave the file system

$ sudo umount /mnt/image Unmount the file system

With the virtual filesystem unmounted, you could move it to another system or burn it to a CD to use a filesystem in another location. If you don’t want the filesystem any more, simply delete the file.

Viewing and Changing Filesystem Attributes

Using the tune2fs or dumpe2fs commands, you can view attributes of ext2, ext3, and ext4 filesystems. The tune2fs command can also be used to change filesystem attributes. Use the swapfs command to create a swap partition. Here are examples (both commands produce the same output):

$ sudo tune2fs -l /dev/sda1 | less View tunable file system attributes

$ sudo dumpe2fs -h /dev/sda1 Same as tune2fs output

dumpe2fs 1.42 (29-Nov-2011)

Filesystem volume name: <none>

Last mounted on: <not available>

Filesystem UUID: f5f261d3-3879-41d6-8245-f2153b003204

Filesystem magic number: 0xEF53

Filesystem revision #: 1 (dynamic)

Filesystem features: filetype sparse_super

Default mount options: (none)

Filesystem state: clean

Errors 'margin-top:7.5pt;margin-right:12.5pt;margin-bottom: 4.5pt;margin-left:17.0pt;text-align:justify;line-height:normal'>Filesystem OS type: Linux

Inode count: 7914368

Block count: 7907988

Reserved block count: 395399

Free blocks: 5916863

Free inodes: 7752077

First block: 0

Block size: 4096

Fragment size: 4096

Reserved GDT blocks: 1022

Blocks per group: 32768

Fragments per group: 32768

Inodes per group: 32704

Inode blocks per group: 1022

Filesystem created: Fri Jun 15 12:13:17 2007

Last mount time: Sat Mar 12:13:14 2013

Last write time: Sat Mar 12:13:14 2013

Mount count: 2

Maximum mount count: 29

The output shows a lot of information about the filesystem. For example, if you have a filesystem that needs to create many small files (such as a news server), you can check that you don’t run out of inodes. Setting the Maximum mount count ensures that the filesystem is checked for errors after it has been mounted the selected number of times. You can also find dates and times for when a filesystem was created, last mounted, and last written to.

To change settings on an existing ext2, ext3, or ext4 filesystem, you can use the tune2fs command. The following command changes the number of mounts before a forced filesystem check:

$ sudo tune2fs -c 31 /dev/sda1 Sets # of mounts before forced check

tune2fs 1.42 (29-Nov-2011)

Setting maximal mount count to 31

If you’d like to switch to forced filesystem checks based on time interval rather than number of mounts, disable mount-count checking by setting it to negative 1 (-1):

$ sudo tune2fs -c -1 /dev/sda1

tune2fs 1.42 (29-Nov-2011)

Setting maximal mount count to -1

Use the -i option to enable time-dependent checking. Here are some examples:

$ sudo tune2fs -i 10 /dev/sda1 Check after 10 days

$ sudo tune2fs -i 1d /dev/sda1 Check after 1 day

$ sudo tune2fs -i 3w /dev/sda1 Check after 3 weeks

$ sudo tune2fs -i 6m /dev/sda1 Check after 6 months

$ sudo tune2fs -i 0 /dev/sda1 Disable time-dependent checking

Be sure you always have either mount-count or time-dependent checking turned on.

Use the -j option to turn an ext2 filesystem into ext3 (by adding a journal):

$ sudo tune2fs -j /dev/sda1 Journaling changes ext2 to ext3

Creating and Using Swap Partitions

Swap partitions are needed in Linux systems to hold data that overflows from your system’s RAM. If you didn’t create a swap partition when you installed Linux, you can create it later using the mkswap command. You can create your swap partition either on a regular disk partition or in a file formatted as a swap partition. Here are some examples:

$ sudo mkswap /dev/sda6 Format sda6 as a swap partition

Setting up swapspace version 1, size = 205594 kB

To check your swap area for bad blocks, use the -c option to mkswap:

$ sudo mkswap -c /dev/sda1

If you don’t have a spare partition, you can create a swap area within a file:

$ sudo dd if=/dev/zero of=/mnt/swapfile count=65536

65536+0 records in

65536+0 records out

33554432 bytes (34 MB) copied, 1.56578 s, 21.4 MB/s

$ sudo chmod 600 /mnt/swapfile

$ sudo mkswap /mnt/swapfile

Setting up swapspace version 1, size = 67104 kB

The dd command in the preceding code creates a 32MB file named swapfile. The chmod command locks down the permissions on the file to avoid getting a warning from the swapon command down the road. The mkswap command formats the /mnt/swapfile file to be a swap partition.

After you have created a swap partition or swap file, you need to tell the system to use the swap area you made using the swapon command. This is similar to what happens at boot time. Consider the following examples:

$ sudo swapon /dev/sda1 Turn swap on for /dev/sda1 partition

$ sudo swapon -v /dev/sda1 More verbosity as swap is turned on

swapon on /dev/sda1

$ sudo swapon -v /mnt/swapfile Turn swap on for /mnt/swapfile

swapon on /mnt/swapfile

You can also use the swapon command to see a list of your swap files and partitions:

$ swapon -s View all swap files and partitions that are on

Filename Type Size Used Priority

/dev/sda5 partition 1020088 142764 -1

/mnt/swapfile file 65528 0 -6

Before you turn off a swap area, make sure no space is being used on it (look for a “0” under the Used column). Then, to turn off a swap area, you can use the swapoff command:

$ sudo swapoff -v /mnt/swapfile

swapoff on /mnt/swapfile

Swap areas are prioritized. The kernel will swap first to areas of high priorities and then go down the list. Areas of the same priority get striped between. You can specify the priority of your swap area as you enable it using the -poption:

$ sudo swapon -v -p 1 /dev/sda1 Assign top swap priority to sda1

Mounting and Unmounting Filesystems

Before you can use a regular, non-swap filesystem, you need to attach it to a directory in your computer’s filesystem tree by mounting it. Your root filesystem (/) and other filesystems you use on an ongoing basis are typically mounted automatically based on entries in your /etc/fstab file. Other filesystems can be mounted manually as they are needed using the mount command.

Mounting Filesystems from the fstab File

When you first install Linux, the /etc/fstab file is usually set up automatically to contain information about your root filesystems and other filesystems. Those filesystems can then be set to mount at boot time or be ready to mount manually (with mount points and other options ready to use when a manual mount is done).

Here is an example of a /etc/fstab file:

proc /proc proc nodev,noexec,nosuid 0 0

/dev/mapper/abc-root / ext4 errors=remount-ro 0 1

LABEL=/boot /boot ext2 defaults 0 2

/dev/mapper/abc-swap swap swap defaults 0 0

/dev/sdb1 /mnt/windows vfat noauto 0 0

Note For clarity, the UUID listing for the /boot filesystem was removed in the preceding example. For the /boot filesystem, you’ll normally see an entry such as UUID=da2dbc48-862e-4fbe-9529-a88b57b15bac instead of the UUID. Either UUID or LABEL can be used to provide a way of uniquely identifying a disk partition.

All the filesystems are mounted automatically, except for /dev/sdb1 (as indicated by the noauto option). The root (/) and swap disk partitions are configured as logical volume management (LVM) volumes. LVM volumes can make it easier to grow and shrink filesystems while still retaining the same device names. The /dev/sdb1 disk partition was added manually in this example to mount the Windows partition located on the second hard disk device.

The /etc/fstab file no longer typically holds information about removable media. That’s because the Hardware Abstraction Layer (HAL) facility automatically detects removable media and mounts those media in appropriate mount points in the /media directory (based on such things as volume ID on the media).

The following list describes each field in the /etc/fstab file.

1The device name representing the filesystem. Originally, this contained the device name of the partition to mount (such as /dev/sda1). It can now also contain a LABEL, a universally unique identifier (UUID), or a remote filesystem (NFS or CIFS) instead of a device name.

2The mount point in the filesystem. The filesystem contains all data from the mount point down the directory tree structure, unless another filesystem is mounted at some point beneath it.

3—The filesystem type. See the section “Understanding Filesystem Basics “ for a list of many common filesystem types.

4—The mount command options. Examples of mount options include noauto (to prevent the filesystem from mounting at boot time) and ro (to mount the filesystem read-only). To let any user mount a filesystem, you could add the user or owner option to this field. Commas must separate options. See the mount command manual page (under the -o option) for information on other supported options.

5—Dump filesystem? This field is only significant if you run backups with dump (which is rare). A number 1 signifies that the filesystem needs to be dumped. A zero means that it doesn’t.

6—Filesystem check? The number in this field indicates whether or not the filesystem needs to be checked with fsck. A zero indicates that the filesystem should not be checked. A number 1 means that the filesystem needs to be checked first (this is used for the root filesystem). A number 2 assumes that the filesystem can be checked at any point after the root filesystem is checked.

You can create your own entries for any hard disk or removable media partitions you want in the /etc/fstab file. Remote filesystems (NFS, Samba, and others) can also contain entries in the /etc/fstab file to automatically mount those filesystems at boot time or later by hand.

Mounting Filesystems with the mount Command

The mount command is used to view mounted filesystems, as well as mount any local (hard disk, USB drive, CD, DVD, and so on) or remote (NFS, Samba, and so on) filesystems immediately. Here is an example of the mountcommand for listing mounted filesystems:

$ mount List mounted remote and local file systems

proc on /proc type proc (rw,noexec,nosuid,nodev)

/dev/mapper/abc-root on / type ext4 (rw,errors=remount-ro)

/dev/sda1 on /boot type ext2 (rw)

192.1.0.9:/volume1/books on /mnt/books type nfs (rw,addr=192.1.1.9)

/dev/sdb1 on /media/myusb type ext4 (rw,nosuid,nodev,uhelper=udisks)

Use the -t option to list only mounts of a specific filesystem type:

$ mount -t ext4 List mounted ext4 file systems

/dev/mapper/abc-root on / type ext4 (rw,errors=remount-ro)

/dev/sdb1 on

/media/myusb type ext4 (rw,nosuid,nodev,uhelper=udisks)

To display partition labels with mount information, use the -l option:

$ mount -t ext4 -l List mounted ext4 file systems and labels

/dev/sda7 on / type ext4 (rw) [/123]

/dev/sda6 on /mnt/debian type ext4 (rw) [/mnt/debian]

/dev/sda3 on /mnt/slackware type ext4 (rw) [/mnt/slackware]

Here is a simple mount command to mount the /dev/sdb1 device on an existing directory named /mnt/mymount:

$ sudo mount /dev/sdb1 /mnt/mymount/ Mount a local file system

$ sudo mount -v /dev/sdb1 /mnt/mymount/ Mount /dev/sdb1 more verbose

mount: you didn't specify a filesystem type for /dev/sdb1

I will try type ext4

/dev/sdb1 on /mnt/mymount type ext4 (rw)

In the preceding examples, the mount command will either look for an entry for /dev/sdb1 in the /etc/fstab file or try to guess the type of filesystem.

Use -t to explicitly indicate the type of filesystem to mount:

$ sudo mount -v -t ext4 /dev/sdb1 /mnt/mymount/ Mount an ext4 file system

/dev/sdb1 on /mnt/mymount type ext4 (rw)

You can also display the label/name of the partition that is mounted:

$ sudo mount -vl -t ext4 /dev/sdb1 /mnt/mymount/ Mount and show label

If you’re mounting something that is listed in your fstab file already, you need to specify only one item: mount point or device. For example, with the following fstab entry:

/dev/sdb1 /mnt/mymount ext4 defaults 1 2

you can do either of the following to mount the filesystem:

$ sudo mount -v /dev/sdb1 Mount file system with device name only

/dev/sdb1 on /mnt/mymount type ext4 (rw)

$ sudo mount -v /mnt/mymount/ Mount file system with mount point only

/dev/sdb1 on /mnt/mymount type ext4 (rw)

You can specify mount options by adding -o and a comma-separated list of options. They are the same options you can add to field 4 of the /etc/fstab file. By default, partitions are mounted with read/write access. You can explicitly indicate to mount a filesystem as read/write (rw) or read-only (ro):

$ sudo mount -v -t ext4 -o rw /dev/sdb1 /mnt/mymount/ Mount read/write

/dev/sdb1 on /mnt/mymount type ext4 (rw)

$ sudo mount -v -t ext4 -o ro /dev/sdb1 /mnt/mymount/ Mount read-only

/dev/sdb1 on /mnt/mymount type ext4 (ro)

A few other useful mount options you can use include:

· noatime—Does not update the access time on files. Good on filesystems with a lot of I/O, such as mail spools and logs.

· noexec—Prevents execution of binaries located on this filesystem. Can be used to increase security, for example for /tmp in environments with untrusted users.

· remount—Change options on a mounted filesystem. With remount, you can unmount the filesystem and remount it with the new options in a single command. In this example, we change a previous read/write mount to read-only:

· $ sudo mount -v -o remount,ro /dev/sdb1

/dev/sdb1 on /mnt/mymount type ext4 (ro)

· --bind—Mount an existing filesystem to another location in the tree. Assuming /dev/sdb1 is already mounted on /mnt/mymount, type the following:

· $ sudo mount --bind -v /mnt/mymount/ /tmp/mydir/

/mnt/mymount on /tmp/mydir type none (rw,bind)

Now the same filesystem is accessible from two locations. The new mount point has the same mount options as the original.

· --move—Move a filesystem from one mount point to another. Assuming /dev/sdb1 is already mounted on /mnt/mymount, this moves the filesystem to /tmp/mydir:

· $ sudo mount -v --move /mnt/mymount/ /tmp/mydir/

/mnt/mymount on /tmp/mydir type none (rw)

Just as you can swap to a file, you can create a filesystem in a file and then mount it in what is called a loopback mount. Creating and mounting such a file is described in the “Creating a Virtual Filesystem” section earlier in this chapter. A common situation where you might want to mount a file in loopback is after downloading a Linux install CD or live CD. By mounting that CD image in loopback, you can view its contents or copy files from that image to your hard disk.

In the following example, the mount command is allowed to automatically pick an existing loopback device when mounting a CD image file (filesystem type iso9660). The command output shows /dev/loop0 was selected:

$ sudo mount -v -t iso9660 -o loop /tmp/myimage.iso /mnt/mymount/

mount: going to use the loop device /dev/loop0

/tmp/myimage.iso on /mnt/mymount type ext4 (rw,loop=/dev/loop0)

In the following example, I downloaded a Linux USB flash drive boot image called diskboot.img to /tmp. Here is an example of how to mount the boot image:

$ sudo mount -v -o loop /tmp/diskboot.img /mnt/mymount

mount: going to use the loop device /dev/loop0

mount: you didn't specify a filesystem type for /dev/loop0

I will try type vfat

/tmp/diskboot.img on /mnt/mymount type vfat (rw,loop=/dev/loop0)

To see the status of the loopback devices, use the losetup command:

$ sudo losetup /dev/loop0 List mounted loopback devices

/dev/loop0: [0807]:1009045 (/tmp/diskboot.img)

If a loopback mount gets stuck and you have problems during unmount, try detaching it as follows:

$ sudo losetup -d /dev/loop1 Force unmount mounted loopback device

Note The mount command can also be used to attach to NFS, or Samba/Windows CIFS shares. See Chapter 12 for information on mounting those remote filesystem types.

Unmounting Filesystems with umount

To unmount a filesystem, use the umount command. You can umount the filesystem using the device name or the mount point. You’re better off unmounting with the mount point, to avoid the confusion when using bind mounts (one device, multiple mount points). Here is an example of each, with verbosity on:

$ sudo umount -v /dev/sdb1 Unmount by device name

/dev/sdb1 umounted

$ sudo umount -v /mnt/mymount/ Unmount by mount point

/tmp/diskboot.img umounted

If the device is busy, the unmount will fail. A common reason for an unmount to fail is that you have a shell open with the current directory of a directory inside the mount:

$ sudo umount -v /mnt/mymount/

umount: /mnt/mymount: device is busy

umount: /mnt/mymount: device is busy

Sometimes, it’s not obvious what makes the device busy. You can use lsof to list open files, and then search that list for the mount point that interests you:

$ sudo lsof | grep mymount Find open files on mymount partition

bash 11224 chris cwd DIR 8,17 4096 2 /mnt/mymount

You can see that a bash process run by chris with a PID of 9341 is preventing the mymount partition from being unmounted.

Another option when a filesystem is busy is to perform a lazy unmount:

$ sudo umount -vl /mnt/mymount/ Perform a lazy unmount

A lazy unmount unmounts the filesystem from the tree now, but waits for the device to no longer be busy before cleaning up everything. Unmounts of removable media can also be done with eject. This unmounts a CD and ejects the CD from the drive:

$ sudo eject /dev/cdrom Unmount and eject a CD

Checking Filesystems

In Linux, instead of having the scandisk utility you have in Windows, you can scan a physical device for bad blocks at a physical level with the badblocks command and scan a filesystem for errors at the logical level with the fsckcommand. Here’s how to scan for bad blocks:

$ sudo badblocks /dev/sdb1 Physically scan disk for bad blocks

$ sudo badblocks -v /dev/sdb1 Add verbosity to hard disk scan

Checking blocks 0 to 200781

Checking for bad blocks (read-only test): done

Pass completed, 0 bad blocks found.

By default, badblock does a safe read-only test of the blocks. You can also perform a non-destructive read/write test. This is the slowest test, but the best one you can perform without destroying the data on the device. Add -s to see the ongoing progress:

$ sudo badblocks -vsn /dev/sdb1 Check bad blocks, non-destructive

Checking for bad blocks in non-destructive read-write mode

From block 0 to 200781

Testing with random pattern: Pass completed, 0 bad blocks found.

To test a disk that has no data on it that you need to keep, the following command performs a faster, destructive read-write test:

Warning This will erase all the data on the partition.

$ sudo badblocks -vsw /dev/sda1 Destructive check for bad blocks

Checking for bad blocks in read-write mode

From block 0 to 200781

Testing with pattern 0xaa: done

Reading and comparing: done

...

Pass completed, 0 bad blocks found.

You can perform multiple badblocks passes; for example, this command line can be used to burn in a drive and screen for hard drive infant mortality:

$ sudo badblocks -vswp 2 /dev/sda1

Like the mkfs command, the fsck command is just a front end to filesystem-specific utilities. You can check an ext filesystem by simply adding the device name of the disk partition you want to check to the fsck command:

$ sudo fsck /dev/sdb1

fsck from util-linux 2.20.1

mypart was not cleanly unmounted, check forced.

fsck 1.42 (29-Nov-2011)

e2fsck 1.42 (29-Nov-2011)

Pass 1: Checking inodes, blocks, and sizes

Pass 2: Checking directory structure

Pass 3: Checking directory connectivity

Pass 4: Checking reference counts

Pass 5: Checking group summary information

mypart: 11/50200 files (9.1% non-contiguous), 12002/200780 blocks

You can add other options to fsck, such as -T (to not display the useless fsck version number) and -V (to be more verbose about what fsck actually does):

$ sudo fsck -TV /dev/sdb1 Check file system, verbose no version

[/sbin/fsck.ext4 (1) -- /dev/sdb1] fsck.ext4 /dev/sdb1

e2fsck 1.42 (29-Nov-2011)

mypart: clean, 11/50200 files, 12002/200780 blocks

For any problem that fsck encounters, it will ask you if you want to repair it:

$ sudo fsck -TV /dev/sdb1 Prompting to correct problems encountered

[/sbin/fsck.ext4 (1) -- /mnt/mymount] fsck.ext4 /dev/sda1

e2fsck 1.39 (29-Nov-2011)

Couldn't find ext2 superblock, trying backup blocks...

Resize inode not valid. Recreate<y>? y

Unless you have a very in-depth knowledge of filesystems, you’re better off answering yes. This can be done automatically with the -y option:

$ sudo fsck -TVy /dev/sdb1

[/sbin/fsck.ext4 (1) -- /mnt/mymount] fsck.ext4 -y /dev/sdb1

e2fsck 1.42 (29-Nov-2011)

Couldn't find ext2 superblock, trying backup blocks...

Resize inode not valid. Recreate? yes

mypart was not cleanly unmounted, check forced.

Pass 1: Checking inodes, blocks, and sizes

Pass 2: Checking directory structure

Pass 3: Checking directory connectivity

Pass 4: Checking reference counts

Pass 5: Checking group summary information

Free blocks count wrong for group #0 (3552, counted=3553).

Fix? yes

Free blocks count wrong (188777, counted=188778).

Fix? yes

mypart: ***** FILE SYSTEM WAS MODIFIED *****

mypart: 11/50200 files (0.0% non-contiguous), 12002/200780 blocks

Creating Encrypted Filesystems

Using components of the cryptsetup package, you can create encrypted filesystems to protect the data on your hard disks or removable media. A common practice is to encrypt the data on a separate /home partition on your laptop or on a USB drive you carry in your pocket. If those devices were lost or stolen, nobody could access your data without the passphrase.

Encrypting the /home partition on your Ubuntu system just means selecting the right options during the disk partition steps of installing Ubuntu. Encrypting a disk partition later, using the command line, requires several steps, as shown in the list that follows.

To encrypt your USB drive so that only someone who knows the password can access the encrypted data on the drive, you can do the following:

1. Watch the /var/log/messages file using the tail command. Once tail is running, insert your USB drive into your computer and you will see messages related to the USB drive being detected:

$ sudo tail -f /var/log/messages

Apr 11 05:54:09 myubuntu kernel: [4486653.002219] sd 11:0:0:0:

Attached scsi generic sg3 type 0

Apr 11 05:54:09 myubuntu kernel: [4486653.004329] sd 11:0:0:0:

[sdc] 7831552 512-byte logical blocks: (4.00 GB/3.73 GiB)

Apr 11 05:54:09 myubuntu kernel: [4486653.005707] sd 11:0:0:0:

[sdc] Write Protect is off

Apr 11 05:54:09 myubuntu kernel: [4486653.014322] sd 11:0:0:0:

[sdc] Attached SCSI removable disk

2. Note information about the device. In this example, the USB storage device is assigned to the sdc device (/dev/sdc). It is a 4.00 GB device and it is not write protected. It is important to get the device name correct or you could erase your Ubuntu system or other critical data in the next step!

3. If you want to erase all old data on the device, you can use the dd command as follows:

$ sudo dd if=/dev/zero of=/dev/sdc

dd: writing to `/dev/sdc': No space left on device

7831553+0 records in

7831552+0 records out

4009754624 bytes (4.0 GB) copied, 1074.47 s, 3.7 MB/s

4. Partition the USB drive to prepare it for the encrypted filesystem. Because the disk is empty, first simply save a blank partition table to the disk:

$ sudo fdisk /dev/sdc

Command (m for help): w

5. Create a single partition on the USB drive as follows:

$ sudo fdisk /dev/sdc

Command (m for help): n

Partition type:

p primary (0 primary, 0 extended, 4 free)

e extended

Select (default p): p

Partition number (1-4, default 1): 1

First sector (2048-78315, default 2048): <ENTER>

Using default value 2048

Last sector, +sectors or +size{K,M,G} (2048-78315, default 78315):

<ENTER>

Using default value 78315

Command (m for help): w

6. Install encryption software on your Ubuntu system:

$ sudo apt-get install cryptsetup

7. Load the modules needed to encrypt your USB drive:

$ sudo modprobe dm-crypt

$ sudo modprobe aes

$ sudo modprobe sha512

8. Encrypt the USB partition with the cryptsetup command. Type YES when prompted. You must then be sure to remember the passphrase or you will not be able to access the data you put on the drive later:

$ sudo cryptsetup luksFormat /dev/sdc1

WARNING!

========

This will overwrite data on /dev/sdc1 irrevocably.

Are you sure? (Type uppercase yes): YES

Enter LUKS passphrase: *********

Verify passphrase: *********

9. Open the encrypted partition as follows (I named the device mycrypt, but you can use any name you like):

$ sudo cryptsetup luksOpen /dev/sdc1 mycrypt

Enter passphrase for /dev/sdc1: *********

10. Format the partition as you would any disk partition (in this case, an ext4 filesystem type). The device name of the new filesystem partition includes the name you assigned on the luksOpen line in the /dev/mapper directory (/dev/mapper/mycrypt in this example):

$ sudo mkfs -t ext4 /dev/mapper/mycrypt

11. You can mount the filesystem. Then change the ownership to the user you want to own the USB partition (the user named chris and the group named chris, in this case):

$ sudo mkdir /mnt/testing

$ sudo mount /dev/mapper/mycrypt /mnt/testing/

$ sudo chown chris:chris /mnt/testing

12. You can copy some files to the new partition to make sure it’s working. When you are done, unmount the partition. Then you must close the partition using the luksClose option to cryptsetup, along with the name in the /dev/mapper directory that represents the encrypted partition:

$ sudo umount /dev/mapper/mycrypt

$ sudo cryptsetup luksClose \

udisks-luks-uuid-9f8cb640-8a9b-4ae0-ac31-48aff59bf167-uid13597

You can now safely remove the USB drive. The next time you insert the drive in any Linux system that supports this type of encryption, what happens next depends on whether you have a desktop running or not:

· From a desktop, a pop-up window asks for the passphrase. Once you enter it, the unencrypted filesystem is mounted under the /media directory and a file manager window opens for you to access the data on the partition. When you are done using it, right-click the icon representing the partition on your desktop and select Safely Remove. Then you can safely remove your USB drive.

· From a shell console, you are prompted for the passphrase you entered when you created the encrypted partition. At that point, you may need to manually mount the filesystem (named something like /dev/mapper/mycrypt) to use it. Once you are done using the filesystem, unmount it (umount) and run cryptsetup luksClose on the encrypted device, as shown earlier in this procedure. Then you can safely remove the drive.

Checking RAID Disks

Redundant Array of Independent Drives (RAID) disks let you duplicate or distribute data across multiple hard drives. Using RAID can improve reliability and performance of your storage media. The mdadm command, which is part of the mdadm package, can be used to check softraid devices on your computer. Consider this example:

$ sudo apt-get install mdadm

$ sudo mdadm -Q /dev/md1

/dev/md1: 1498.13MiB raid1 2 devices, 0 spares.

Use mdadm --detail for more detail.

/dev/md1: No md super block found, not an md component.

The message on the last line simply means that /dev/md1 is not a member of a RAID array. That is normal because md1 is the array itself. Similarly, if you query a member of a RAID array, your output will look like this:

$ sudo mdadm -Q /dev/sdb3

/dev/sdb3: is not an md array

/dev/sdb3: device 1 in 4 device active raid6 md0.

Use mdadm --examine for more detail.

To obtain more detailed output, add the --detail option:

$ sudo mdadm -Q --detail /dev/md1

/dev/md1:

Version : 00.90.01

Creation Time : Fri Apr 5 16:32:12 2013

Raid Level : raid1

Array Size : 1534080 (1498.38 MiB 1570.90 MB)

Device Size : 1534080 (1498.38 MiB 1570.90 MB)

Raid Devices : 2

Total Devices : 2

Preferred Minor : 1

Persistence : Superblock is persistent

Update Time : Mon Apr 15 02:06:01 2013

State : clean

Active Devices : 2

Working Devices : 2

Failed Devices : 0

Spare Devices : 0

UUID : 49c564cc:2d3c9a14:d93ce1c9:070663ca

Events : 0.42

Number Major Minor RaidDevice State

0 3 2 0 active sync /dev/hda2

1 3 66 1 active sync /dev/hdb2

The mdadm command can also be used to manage your softraid devices. For more info, run the following:

$ sudo mdadm --manage -help

$ man mdadm

Make sure that you have a RAID disk prior to installing the mdadm package. When you install mdadm, the installation program will attempt to configure your RAID drives as part of the installation. Furthermore, should you uninstall mdadm, it will likely leave behind a mdadm.conf file in /etc/mdadm. The presence of this file can cause compatibility issues with the lvm2 package, described later.

Finding Out about Filesystem Use

Running out of disk space can be annoying on your desktop system and potentially a disaster on your servers. To determine how much disk space is available and how much is currently in use, you can use the df command. To check how much space particular files and directories are consuming, use the du command.

The df command provides utilization summaries of your mounted filesystems. Using the -h option, you can have the data (which is shown in bytes by default) converted to megabytes (M) and gigabytes (G), to make that output more human-readable:

$ df -h Display space on filesystems in human-readable form

Filesystem Size Used Avail Use% Mounted on

/dev/sda2 7.6G 3.4G 3.9G 47% /

/dev/sda1 99M 14M 80M 15% /boot

tmpfs 501M 0 501M 0% /dev/shm

/dev/sda5 352G 197G 137G 59% /home

//thompson/chris 9204796 5722608 3007068 66% /mnt/mymount

Because ext filesystems have only so many inodes created at mkfs time, if you have lots of small files, you can possibly run out of inodes before you run out of actual space. To check inode utilization, use the -i option:

$ df -hi

Filesystem Inodes IUsed IFree IUse% Mounted on

/dev/sda2 2.0M 108K 1.9M 6% /

If you have network mounts (such as Samba or NFS), these will show up, too, in your df output. To limit df output to local filesystems, type the following:

$ df -hl Display disk space only for local file systems

To add the filesystem type to the listing, use the -T option:

$ df -hT Add file system type information to listing

Filesystem Type Size Used Avail Use% Mounted on

/dev/sda7 ext4 8.8G 5.5G 2.9G 66% /

To check for disk space usage for particular files or directories in a filesystem, use the du command. The following command was run as the user named chris:

$ du -h /home/ Show disk space usage for /home directory

du: `/home/joe': Permission denied

4.0K /home/chris/Mail

52K /home/chris

64K /home/

The output shows that access to another home directory’s disk use (in this case /home/joe) was denied for security reasons. So the next examples show how to avoid permission issues and get totals that are correct by using the root user account. This is clearly visible when you use -s to summarize:

$ du -sh /home Regular user is denied space totals to others' homes

du: `/home/joe': Permission denied

du: `/home/horatio199': Permission denied

64K /home

$ sudo du -sh /home You can display summary disk use as root user

1.6G /home

You can specify multiple directories with the -c option and total them up:

$ sudo du -sch /home /var Show directory and total summaries

1.6G /home

111M /var

1.7G total

You can exclude files that match a pattern from being counted using the exclude option. In the following example, disk image files (ending with the .iso suffix) are not used in totaling the disk space used:

$ sudo du -sh --exclude='*.iso' /home/chris Exclude ISOs from totals

588M /home/chris

You can specify what depth in the tree you want to summarize. Set --max-depth to a number larger than the 1 value shown to dig deeper into disk space usage:

$ sudo du -h --max-depth=1 /home Provide disk use to one level deep

1.6G /home/chris

52K /home/joe

1.6G /home

$ sudo du -h --max-depth=2 /home Dig two-levels deep for disk space use

...

4.0K /home/joe/Mail

52K /home/joe

1.6G /home

Logical Volume Manager

Logical Volume Manager (LVM) is a feature designed to help you cope with the changing needs for disk space on your Linux systems. With your hard disks configured as LVM volumes, you have tremendous flexibility in growing, shrinking, and moving the storage space on your systems as your needs change. LVM also allows for snapshots, a feature typically found on expensive enterprise SANs, or Storage-Area Networks.

Ubuntu incorporates LVM2 into its releases and uses it to define how disk partitions are allocated when you first install Ubuntu. Using LVM2, you define and manage volume groups (vg), logical volumes (lv), and physical volumes (pv). Each logical volume and physical volume is divided up into logical extents and physical extents, respectively.

The basic business of using LVM is to create the volume groups and logical volumes you need, and then assign the extents (small chunks of disk space) to those areas where they are needed. Unlike older disk partitioning schemes, where you might have to back up your data, change your partitioning, and then return data to the resized partitions, you can simply add unused extents where they are needed. Likewise, if you run out of extents in a volume group, you can simply add more physical volumes so more space is available.

To use LVM, you need to install the lvm2 package.

LVM comes with a set of commands that can be used to work with LVM volumes. Step through the procedure in the following section to learn about many of those LVM commands.

Warning To avoid messing up the hard disks your computer relies on as you learn LVM, I recommend you try the following examples on some non-critical storage device. For example, I used an inexpensive 4GB USB flash drive (on /dev/sdc) to run the commands shown in this section.

Creating LVM Volumes

To begin, use the fdisk command to create physical partitions for the storage device on which you want to create logical partitions. Here we have an 4GB USB flash drive, located on device /dev/sdc:

$ sudo fdisk /dev/sdc Start command to manage disk partitions

Command (m for help): p Print current partitions (none exist)

Disk /dev/sdc: 4009 MB, 4009754624 bytes

84 heads, 22 sectors/track, 4237 cylinders

Units = cylinders of 1848 * 512 = 946176 bytes

Sector size (logical/physical): 512 bytes / 512 bytes

I/O size (minimum/optimal): 512 bytes / 512 bytes

Disk identifier: 0xdba43801 Device Boot Start End

Blocks Id System

Command (m for help): n Create a new partition

Command action

e extended

p primary partition (1-4)

p Make it a primary partition

Partition number (1-4): 1 Assigned to partition 1

First cylinder (2-4237, default 2): <Enter>

Using default value 2

Last cylinder or +size or +sizeM or +sizeK

(2-4237, default 4237): <Enter>

Using default value 4237

Command (m for help): t Assign a partition type

Selected partition 1

Hex code (type L to list codes): 8E Indicate 8E (LVM partition)

Changed system type of partition 1 to 8e (Linux LVM)

Command (m for help): p Type p to see the new partition

...

Device Boot Start End Blocks Id System

/dev/sdc1 1 4237 3914977 8e Linux LVM

Before proceeding, make sure you have made the correct changes to the correct partition! If everything looks correct, write the new partition table, as follows:

Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.

Syncing disks.

Back at the shell prompt, use the sfdisk command to see the partitioning on the drive:

$ sudo sfdisk -l /dev/sdc View the LVM partitions

Disk /dev/sdc: 4237 cylinders, 84 heads, 22 sectors/track

Units = cylinders of 946176 bytes, blocks of 1024 bytes, count from 0

Device Boot Start End #cyls #blocks Id System

/dev/sdc1 0+ 4236 4237- 3914977 8e Linux LVM

/dev/sdc2 0 - 0 0 0 Empty

/dev/sdc3 0 - 0 0 0 Empty

/dev/sdc4 0 - 0 0 0 Empty

Next, make /dev/sdc1 a new LVM physical volume and use the pvs command to view information about physical LVM volumes:

$ sudo pvcreate /dev/sdc1 Make sdb1 an LVM physical volume

Physical volume "/dev/sdc1" successfully created

$ sudo pvs View physical LVM partitions

PV VG Fmt Attr PSize PFree

/dev/sdc1 lvm2 a-- 3.73g 3.73g

Then use vgcreate to create the vgusb volume group and list the active current volume groups:

$ sudo vgcreate vgusb /dev/sdc1 Create vgusb volume group

Volume group "vgusb" successfully created

$ sudo vgs View current volume groups

VG #PV #LV #SN Attr VSize VFree

vgusb 1 0 0 wz--n- 3.73g 3.73g

Use lvcreate to create a new LVM partition of 1GB from the vgusb volume group. Then use lvs to see the logical volume and vgs to see that the amount of free space has changed:

$ sudo lvcreate --size 1G --name lvm_u1 vgusb

Logical volume "lvm_u1" created

$ sudo lvs View the logical volume information

LV VG Attr LSize Pool Origin Data% Move Log Copy% Convert

lvm_u1 vgusb -wi-a--- 1.00g

$ sudo vgs See that you still have 2.73GB free

VG #PV #LV #SN Attr VSize VFree

vgusb 1 1 0 wz--n- 3.73g 2.73g

To create an ext4 filesystem on the lvm partition, use the mkfs.ext4 command as follows:

$ sudo mkfs.ext4 /dev/mapper/vgusb-lvm_u1

mke2fs 1.41.12 (17-May-2010)

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

...

Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 23 mounts or

180 days, whichever comes first. Use tune2fs -c or -i to override.

The ext4 filesystem has now been created and the LVM volume is ready to use.

Using LVM Volumes

To use the new volume just created, represented by /dev/mapper/vgusb-lvm_u1, create a mount point (/mnt/u1) and mount the volume. Then use df to check the available space:

$ sudo mkdir /mnt/u1 Create mount point

$ sudo mount -t ext4 /dev/mapper/vgusb-lvm_u1 /mnt/u1 Mount volume

$ df -m /mnt/u1 Check disk space

Filesystem 1M-blocks Used Available Use% Mounted on

/dev/mapper/vgusb-lvm_u1

1008 34 924 4% /mnt/u1

At this point, the filesystem contains only the lost+found directory:

$ ls /mnt/u1

lost+found

Copy a file to the new filesystem. For example, choose one of the kernel files from the /boot directory and copy it to /mnt/u1:

$ sudo cp /boot/vmlinuz-* /mnt/u1/ Copy a file to /mnt/u1

$ df -m /mnt/u1 See that 45MB is used on /mnt/u1

Filesystem 1M-blocks Used Available Use% Mounted on

/dev/mapper/vgusb-lvm_u1

1008 45 913 5% /mnt/u1

Run md5sum on the file you copied and save the resulting checksum for later. For example:

$ sudo md5sum /mnt/u1/vmlinuz-* Check md5sum

56b9ca81bdfba6e563f407caf9a52a2b /boot/vmlinuz-3.2.0-38-generic

Growing the LVM Volume

Say that you are running out of space and you want to add more space to your LVM volume. To do that, unmount the volume and use the lvresize command. (Actually, it is not required that you unmount the volume to grow it, but it is done here as an extra precaution.) After that, you must also check the filesystem with e2fsck and run resize2fs to resize the ext4 filesystem on that volume:

$ sudo umount /mnt/u1 Unmount volume

$ sudo lvresize --size 2G /dev/vgusb/lvm_u1 Resize volume

Extending logical volume lvm_u1 to 2.00 GiB

Logical volume lvm_u1 successfully resized

$ sudo e2fsck -f /dev/vgusb/lvm_u1

e2fsck 1.41.12 (17-May-2010)

Pass 1: Checking inodes, blocks, and sizes

Pass 2: Checking directory structure

Pass 3: Checking directory connectivity

Pass 4: Checking reference counts

Pass 5: Checking group summary information

/dev/vgusb/lvm_u1: 14/65536 files (0.0% non-contiguous),

15546/262144 blocks$ sudo resize2fs /dev/vgusb/lvm_u1 2G Resize lvm_u1

resize2fs 1.41.12 (17-May-2010)

Resizing the filesystem on /dev/vgusb/lvm_u1 to 524288 (4k) blocks.

The filesystem on /dev/vgusb/lvm_u1 is now 524288 blocks long.

In the example just shown, the volume and the filesystem are both resized to 2GB. Next, mount the volume again and check the disk space and the md5sum you created earlier:

$ sudo mount -t ext4 /dev/mapper/vgusb-lvm_u1 /mnt/u1 Remount volume

$ df -m /mnt/u1 See 45MB of 2016MB used

Filesystem 1M-blocks Used Available Use% Mounted on

/dev/mapper/vgusb-lvm_u1

2016 45 1869 3% /mnt/u1

$ sudo md5sum /mnt/u1/vmlinuz-* Recheck md5sum

56b9ca81bdfba6e563f407caf9a52a2b /boot/vmlinuz-3.2.0-38-generic

The newly mounted volume is now 2GB instead of 1GB in size.

Shrinking an LVM Volume

You can also use the lvresize command if you want to take unneeded space from an existing LVM volume. As before, unmount the volume before resizing it and run e2fsck (to check the filesystem) and resize2fs (to resize it to the smaller size):

$ sudo umount /mnt/u1

$ sudo e2fsck -f /dev/vgusb/lvm_u1

e2fsck 1.41.12 (17-May-2010)

Pass 1: Checking inodes, blocks, and sizes

Pass 2: Checking directory structure

...

/dev/vgusb/lvm_u1: 14/131072 files (0.0% non-contiguous),

19723/524288 blocks

$ sudo resize2fs /dev/vgusb/lvm_u1 1G Resize fs

resize2fs 1.41.12 (17-May-2010)

Resizing the filesystem on /dev/vgusb/lvm_u1 to 262144 (4k) blocks.

The filesystem on /dev/vgusb/lvm_u1 is now 262144 blocks long.

$ sudo lvresize --size 1G /dev/vgusb/lvm_u1

WARNING: Reducing active logical volume to 1.00 GiB

THIS MAY DESTROY YOUR DATA (filesystem etc.)

Do you really want to reduce lvm_u1? [y/n]: y

Reducing logical volume lvm_u1 to 1.00 GiB

Logical volume lvm_u1 successfully resized

$ sudo mount -t ext4 /dev/mapper/vgusb-lvm_u1 /mnt/u1 Remount volume

$ df -m /mnt/u1 See 4MB of 12MB used

Filesystem 1M-blocks Used Available Use% Mounted on

/dev/mapper/vgusb-lvm_u1

1008 45 913 5% /mnt/u1

The newly mounted volume appears now as 1008MB instead of 2016MB in size.

Removing LVM Logical Volumes and Groups

To remove an LVM logical volume from a volume group, unmount it and then use the lvremove command as follows:

$ sudo umount /dev/vgusb/lvm_u1

$ sudo lvremove /dev/vgusb/lvm_u1

Do you really want to remove active logical volume "lvm_u1"? [y/n]: y

Logical volume "lvm_u1" successfully removed

To remove an existing LVM volume group, use the vgremove command:

$ sudo vgremove vgusb

Volume group "vgusb" successfully removed

There are many more ways to work with LVM. Refer to the LVM HOWTO for further information (http://tldp.org/HOWTO/LVM-HOWTO/).

Summary

Creating and managing filesystems in Linux are critical parts of Linux system administration. Ubuntu contains support for many standard Linux filesystem types (ext2, ext3, ext4, xfs, reiserfs, and others). It can also create and manage Windows filesystem types (VFAT, NTFS, and so on) as well as legacy and specialty Linux and Unix filesystem types (such as minix, jfs, and xfs).

You can partition hard disks with commands such as fdisk and parted. Tools for working with filesystems include those that create filesystems (mkfs), view and modify filesystem attributes (tune2fs and dumpe2fs), mount/unmount filesystems (mount and umount), and check for problems (badblocks and fsck). To see how much space has been used in filesystems, use the df and du commands.