The fdisk command allows you to get the information of partition, create and delete partitions.
10.2.1 Merits of Partitioning
Dividing into partitions has the following reasons.
- Differentiate the backup frequency of system and data
- Narrow the influence range at the time of system failure
- Improve file access speed
fdisk [option] [device_file] |
It displays the disk structure. It divides disk into partitions.
-l
Display the device partition information
|
Practice : Display a hard disk partition information |
# fdisk -l ↵ Display a hard disk
partition information
Disk /dev/sda: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System 2 devices information was displayed
/dev/sda1 * 1 587 4715046 83 Linux
/dev/sda2 588 783 1574370 82 Linux swap / Solaris
|
* This result changes with environment.
From the information displayed by the fdisk command, we can know that the two devices are created to this hard disk and the names are /dev/sda1 and /dev/sda2. We can understand the hard disk(sda) has been divided into 2 partitions. The first partition is 4715046 blocks (4.5 G bytes), and its ID is 83 (for Linux).
The second partition is 1574370 blocks (1.5 G bytes), and its ID is 82 (for swap file).
Figure 10-2: Examples of partitioning
10.2.2 Partitioning
For connecting hard disks to computer, the three interface standards(IDE, SATA (serial ATA), and SCSI) are widely used.
You can divide into 4 primary partitions from a hard disk. If you need more than 4 partitions, you have to create an extended partition instead of 4th primary partition. You can create multiple logical partitions in the extended partition.
- Some examples of partitioning
Four primary partitions
Three primary partitions and an extended partition which has two logical partitions
The logical partition is basically same as primary partition. You can create a file system in a logical partition and store files and directories in it. On the other hand, the extended partition is container of logical partitions, so you cannot create any file systems in it.
- The maximum number of partitions
IDE:63 (primary 3 + logical60)
SATA:15 (primary 3 + logical12)
SCSI:15 (primary 3 + logical12)
In general, we use more than 4 partitions in Linux, so the configuration of three primary partitions and one extended partition exists in many case. Moreover Linux sometimes uses two or more hard disks as one hard disk by using the LVM (Logical Volume Manager) function.
10.2.3 Extend Hard Disk and Make it available
Let's confirm the procedure which makes new hard disk available. We will make a data area and a swap space (temporary space for swapping ) on a new disk.
In order to get two space, we divide the disk into two partitions by the fdisk command. You also should be careful for its operation. If you operate for the partition already used, the existing data may be destroyed.
If you noticed that your configuration parameters are wrong, you quit it as soon as possible before saving it.
Since partitioning is one of system management's work, it is necessary to execute these commands by the root user. If possible, you should use this command in the level 1 mode which only system manager can operate. (The general user usually uses Linux in level 3 or level5.)
* Procedure for making a new hard disk available
- Check the partition information
- Change the partition's attributes
- Save the partition information
- Reboot (usually warning is displayed, if reboot is required)
- Setting up automatic mounting
Practice : Check the partition information |
Let's run fdisk command with /dev/sdc as device parameter. The current partition information can be displayed by p command. Nothing will be displayed if the hard disk has not been divided into partitions yet. Even if partitioning was already done, if there is some space which has not been kept as partitions, you can create new partition.
# fdisk /dev/sdc ↵
Command (m for help): p ↵ Display the current partition information
Disk /dev/sdc: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
Nothing is displayed because partitioning has not done yet.
|
Practice : Create partitions |
Let's get new partition by n command.
Let's create primary partition by entering p.
Direct the first cylinder and size of the partition. After getting partitions, let's check the created partitions by using p command.
Command (m for help): n ↵ Get new partition
Command action
e extended
p primary partition (1-4)
p ↵ Primary partition
Partition number (1-4): 1 ↵ 1 because of first partition
First cylinder (1-783, default 1): ↵ Default means the foremost cylinder
Last cylinder or +size or +sizeM or +sizeK (1-783, default 783): +1024M ↵
The size of getting
Command (m for help): n ↵ Get new partition
Command action
e extended
p primary partition (1-4)
p ↵ Primary partition
Partition number (1-4): 2 ↵ 2 because of first partition
First cylinder (126-783, default 126): 126 ↵ Direct the foremost cylinder
Last cylinder or +size or +sizeM or +sizeK (126-783, default 783): +1024M ↵
The size of getting
Command (m for help): p ↵ Display the current partition information
Disk /dev/sdc: 6442 MB, 6442450944 bytes
255 heads, 63 sectors/track, 783 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System Display the created partitions
/dev/sdc1 1 125 1004031 83 Linux
/dev/sdc2 126 250 1004062+ 83 Linux
|
There are two opportunities which require the number of cylinder. In case of using the disk space continuously, we can use the default cylinder number as the first cylinder number. You can specify the size with '+', instead of the number of last cylinder. You don't have to calculate the number of cylinders here.
Practice : Change the type of partition |
There are some partition types by use purpose. A new partition which just created is assigned 83 as the type, which is used for Linux file system.
If you want to use it as a swap, please change its type by the t command.
In order to use as a swap partition, 82 (for a swap) is specified as ID. You can display about what kind of partition type can be used by using L command, when you have to specify the partition type.
Command (m for help): t ↵ Change the information about partition
Partition number (1-4): 2 ↵
Hex code (type L to list codes): L ↵ List the type of partitions
0 Empty 1e Hidden W95 FAT1 80 Old Minix be Solaris boot
1 FAT12 24 NEC DOS 81 Minix / old Lin bf Solaris
2 XENIX root 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 84 OS/2 Hidden C: c6 DRDOS/sec (FAT-
5 Extended 41 PPC PReP Boot 85 Linux extended c7 Syrinx
6 FAT16 42 SFS 86 NTFS volume set da Non-FS data
7 HPFS/NTFS 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / .
8 AIX 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility
9 AIX bootable 4f QNX4.x 3rd part 8e Linux LVM df BootIt
a OS/2 Manag 50 OnTrack DM 93 Amoeba e1 DOS access
b W95 FAT32 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O
c W95 FAT32 (LBA) 52 CP/M 9f BSD/OS e4 SpeedStor
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad eb BeOS fs
f W95 Ext'd (LBA) 54 OnTrackDM6 a5 FreeBSD ee EFI GPT
10 OPUS 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/
11 Hidden FAT12 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC
12 Compaq diagnost 5c Priam Edisk a8 Darwin UFS f1 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor a9 NetBSD f4 SpeedStor
16 Hidden FAT16 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fd Linux raid auto
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fe LANstep
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wiz hid ff BBT
1c Hidden W95 FAT3 75 PC/IX
Hex code (type L to list codes):82 ↵ Specify the code of swap area
Changed system type of partition 2 to 82 (Linux swap / Solaris)
|
Practice : Preserve the partition information |
Partition information is changed by writing it on hard disk by the w command. If re-reading of partition information can not be done, the warning is displayed, then please reboot the system.
If you want to cancel the partition change, you can use q command for quitting fdisk without writing partition information to hard disk.
Command (m for help): w ↵ Write the partition information into hard disk
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks. Though a warning message was displayed, the partition information
was written.
Command (m for help): q ↵
|
w - - - Write for save
q - - - Quit without change