-->

Monitoring Disk Space On Linux

Another important task of the system administrator is to keep track of the disk usage on the system. Monitoring Disk Space On Linux, Whether you’re running a simple Linux desktop or a large Linux server, you need to know how much space you have for your applications.

Monitoring Disk Space On Linux
Monitoring Disk Space On Linux


Some command line commands can help you manage the media environment on your Linux system. This section describes the core commands you’ll likely run into during your system administration duties.

Mounting media

As discussed in Chapter 3, the Linux filesystem combines all media disks into a single virtual directory. Before you can use a new media disk on your system, you must place it in the virtual directory. This task is called mounting.

In today’s graphical desktop world, most Linux distributions have the ability to automatically mount specific types of removable media. A removable media device is a medium that (obviously) can be easily removed from the PC, such as CD-ROMs and USB memory sticks.

If you’re not using a distribution that automatically mounts and unmounts removable media, you have to do it yourself. This section describes the Linux command line commands to help you manage your removable media devices.

The mount command

Oddly enough, the command used to mount media is called mount. By default, the mount command displays a list of media devices currently mounted on the system:

  1. $ mount
  2. /dev/mapper/VolGroup00-LogVol00 on / type ext3 (rw)
  3. proc on /proc type proc (rw)
  4. sysfs on /sys type sysfs (rw)
  5. devpts on /dev/pts type devpts (rw,gid=5,mode=620)
  6. /dev/sda1 on /boot type ext3 (rw)
  7. tmpfs on /dev/shm type tmpfs (rw)
  8. none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
  9. sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
  10. /dev/sdb1 on /media/disk type vfat
  11. (rw,nosuid,nodev,uhelper=hal,shortname=lower,uid=503)
  12. $

The mount command provides four pieces of information:

  • The device filename of the media
  • The mount point in the virtual directory where the media is mounted
  • The filesystem type
  • The access status of the mounted media

The last entry in the preceding example is a USB memory stick that the GNOME desktop automatically mounted at the /media/disk mount point. The vfat filesystem type shows that it was formatted on a Microsoft Windows PC.

To manually mount a media device in the virtual directory, you must be logged in as the root user or use the sudo command to run the command as the root user. The following is the basic command for manually mounting a media device:

mount -t type device directory

The type parameter defines the filesystem type under which the disk was formatted. Linux recognizes lots of different filesystem types. If you share removable media devices with your Windows PCs, you are most likely to run into these types:

  • vfat: Windows long filesystem
  • ntfs: Windows advanced filesystem used in Windows NT, XP, and Vista
  • iso9660: The standard CD-ROM filesystem

Most USB memory sticks and floppies are formatted using the vfat filesystem. If you need to mount a data CD, you must use the iso9660 filesystem type.

The next two parameters define the location of the device file for the media device and the location in the virtual directory for the mount point. For example, to manually mount the USB memory stick at device /dev/sdb1 at location /media/disk, you use the following command:

mount -t vfat /dev/sdb1 /media/disk

After a media device is mounted in the virtual directory, the root user has full access to the device, but access by other users is restricted. You can control who has access to the device using directory permissions.

In case you need to use some of the more exotic features of the mount command, The Table will shows the available parameters .

The mount Command Parameters
The mount Command Parameters




The -o option allows you to mount the filesystem with a comma-separated list of additional options. These are popular options to use:

  • ro: Mounts as read-only
  • rw: Mounts as read-write
  • user: Allows an ordinary user to mount the filesystem
  • check=none: Mounts the filesystem without performing an integrity check
  • loop: Mounts a file

The unmount command

To remove a removable media device, you should never just remove it from the system. Instead, you should always unmount it first.

Tip
Linux doesn’t allow you to eject a mounted CD. If you ever have trouble removing a CD from the drive, most likely it means the CD is still mounted in the virtual directory. Unmount it first, and then try to eject it.
The command used to unmount devices is umount (yes, there’s no “n” in the command, which gets confusing sometimes). The format for the umount command is pretty simple:

umount [directory | device ]

The umount command gives you the choice of defining the media device by either its device location or its mounted directory name. If any program has a file open on a device, the system won’t let you unmount it.

  1. [root@testbox mnt]# umount /home/rich/mnt
  2. umount: /home/rich/mnt: device is busy
  3. umount: /home/rich/mnt: device is busy
  4. [root@testbox mnt]# cd /home/rich
  5. [root@testbox rich]# umount /home/rich/mnt
  6. [root@testbox rich]# ls -l mnt
  7. total 0
  8. [root@testbox rich]#

