-->

Working with Filesystems On Linux

Linux provides a few different utilities that make it easier to work with filesystems from the command line. You can add new filesystems or change existing filesystems from the comfort of your own keyboard. This section walks you through the commands for interacting with filesystems from a command line environment.

Working with Filesystems On Linux
Working with Filesystems On Linux


Creating partitions

To start out, you need to create a partition on the storage device to contain the filesystem. The partition can be an entire disk or a subset of a disk that contains a portion of the virtual directory.

The fdisk utility is used to help you organize partitions on any storage device installed on the system. The fdisk command is an interactive program that allows you to enter commands to walk through the steps of partitioning a hard drive.

To start the fdisk command, you need to specify the device name of the storage device you want to partition and you need to have superuser privileges. When you don’t have superuser privileges and attempt to use fdisk, you’ll receive some sort of error message, like this one:
$ fdisk /dev/sdbUnable to open /dev/sdb$


Note

Sometimes, the hardest part of creating a new disk partition is trying to find the physical disk on your Linux system. Linux uses a standard format for assigning device names to hard drives, but you need to be familiar with the format. For older IDE drives, Linux uses /dev/hdx, where x is a letter based on the order the drive is detected (a for the first drive, b for the second, and so on). For both the newer SATA drives and SCSI drives, Linux uses /dev/sdx, where x is a letter based on the order the drive is detected (again, a for the first drive, b for the second, and so on). It’s always a good idea to double-check to make sure you are referencing the correct drive before formatting the partition!

If you do have superuser privileges and the correct device name, the fdisk command allows you entrance into the utility as demonstrated here on a CentOS distribution:

  1. $ sudo fdisk /dev/sdb
  2. [sudo] password for Christine:
  3. Device contains neither a valid DOS partition table,
  4. nor Sun, SGI or OSF disklabel
  5. Building a new DOS disklabel with disk identifier 0xd3f759b5.
  6. Changes will remain in memory only
  7. until you decide to write them.
  8. After that, of course, the previous content won’t be recoverable.
  9. Warning: invalid flag 0x0000 of partition table 4 will
  10. be corrected by w(rite)
  11. […]
  12. Command (m for help):

Tip
If this is the first time you’re partitioning the storage device, fdisk gives you a warning that a partition table is not on the device.

The fdisk interactive command prompt uses single letter commands to instruct fdisk what to do. The Table below shows the commands available at the fdisk command prompt.

The fdisk Commands
The fdisk Commands

Although this list may look intimidating, usually you need just a few basic commands in day-to-day work.

For starters, you can display the details of a storage device using the p command:

  1. Command (m for help): p
  2. Disk /dev/sdb: 5368 MB, 5368709120 bytes
  3. 255 heads, 63 sectors/track, 652 cylinders
  4. Units = cylinders of 16065 * 512 = 8225280 bytes
  5. Sector size (logical/physical): 512 bytes / 512 bytes
  6. I/O size (minimum/optimal): 512 bytes / 512 bytes
  7. Disk identifier: 0x11747e88
  8. Device Boot Start End Blocks Id System
  9. Command (m for help):

The output shows that the storage device has 5368MB of space on it (5GB). The listing under the storage device details shows whether there are any existing partitions on the device. The listing in this example doesn’t show any partitions, so the device is not partitioned yet.

Next, you’ll want to create a new partition on the storage device. Use the n command for that:

  1. Command (m for help): n
  2. Command action
  3. e extended
  4. p primary partition (1-4)
  5. p Partition number (
  6. 1-4): 1
  7. First cylinder (1-652, default 1): 1
  8. Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): +2G
  9. Command (m for help):

Partitions can be created as either a primary partition or an extended partition. Primary partitions can be formatted with a filesystem directly, whereas extended partitions can only contain other primary partitions. The reason for extended partitions is that there can only be four partitions on a single storage device. You can extend that by creating multiple extended partitions and then creating primary partitions inside the extended partitions.

This example creates a primary storage device, assigns it partition number 1, and then allocates 2GB of the storage device space to it. You can see the results using the p command again:

  1. Command (m for help): p
  2. Disk /dev/sdb: 5368 MB, 5368709120 bytes
  3. 255 heads, 63 sectors/track, 652 cylinders
  4. Units = cylinders of 16065 * 512 = 8225280 bytes
  5. Sector size (logical/physical): 512 bytes / 512 bytes
  6. I/O size (minimum/optimal): 512 bytes / 512 bytes
  7. Disk identifier: 0x029aa6af
  8. Device Boot Start End Blocks Id System
  9. /dev/sdb1 1 262 2104483+ 83 Linux
  10. Command (m for help):

Now in the output there’s a partition shown on the storage device (called /dev/sdb1). The Id entry defines how Linux treats the partition. fdisk allows you to create lots of partition types. Using the l command lists the different types available. The default is type 83, which defines a Linux filesystem. If you want to create a partition for a different filesystem (such as a Windows NTFS partition), just select a different partition type.

You can repeat the process to allocate the remaining space on the storage device to another Linux partition. After you’ve created the partitions you want, use the w command to save the changes to the storage device:

  1. Command (m for help): w
  2. The partition table has been altered!
  3. Calling ioctl() to re-read partition table.
  4. Syncing disks.
  5. $

The storage device partition information was written to the partition table, and Linux was informed of the new partition via the ioctl() call. Now that you have set up a partition on the storage device, you’re ready to format it with a Linux filesystem.

Tip

Some distributions and older distribution versions do not automatically inform your
Linux system of a new partition after it is made. In this case, you need to use either
the partprobe or hdparm command (see their man pages), or reboot your system so it
reads the updated partition table

0 Response to "Working with Filesystems On Linux"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel