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.
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:
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:
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.
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:
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:
- $ sudo fdisk /dev/sdb
- [sudo] password for Christine:
- Device contains neither a valid DOS partition table,
- nor Sun, SGI or OSF disklabel
- Building a new DOS disklabel with disk identifier 0xd3f759b5.
- Changes will remain in memory only
- until you decide to write them.
- After that, of course, the previous content won’t be recoverable.
- Warning: invalid flag 0x0000 of partition table 4 will
- be corrected by w(rite)
- […]
- 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 |
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:
- Command (m for help): p
- Disk /dev/sdb: 5368 MB, 5368709120 bytes
- 255 heads, 63 sectors/track, 652 cylinders
- Units = cylinders of 16065 * 512 = 8225280 bytes
- Sector size (logical/physical): 512 bytes / 512 bytes
- I/O size (minimum/optimal): 512 bytes / 512 bytes
- Disk identifier: 0x11747e88
- Device Boot Start End Blocks Id System
- 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:
- Command (m for help): n
- Command action
- e extended
- p primary partition (1-4)
- p Partition number (
- 1-4): 1
- First cylinder (1-652, default 1): 1
- Last cylinder, +cylinders or +size{K,M,G} (1-652, default 652): +2G
- 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:
- Command (m for help): p
- Disk /dev/sdb: 5368 MB, 5368709120 bytes
- 255 heads, 63 sectors/track, 652 cylinders
- Units = cylinders of 16065 * 512 = 8225280 bytes
- Sector size (logical/physical): 512 bytes / 512 bytes
- I/O size (minimum/optimal): 512 bytes / 512 bytes
- Disk identifier: 0x029aa6af
- Device Boot Start End Blocks Id System
- /dev/sdb1 1 262 2104483+ 83 Linux
- 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:
- Command (m for help): w
- The partition table has been altered!
- Calling ioctl() to re-read partition table.
- Syncing disks.
- $
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