In this example, the command prompt was still in a directory within the filesystem structure, so the umount command couldn’t unmount the image file. After the command prompt was moved out of the image file filesystem, the umount command successfully unmounted the image file.

Using the df command

Sometimes, you need to see how much disk space is available on an individual device. The df command allows you to easily see what’s happening on all the mounted disks:

  1. $ df
  2. Filesystem 1K-blocks Used Available Use% Mounted on
  3. /dev/sda2 18251068 7703964 9605024 45% /
  4. /dev/sda1 101086 18680 77187 20% /boot
  5. tmpfs 119536 0 119536 0% /dev/shm
  6. /dev/sdb1 127462 113892 13570 90% /media/disk
  7. $

The df command shows each mounted filesystem that contains data. As you can see from the mount command earlier, some mounted devices are used for internal system purposes. The command displays the following:
  • The device location of the device
  • How many 1024-byte blocks of data it can hold
  • How many 1024-byte blocks are used
  • How many 1024-byte blocks are available
  • The amount of used space as a percentage
  • The mount point where the device is mounted

A few different command line parameters are available with the df command, most of which you’ll never use. One popular parameter is -h, which shows the disk space in human-readable form, usually as an M for megabytes or a G for gigabytes:

  1. $ df -h
  2. Filesystem Size Used Avail Use% Mounted on
  3. /dev/sdb2 18G 7.4G 9.2G 45% /
  4. /dev/sda1 99M 19M 76M 20% /boot
  5. tmpfs 117M 0 117M 0% /dev/shm
  6. /dev/sdb1 125M 112M 14M 90% /media/disk
  7. $

Now instead of having to decode those ugly block numbers, all the disk sizes are shown using “normal” sizes. The df command is invaluable in troubleshooting disk space problems on the system.

Note
Remember that the Linux system always has processes running in the background that handle files. The values from the df command reflect what the Linux system thinks are the current values at that point in time. It’s possible that you have a process running that has created or deleted a file but has not released the file yet. This value is not included in the free space calculation.

Using the du command

With the df command, you can easily see when a disk is running out of space. The next problem for the system administrator is to know what to do when that happens.

Another useful command to help you is the du command. The du command shows the disk usage for a specific directory (by default, the current directory). This is a quick way to determine if you have any obvious disk hogs on the system.

By default, the du command displays all the files, directories, and subdirectories under the current directory, and it shows how many disk blocks each file or directory takes. For a standard-sized directory, this can be quite a listing. Here’s a partial listing of using the du command:

  1. $ du
  2. 484 ./.gstreamer-0.10
  3. 8 ./Templates
  4. 8 ./Download
  5. 8 ./.ccache/7/0
  6. 24 ./.ccache/7
  7. 368 ./.ccache/a/d
  8. 384 ./.ccache/a
  9. 424 ./.ccache
  10. 8 ./Public
  11. 8 ./.gphpedit/plugins
  12. 32 ./.gphpedit
  13. 72 ./.gconfd
  14. 128 ./.nautilus/metafiles
  15. 384 ./.nautilus
  16. 72 ./.bittorrent/data/metainfo
  17. 20 ./.bittorrent/data/resume
  18. 144 ./.bittorrent/data
  19. 152 ./.bittorrent
  20. 8 ./Videos
  21. 8 ./Music
  22. 16 ./.config/gtk-2.0
  23. 40 ./.config
  24. 8 ./Documents

The number at the left of each line is the number of disk blocks that each file or directory takes. Notice that the listing starts at the bottom of a directory and works its way up through the files and subdirectories contained within the directory.

The du command by itself can be somewhat useless. It’s nice to be able to see how much disk space each individual file and directory takes up, but it can be meaningless when you have to wade through pages and pages of information before you find what you’re looking for.

You can use a few command line parameters with the du command to make things a little
more legible:

  • -c: Produces a grand total of all the files listed
  • -h: Prints sizes in human-readable form, using K for kilobyte, M for megabyte, and G for gigabyte
  • -s: Summarizes each argument


The next step for the system administrator is to use some file-handling commands for manipulating large amounts of data. That’s exactly what the next section covers.

0 Response to "Monitoring Disk Space On Linux"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel