Working with the Fdisk program. Markup utilities. fdisk

To determine what condition it is in HDD purchased PC, you should:

1) try to boot from hard drive, why turn on the computer if there is no floppy disk in drive A. If any operating system booted, then the hard drive is already prepared for operation and you don’t have to resort to using the FDISK utility. If the download fails, then you need to go to step 2.

2) check if the hard drive is configured, i.e. Does it have DOS partitions? To do this you need to boot from boot floppy, enter the FDISK command (there must be an FDISK utility on the floppy disk) and select the “Display information about available partitions” option in the main menu. If the result indicates that there are no partitions, then configuration of the hard drive is required. Otherwise, it is possible to reconfigure (change the number and/or size of partitions and logical drives) using the same utility. In any of the described cases, you can complete the preparation of the hard drive for operation by high level formatting all logical drives using FORMAT commands(very often disk formatting is carried out later during the installation of the operating system). If you want to format before installing the OS, then one of the floppy disks must have FORMAT utility. If the FDISK utility does not start, you need to go to step 3.

3) perform low-level formatting of the hard drive special utility supplied with the PC. Low-level formatting of a hard drive involves the formation of sectors (taking into account the interleaving coefficient), which consists of writing sector labels to the tracks and checking their good quality (the physical ability to write information to them). Defective sectors are marked in a special way. After low level formatting you should run the FDISK command and, if necessary, format the logical drives. The inability to low-level format a disk means that the PC hardware is faulty or the cables are poorly connected.

Thus, preparation of hard disk to work “from scratch” is implemented in three stages: 1) low-level formatting of the hard disk; 2) configuring the hard drive; 3) high-level formatting of all logical drives created on it (this item is optional). Low-level formatting modern hard disks are produced at the manufacturing plant. Repeated low-level formatting of such disks is not required, and is a very dangerous operation, since it can lead to the loss of service information. Low-level reformatting hard drives legacy models can be performed using the CALIBRATE utility from the NORTON UTILITIES kit. It is useful to perform this procedure approximately once a year to restore the sector marks and at the same time test the quality of the disk surface. The need to separate low-level and high-level hard formatting disk over time is explained by the fact that after low-level formatting it is required to configure it. Algorithm for configuring a hard drive to work in Windows environment is as follows:


1) creating a main DOS partition with a logical disk in it;

2) creation additional section DOS (if necessary, for example, there is a need to install 2 OS on a computer: Windows 98 and Windows NT and work with different file systems);

3) formation of logical disks in an additional DOS partition (if it was created in step 2);

4) setting the indicator that the main DOS partition is active.

Purpose of the Fdisk program

The Microsoft Fdisk utility, known since the days of MS DOS, can also be used on modern systems.

This simple program does not provide as much flexibility as PartitionMagic, but it does an excellent job of partitioning disks. Fdisk can be useful in

emergency situations or when the disk is partitioned for the first time. It allows you to quickly clear partitions of data.

During its operation, the Fdisk program destroys the data recorded on the disk, so do not forget to archive the necessary files in time.

The Fdisk program takes up very little space, which makes it possible to include it in an “emergency” floppy disk.

Starting the program

If the operating system crashes, insert an emergency floppy disk containing the Fdisk file into the floppy disk drive. Restart the PC from the floppy disk as described in the previous sections. To run Fdisk you can use the disk Windows installations 9x. Boot from it and select the command Start menu computer with CD-ROM support. After loading MS DOS, enter MS in the command line DOS command a:\fdisk and press . The program will start and its window will be displayed.

IN delivery of Windows XP included various utilities, launched from command line, however, many of them have undergone significant changes compared to previous ones Windows versions. Many commands have been improved. There are a number of new commands, and some have been completely removed from Windows XP. Windows XP in particular does not support following commands- fdisk, backup, detrag, emm386, mirror, msd, r e s t o r e, scandisk, sys, etc.

To launch Fdisk in Windows Millennium, simply select the Start menu >> Run. In the Start Programs dialog box, enter the command in the Open text box

fdisk and click OK.

Key combination allows you to launch the Manager program Windows tasks, which can help you out in difficult times. In the program window, you can select and close any of the programs that, in your opinion, led to the system hanging. To restart the system, select the menu command Shutdown >> Reboot (Shutdown).

Creating partitions

To create disk partitions, follow these steps:

1. The Fdisk utility will prompt you to activate support large disks. If you refuse such activation, you will be able to create a partition with file system FAT 16 up to 2,048 MB. Select the Create DOS partition or DOS logical partition command from the menu, which will create the first partition for installing Windows 9x.

2. Select the Create Primary DOS Partition command to create a primary boot partition.

3. On the proposal to use everything available for the section disk space answer negatively. You should provide a reserve of disk space for two additional partitions, which will allow you to install Windows XP, as well as separately store document and program files.

4. Determine the size of the first partition. The table can help with this. 1.

Table 1. Sizes of logical drives required for installing various operating systems

Operating system Size, MB

Windows 98 400-2,000

Windows NT 4.0 124-1000

Windows 2000 Professional 650-2,000

Windows XP Home Edition 1 500-2 000

Windows XP Professional 1,500-2,000

Linux RedHat 6.1 135-2,000

5. Set the first partition as active, intended for boot.

Press the key and return to the main program menu. Select the menu command Select Active Partition and define the first partition as the active one.

6. First select the menu command Create DOS Section or logical drive DOS, and then - Create an additional DOS partition, which will create a logical partition for the next file system.

7. Guided by table. 1, determine the size of the second partition in which the second operating system will be installed.

8. Create the third partition in the same way. It can also be created in Windows XP using the Disk Manager program.

9. To exit the program, press the key , and then pressing the key combination twice , perform a “warm system start”.

In order to have comprehensive information about hard drive parameters, follow the sequence Start => Control Panel => Administrative Tools => Computer Management.

Select Disk Defragmenter. In the dialog box that opens, select the disk to be analyzed and click the Analyze button. In the Disk Defragmentation dialog box, click the Print Report button.

fdisk [-BIaipstu] [-b loader ] [-1234] [ disk] fdisk -f The configuration file [-itv] [ disk ]

PROLOGUE To BIOS was able to boot the kernel, certain conventions must be followed. Sector zero of the disk must contain boot code, a slice table, and magic number. Using slices BIOS, the disk may be split into several parts. BIOS reads zero sector and checks the magic number. The boot code from sector zero then checks the slice table to find the slice marked "active." It then reads the program bootstrap from the active slice, and if the slice is marked as bootable, starts it. In DOS there can be several slices, one of which will be active. Utility fdisk for DOS allows you to divide disk space into slices and mark one of them as active. DESCRIPTION Utility fdisk for serves the same purposes as its similar utility for DOS. The first call form is used to display information about slice parameters or to edit it interactively. The second is for writing a slice table using a configuration file The configuration file; it is intended to be used by other scripts or programs. fdisk supports the following options: -a Only change the active slice. Ignored in combination with option -f. loader-b Use the boot code from the bootloader file. Default is/boot/mbr -f. The configuration file-B Change the boot code in sector zero of the disk. Ignored in combination with option The configuration file. -f Set slice parameters from file - " (minus sign) standard input will be used as the configuration file. For a description of the configuration file format, see the section below CONFIGURATION FILE.

WARNING: Unlike interactive mode, when using the option -f changes are made without asking for confirmation. Be careful!
-i Initialize sector zero of the disk. Before editing, existing slices will be marked as unused (" -f"). (Compare with option -u). 0 -I Initialize the slice table in the sector one FreeBSD slice for the entire disk. fdisk-p Print the slice table in the format CONFIGURATION FILE configuration file and go out; cm. below. -s Output -f brief information and go out.-t Test mode: changes will not be written to disk. Typically used in conjunction with the option -f to find out what would have been written to the slice table. Includes option -v. -f, fdisk-u Edit the disk slice table in sector 0. Ignored in combination with the option -f. disk-v Print detailed messages. In combination with fdisk prints the writable slice table. 0 -1234 Operate on only one slice. Ignored in combination with option . As an argument calculations are: cylinders=769 heads=15 sectors/track=33 (495 blks/cyl) Warning: BIOS sector numbering starts with sector 1 Information from DOS bootblock is: The data for partition 1 is: sysid 165,(FreeBSD/NetBSD/ 386BSD) start 495, size 380160 (185 Meg), flag 0 beg: cyl 1/ sector 1/ head 0; 165 end: cyl 768/ sector 33/ head 14 The data for partition 2 is: sysid 164,(unknown) start 378180, size 2475 (1 Meg), flag 0 beg: cyl 764/ sector 1/ head 0; end: cyl 768/ sector 33/ head 14 The data for partition 3 is: The data for partition 4 is: sysid 99,(ISC UNIX, other System V/386, GNU HURD or Mach) start 380656, size 224234 (109 Meg ), flag 80 beg: cyl 769/ sector 2/ head 0; end: cyl 197/ sector 33/ head 14 The disk is divided into three slices that completely cover it. The second slice partially overlaps the end of the first. (Used for debugging purposes). sysid is used to label the slices. FreeBSD reserves a magic number for itself V -u decimal system -f Numbers (A5 in hexadecimal). start and size fields containing the starting address and slice size in sectors. flag 80 indicates that this is the active slice. fdisk The cyl, sector and head fields are used to indicate the start and end addresses of the slice (cylinder, sector and head, respectively). Note: these numbers are calculated based on the disk geometry assumed by the BIOS and are stored in the boot sector. Flags fdisk-i And, indicate to the program that the slice table should be changed. If the flag was not specified, work with the utility will occur in dialog mode. In this case, no changes will be written to disk unless you explicitly request it. Fdisk will show each slice and ask if you want to edit it. If the answer is yes, -u V sysid is used to label the slices. FreeBSD reserves a magic number for itself will sequentially move from field to field, displaying the old value and requesting a new one. When you are finished editing the slice, -u edits (updates) the parameters of existing slices, whereas sysid is used to label the slices. FreeBSD reserves a magic number for itself used to "initialize" them (old values ​​are ignored). If the first slice is edited, then sysid is used to label the slices. FreeBSD reserves a magic number for itself will configure it to cover the entire disk and make it active. NOTES When automatically calculating the starting cylinder, head, etc. uses a set of values ​​that reflect the geometry of the disk based on the data BIOS. By default, these values ​​are taken from the drive label in the kernel ( in-core disk label), But fdisk provides the opportunity to change them. This allows you to create a boot block that can work with drives that use address translation via BIOS. When manually marking the disk, make sure that the FreeBSD slices start at the cylinder boundary. Changing the parameters of an existing slice is likely to result in the loss of data stored on that slice. It makes sense to run it several times fdisk interactively to see all the changes it makes. It is safe provided you answer no to the last question, " Should we write new partition table?". There are a number of configuration subtleties that the utility fdisk detects, but which are not described in detail in this man page. CONFIGURATION FILE When using the option -f the disk slice table can be written using the values ​​specified in the file The configuration file. The format of this file is quite simple, each line is either a comment or a specification, as shown below: # comment lines starting with # are comments and are ignored.
  • g spec1 spec2 spec3 Set the BIOS geometry used to calculate slice parameters. Three values ​​must be specified, with a letter in front of each number: cnum - Set the number of cylinders to.
  • num hnum - Set the number of cylinders to.
  • - Set the number of heads equal snum - Set the number of cylinders to.
- Set the number of sectors per track to<= число цилиндров 1 <= число головок <= 256 1 <= число секторов на дорожке < 64 Число цилиндров должно быть меньше либо равным 1024 The values ​​can be specified in any order, because The first letter clearly defines what meaning is meant. However, in any case, all three values ​​must be specified. This line must precede lines specifying slice parameters. Values ​​can be in the following ranges: 1 1024 . This is not strictly necessary, but if you specify a number greater than 1024 , a warning will be issued. Please note that FreeBSD boot slices containing the root ("/") filesystem must be located first 1019 cylinders, 39 heads and 63 sectors on the track: g c1019 h39 s63 g h39 c1019 s63 g s63 h39 c1019 p slice type start length Set the slice with number slice (1-4 ) type type, initial sector start and size length sectors. Only those slices whose parameters are specified by such lines will undergo changes; the remaining slices will retain their previous parameters. However, if the existing slice table is incorrect or the option is specified sysid is used to label the slices. FreeBSD reserves a magic number for itself, all existing slices will be deleted (marked as unused), and the layout will need to be set accordingly p lines. When specifying parameters for multiple slices, you must specify multiple lines p, one for each slice. These lines must follow the line specifying the geometry, if such a line is present in the file. For FreeBSD slices the value is type equals 165 . Specifying a null type is similar to deleting a slice (by marking it "unused"), but the fields start V length in this case also arbitrary values ​​must be assigned, for example, 0 . Note: If necessary, the start offset will be rounded up to the head boundary and the end offset will be rounded down to the cylinder boundary. Example: to delete 4th slice and make it "unused": p 4 0 0 0 Example: to set 1st slice as FreeBSD slice starting with 1 -th sector of size 2503871 sectors (these values ​​will be rounded up and down so that the slice falls on the boundaries of the head and cylinder, respectively): p 1 165 1 2503871 a slice Make a slice slice active. This line can be located anywhere in the file, but there can only be one such line in the entire file. Example: to make active 1 th slice: a 1 FILES Use the boot code from the bootloader file. Default is Default bootloader code. SEE ALSO boot0cfg(8), bsdlabel(8), newfs(8) PROBLEMS The default loader code does not always correctly recognize all slice types, particularly those introduced since MS-DOS 6.x. This utility could be more user friendly. Most FreeBSD newbies don't understand the difference between a "slice" and a "partition", which leads to problems. Using this utility, it is impossible to allocate the entire disk for use by FreeBSD. To do this you need to use the utility bsdlabel(8).

The most famous tool for working with partition tables is the program fdisk. Programs with this name are available in all operating systems. But, despite the coincidence of names, in fact these are completely different programs, so you should not use the description of one of them when using the other. In addition, in one of the manuals I found a statement that using FDISK from Win95/Win98 on systems with several different file systems (installed in different partitions) is dangerous. There are cases where this program has deleted logical partitions of a type other than FAT, although it was asked to delete a FAT partition located somewhere further in the chain of logical partitions.

There are three versions of this program on Linux: fdisk, sfdisk, cfdisk. Their main purpose is to create partitions on a “clean” disk or change the disk partition. But the main method of disk repartition in these programs comes down to deleting existing partitions and creating new ones (accordingly, with the loss of all information on the disk). True, the man page for the utility sfdisk reports that this utility can perform disk repartition, but warns that this operation is very dangerous.

All three programs mentioned have only a text interface, that is, they are launched in a console or terminal window. On the page man to the program fdisk it is reported that cfdisk creates high-quality disk partition tables and therefore, if possible, then you should use this program. At the same time it is noted that fdisk usually gives good results, but has some disadvantages and is not recommended if there are other options. Relatively sfdisk it is claimed that this is a program for hackers - it is more correct than fdisk and more powerful than fdisk V cfdisk, but has a terribly inconvenient interface. And, indeed, the interface cfdisk seems to be the most convenient since it already has an intuitive menu. The author of the article claims that it is for this reason that the program cfdisk used in Debian GNU/Linux and Slackware Linux text installers. But for some reason this utility was not available on the system installed from the Red Hat Linux 9 distribution.

U fdisk V sfdisk there is one important feature - they can display a list of all existing sections in non-interactive mode. To do this, just run these commands with the -l option. Moreover, if you do not specify a device, a list of partitions of all available disks will be displayed. The results of these commands look slightly different. On my disk with a corrupted partition table, I got the following results:

Listing 4.

# /sbin/sfdisk -l /dev/hda

Disk /dev/hda: 77545 cylinders, 16 heads, 63 sectors/track Warning: extended partition does not start at a cylinder boundary. DOS and Linux will interpret the contents differently. Units = cylinders of 516096 bytes, blocks of 1024 bytes, counting from 0 Device Boot Start End #cyls #blocks Id System /dev/hda1 0+ 764- 765- 385528 ​​82 Linux swap end: (c,h,s) expected ( 764,15,62) found (47,254,62) /dev/hda2 * 765+ 4574- 3809 1919736 c W95 FAT32 (LBA) start: (c,h,s) expected (765,1,1) found (48, 1,1) end: (c,h,s) expected (1023,15,63) found (286,254,63) /dev/hda3 4574+ 4765- 192- 96390 83 Linux start: (c,h,s) expected (1023,15,63) found (287,0,1) end: (c,h,s) expected (1023,15,63) found (298,254,63) /dev/hda4 4765+ 77535- 72771- 36676395 f W95 Ext"d (LBA) start: (c,h,s) expected (1023,15,63) found (1023,5,1) end: (c,h,s) expected (1023,15,63) found (1023,14,63) /dev/hda5 4765+ 6805- 2040- 1028128+ 83 Linux start: (c,h,s) expected (1023,15,63) found (299,1,1) end: (c ,h,s) expected (1023,15,63) found (426,254,63) /dev/hda6 6805+ 10885- 4080- 2056288+ 83 Linux start: (c,h,s) expected (1023,15,63) found (427,1,1) end: (c,h,s) expected (1023,15,63) found (682,254,63) /dev/hda7 10885+ 27141- 16257- 8193118+ 83 Linux start: (c, h,s) expected (1023,15,63) found (683,1,1) end: (c,h,s) expected (1023,15,63) found (1023,254,63) /dev/hda8 27141 + 36895- 9754- 4915858+ 83 Linux start: (c,h,s) expected (1023,15,63) found (1023,254,63) end: (c,h,s) expected (1023,15,63 ) found (1023,254,63) /dev/hda9 36895+ 77535- 40641- 20482843+ b W95 FAT32 start: (c,h,s) expected (1023,15,63) found (1023,254,63) end : (c,h,s) expected (1023,15,63) found (1023,254,63)

Listing 5.

Disk /dev/hda: 40.0 GB, 40020664320 bytes 16 heads, 63 sectors/track, 77545 cylinders Units = cylinders of 1008 * 512 = 516096 bytes Device Boot Start End Blocks Id System /dev/hda1 1 765 385528 ​​82 Linux swap Partition 1 does not end on cylinder boundary. /dev/hda2 * 766 4575 1919736 with W95 FAT32 (LBA) Partition 2 does not end on cylinder boundary. /dev/hda3 4575 4766 96390 83 Linux Partition 3 does not end on cylinder boundary. /dev/hda4 4766 77536 36676395 f W95 Ext"d (LBA) Partition 4 does not end on cylinder boundary. /dev/hda5 4766 6806 1028128+ 83 Linux /dev/hda6 6806 10886 2056288+ 83 Linux /dev/hda7 10886 2 7142 8193118+ 83 Linux /dev/hda8 27142 36896 4915858+ 83 Linux /dev/hda9 36896 77536 20482843+ b W95 FAT32

All of the above does not yet allow us to draw an unambiguous conclusion about which of these programs is preferable. Therefore, we will consider all three utilities sequentially.

2.2. fdisk program

Working with the program fdisk carried out through the command interface. If we run it without options, we will only see a prompt for one of the available single-letter keyboard commands (see Listing 6).

Listing 6.

# /sbin/fdisk /dev/hda The number of cylinders for this disk is set to 2498. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO ) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Command (m for help):

We get the list of available commands by command (since these commands are called by pressing the corresponding key, I will denote the commands by a letter enclosed in angle brackets):

Listing 7.

Command (m for help): m 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):

The most commonly used of these commands is perhaps the command

, which displays a complete list of partitions available on the disk (including logical ones). The result of executing this command is similar to the result obtained after executing the command
# /sbin/fdisk -l /dev/hda
from the command line, so you have already seen an example of executing this command (Listing 5). The partition boundaries in this case are shown as cylinder numbers. Generally speaking, in the process of getting acquainted with disk partitioning programs, one gets the impression that partition boundaries are best placed along the boundaries of the cylinders.

If you add utilities to the call fdisk option -u, then the partition boundaries will be specified by sector numbers. I assume (although I have not seen such a statement anywhere in the documentation) that this corresponds to linear sector numbering (LBA). You will also see the sector numbers when you execute the command

after running the utility itself in the following format:
# /sbin/fdisk -u /dev/hda
However, you can switch from one method of specifying partition boundaries to another at any time while working with the program, just give the keyboard command .

I will explain the purpose of some keyboard commands as briefly as they are given by the utility itself, but in Russian:

The last two commands require additional explanation. As you can see, the program developers have taken certain precautions to protect against rash user actions. Until you run the command , the changes you have made are not yet irreversible. You can always press a key and the partition table will remain intact. The program probably works with some temporary file - an MBR image, and only on command the contents of this file are written to the MBR.

By command We are moving to the “second level” of the program fdisk. As stated above, this level is for experts! A list of additional features available at this level can again be obtained by command (see Listing 8):

Listing 8.

Command (m for help): x Expert command (m for help): m Command action b move beginning of data in a partition c change number of cylinders d print the raw data in the partition table e list extended partitions f fix partition order g create an IRIX (SGI) partition table h change number of heads m print this menu p print the partition table q quit without saving changes r return to main menu s change number of sectors/track v verify the partition table w write table to disk and exit Expert command (m for help):

Some of these commands are already familiar to you -

, , , . They operate here in the same way as at the main level. Team - correct the order of partitions, you will have to use it in the case when you deleted partitions and created new ones. The fact is that after such operations, the numbering order of the partitions does not correspond to the order in which they are placed on the disk. Team allows us to correct this discrepancy. Team serves to display in hexadecimal the contents of the sectors in which the disk partition structure is stored. Using other second-level commands, you can change the number of cylinders (command ), heads (command ) and sectors (command ), that is, in fact, set a “false” disk geometry. But if you decide to do this, you must clearly understand where this leads (see the previous part of these notes). Moreover, one must imagine the consequences of such commands as - moving the beginning of the data in the section. I do not undertake to explain or add anything to what has been said, because I have not used these commands in relation to real situations. So I'll finish the story about the team fdisk and go to the utility cfdisk.

2.3. Program cfdisk

This utility is launched in the same format as other commands in this group:
# /sbin/fdisk /dev/hda

The device reference can be omitted if you want to change the partitioning on the /dev/hda drive (this drive is selected by default). After launch, the program tries to read the partition table of the specified disk. If for some reason it fails, an error message is displayed and program execution is terminated. One of the reasons may be incorrect information about the disk geometry, and in this case, you can make the program work if you specify the disk geometry in the call line. Another way is to run the program with the -z option. In this case, the existing disk partition table will be ignored.

Unlike fdisk program interface cfdisk built on the basis of a small menu located at the bottom of the screen (Fig. 1).

Fig.1. Program window cfdisk

The transition from one menu item to another is carried out using the key <Тab> or using the left/right arrow keys. The up/down arrow keys are used to select a disk partition from the list displayed in the center of the screen. For each partition, its name, flag values, partition type (primary or logical), file system type in the partition, partition label, and partition size are indicated. There may be no value in the Flags column, or it may be Boot (which means boot partition), or NC (“Not Compatible with DOS or OS/2” - not compatible with DOS or OS/2). The last flag has the following meaning. Some operating systems (including DOS and OS/2) partition the disk so that the first partition of the disk, as well as all logical partitions, begin on track two, leaving the first track of the cylinder empty. By setting the NC flag using the menu command Maximize or hotkey , you will be able to use this disk space. But keep in mind that in a running system it is not worth saving disk space in this way. I tried to get a couple of extra tracks this way, and my system only worked until the first reboot. And the next time I booted, a message about Kernel Panic appeared. Fortunately, after I unchecked this checkbox using the same program (which, of course, had to boot from the Knopix LiveCD), the system was restored.

Partition size in cfdisk can be shown in megabytes (default), kilobytes, or number of sectors or cylinders. Switching between these modes is carried out with the command Units or using a hotkey (in the latter case, switching is carried out in a cycle). If there is an asterisk after the size, this means that either the partition boundaries do not match the cylinder boundaries, or the wrong disk geometry is used. Linux doesn't use geometry when working with disks, so you shouldn't be afraid of this. But you can also change the geometry of the disk with which it will work cfdisk. There is a hotkey for this. . However, the program manual says that only those who know what they are doing can do this. For others, it’s better to trust the program to determine the geometry of the disk. cfdisk.

Using the command Delete or hotkey You can delete the section currently selected in the list of sections.

If the highlight in the list of sections is moved to free space, the menu appearance will change (see Fig. 2).


Fig.2. Create a new partition using cfdisk

A new partition is created using the command New or hotkey . If the partition type is not uniquely determined, the program will ask you to specify it. Remember that you can create a maximum of 4 primary partitions and that only one of them can be extended. Therefore, all logical partitions must be located sequentially on the disk and cannot be interleaved with primary ones. Then the program will ask you for the size of the partition to be created, and by default it will ask you to create it equal to the entire free space. If you are happy with this, you can simply press the key . Otherwise, you can enter the desired size, and the program accepts the size specified in megabytes (M), kilobytes (K), cylinders (C) and sectors (S). You must indicate the letters in brackets immediately after the number specifying the size (M can be omitted, this is the default value). The partition you create can be placed either at the beginning or at the end of the free disk space, and cfdisk will again ask you to make a decision on this matter.

You can view what the changes you make will lead to using the command Print (

). There are three possible options for displaying this result:

    the so-called raw data format, that is, the hexadecimal type of sectors that will be written to the first sectors of each partition;

    sector format (Fig. 3);

Fig.3. Partition table in sector format


For the changes made to take effect, you need to write the disk partition table, which is done using the command Write or hotkey (moreover, capital W). The program will ask you for confirmation and write the result to disk.

To exit the program use the command Quit or hotkey .

2.4. Program sfdisk

When launched without parameters, this utility produces only a two-page help page about startup options. But it is difficult to understand how to use this utility from this help alone. You should also look at the man sfdisk interactive help page. From this help we learn that the utility sfdisk has 4 main use cases.

Option 1. When launched with the option -s partition it gives the partition size in blocks. Parameter partition can point to a specific partition, for example /dev/hda2, or to the entire disk. In this case, the disk volume in blocks will be displayed. And if you leave only the option -s and do not specify a disk or partition, the volume of all partitions on each disk and the total volume of the disk will be displayed (see Listing 9).

Listing 9.

# /sbin/sfdisk -s /dev/hdb 1251936 # /sbin/sfdisk -s/dev/hda: 39082680 /dev/hda1: 530113 /dev/hda2: 2048287 /dev/hda3: 104422 /dev/hda4: 1 /dev/hda5: 1534176 /dev/hda6: 2048256 /dev/hda7: 12289693 /dev /hda8: 20523006 /dev/hda9: 4536 /dev/hdb: 1251936 /dev/hdb1: 128992 /dev/hdb2: 1121904

Option 2. Second option for using the utility sfdisk serves to obtain information about the disk partition table. To do this, run it with the option -l or with option -d. When launched with the option -l

# /sbin/sfdisk -l [options] /dev/hdb

utility sfdisk displays the partition table of the specified disk in approximately the same format as the utility fdisk(examples are given in). But if you run the program with the additional option -x, we will see the entire chain of logical partitions nested in the extended partition, that is, we will receive more detailed information about the partitions (see Listing 3).

Option -d(Listing 10) allows you to obtain information in a different format, focused on the use of the utility sfdisk to change the partition table (see below for a description of the fourth use case sfdisk).

Listing 10.

# /sbin/sfdisk -d /dev/hda Warning: extended partition does not start at a cylinder boundary.

DOS and Linux will interpret the contents differently.# partition table of /dev/hda unit: sectors /dev/hda1: start= 63, size= 1060227, Id=82 /dev/hda2: start= 1060290, size= 4096575, Id= b, bootable /dev/hda3: start= 5156865, size= 208845, Id=83 /dev/hda4: start= 5365710, size= 72799650, Id= f /dev/hda5: start= 5365773, size= 3068352, Id=83 /dev/hda6: start= 8434188, size= 4096512, Id=83 /dev/hda7: start= 12530763, size= 24579387, Id=83 /dev/hda8: start= 37110213, size= 41046012, Id= c sfdisk Option 3. Third launch option determined by using the option -l-V

, and serves to test the partition table on a specific device. This option is probably best used in conjunction with the option, then you will see both the partition table and information about incorrectness in this table.

Option 4.

# /sbin/sfdisk /dev/hdb/dev/hdb1:
and expects you to enter partition parameters in the following format<номер начального сетора> <размер(в секторах)> <тип(id)> Please note that this is actually the same format that is used by the program to output the partition table when it was launched with the option -d(See Listing 10). Therefore, if you want to fix something in an existing partition table, it makes sense to first use the option -d, in order to create a plan for disk repartition based on the received data.

Each line of input like the above defines one section. Fields are separated by spaces. You can use commas or semicolons as delimiters. Numbers can be decimal, octal or hexadecimal. Fields , specifying the section in the “cylinder, head, sector” format may be absent (the man page even recommends not setting these values), since the utility can calculate them independently. The boot partition is marked with an asterisk in the corresponding field. However, if you do not have systems other than Linux installed, you don’t have to specify the boot partition, since Linux does not use this label.

The section type (Id field) is specified in hexadecimal code without adding a prefix 0x, or simply one of the following characters: S- Linux_Swap (82), L- Linux_Native (83), E- Extended (5) or X- Linux_Extended (85).

When creating a partition table, you can limit yourself to entering only the first two fields - the rest will be specified by default.

In the fourth launch option, the utility sfdisk requires an explicit indication of the device with which it will work. If we specify a mounted device as a target (as most often happens on a running system), we will receive the following message:

Listing 12.

# /sbin/sfdisk /dev/hda Checking that no-one is using this disk right now ... BLKRRPART: Device or resource busy This disk is currently in use - repartitioning is probably a bad idea. Umount all file systems, and swapoff all swap partitions on this disk. Use the --no-reread flag to suppress this check. Use the --force flag to overrule all checks.

So it's best to run sfdisk on a system like Knoppix, or apply it to a second hard drive after unmounting it (note that fdisk in a similar situation it starts without objection). However, if you are confident in the correctness of your decisions, you can make the utility work by specifying the option -f("Do what you're told and don't hesitate!"), or simply override the check that the disk is currently in use by specifying the option --no-reread.

As you can see, the utility sfdisk requires thinking in advance about the structure of the partitions created on the disk. As a precaution, you can save the existing partition table to disk with the command

# /sbin/sfdisk /dev/hda -O hda-part.save

In this case, before writing the modified table to disk, the old contents of the sectors that define the disk partition will be saved in the hda-part.save file. If you make any mistake while making changes, you can restore the original state using the command

# /sbin/sfdisk /dev/hda -I hda-part.save

Note that using options -O V -I is not equivalent to simply saving the partition table. Information saved when using the option -O, is a binary version of the output produced by the option -d.

Using the utility sfdisk You can only change one partition on the disk using the option -N.

After reading the above descriptions of the three main disk partitioning utilities that exist in Linux, you can choose the most suitable one for yourself. Let me just note once again that all these utilities do not allow you to repartition a disk while saving information on existing partitions. So if we are talking about disk repartitioning, you will have to take care to save your data on some kind of backup media. Recently, utilities have appeared that allow you to repartition a disk without losing information (at least that’s what they say). And I will talk about such utilities, but a little later, after I give a brief description of the utilities for creating partitions included in several Linux distributions.

V.A. Kostromin - 2. The fdisk utility and its closest relatives

fdisk is the general name for system utilities for managing hard disk partitions. They are widespread and available on almost any operating system, but they work differently. Use a text-based user interface.

Warning: Don't create, delete or modify partitions unless you know what you're doing!

FDISK utility in Unix/Linux

With FDISK utility you can view, create, resize, delete, copy and move partitions on your hard drive. And I will give clear examples of use.

Viewing partitions in Unix/Linux

Let's start with the easiest thing - viewing sections. Use the "-l" option with the FDISK command to view available partitions on Linux (on Unix systems - this option may not work. Tested on Mac OS X). All available partitions in Linux are displayed by the name of their device. For example: /dev/sda, /dev/sdb or /dev/sdc.

So, let's run the command:

The conclusion we get is the following:

You can find out some useful information about a specific block device. For example, the following command will display all partitions for /dev/sda:

If you have different device names, simply write the device name as /dev/sdb or /dev/sdc and so on.

Displaying available commands via FDISK

If you would like to see all the commands that are available for FDISK, then simply use the following command by mentioning the name of the hard drive, for example /dev/sda, as shown below:

output all available commands for FDISK for example for /dev/sda

Type 'm' to see a list of all available FDISK commands that will help with the /dev/sda hard drive. After entering 'm' on the screen, you will see all the available options for FDISK that you can use for the /dev/sda device (this device is taken as an example):

WARNING: DOS-compatible mode is not recommended. Highly recommended
turn off the mode (command 'c') and change units for the sector (command 'u').

Valid options when working with fdisk

  • a:- This option is used to toggle the boot flag.
  • b:- This option is used to edit the BSD disk label.
  • c:- This option is used to toggle the dos compatibility flag.
  • d:- This option is used to delete a partition.
  • g:- This option creates a new, empty GPT partition table.
  • G:- This option is used to create an IRIX partition table (SGI).
  • l:- This option is used to display known partition types (Linux swap, Linux, GPT, Solaris, and so on).
  • m:- This option is used to display available commands (menus).
  • n:- This option is used to add a new section.
  • o:- This option is used to create a new empty DOS partition table.
  • p:- This option is used to display partition tables.
  • q:- This option is used to exit the menu but without saving any changes.
  • s: - This option creates a new and empty Sun disk label.
  • t:- This option is used to change the type of “System ID” of the partition (you can take it from the output of the “l” option).
  • u:- This option is used to change display/input blocks.
  • v:- This option is used to check partition tables.
  • w:- This option is used to record everything changed and then exit.
  • x:- This option is used to call additional functionality (for experts only).

Displaying all partitions (sections) in Unix/Linux

To list your block device's partition table, use:

# fdisk /dev/sda

After which, you find yourself in a menu (you can perform various operations with partitions in it), we are only interested in viewing, so we enter the “p” option:

Create a partition table (disklabel) in Unix/Linux

If you have an empty block device and you want to make markup on it for installing a future OS, then first you should select the future markup.

Here are the main tables for marking:

  • MBR (msdos)

To create your block device's partition table, use:

# fdisk /dev/sda

This action selected the block device /dev/sda and to create a new partition table on it, use one of the options:

  • g - To create a new, empty GPT partition table.
  • o - To create a new, empty DOS partition table.

And let's move on to creating sections.

Create a new partition in Unix/Linux

And so, let’s say you have free space on one of the devices (for me it’s /dev/sda) and you need to create a new partition (for example, to install the OS). Then you should run the utility as follows (Type the following command to enter the control mode of a specific block device):

# fdisk /dev/sda

After entering command mode, press "n" to create a new partition in /dev/sda with a specific size.

After pressing "n", you are asked to select the type of the future partition:

  • p — For " primary", i.e. create a primary partition.
  • e - For “extended”, i.e. create an extended partition.

I would like to note the following that up to 4 “primary” partitions can be created, and all the rest will be “extended” (i.e. extended partitions). Since for me this is only the 1st section, I select the “p” option. As I said earlier, this is my first, primary partition and for it, I must indicate the number (partition number), logically indicate 1 (But you can choose 1-4, because there are only 4). To create a full-fledged partition, you need to specify the sector (the beginning of the cylinder position for the partition being created), they tell me what it will be - 2048 (selected the standard value), and also, you need to indicate where this sector will end (the end of the cylinder position for the partition being created) - I am offered to indicate the standard value, but it does not suit me, because it will create 1 partition of maximum length (at For me it's 21 GB - test machine). At this stage, you can indicate the end of the sector in kilobytes (K), megabytes (M), gigabytes (G). Suppose I want to create the first 5GB partition, then I should enter “+5G” (or “+5000M” and so on).

You can also create several sections. The actions will be the same.

Please keep in mind that after creating a new partition, you must run the 'w' command for all actions to be saved. Well, after that, restart the OS to make sure that the partition has been created.

Formatting partitions in Unix/Linux

After the partitions have been created, formatting must be done. But before moving on to this action, you need to decide on the file system (FS).

I will use the mkfs utility to format the partition. This utility supports the following FS:

  • ext2: # mkfs.ext2 /dev/sda1
  • ext3: # mkfs.ext3 /dev/sda4
  • ext4: # mkfs.ext4 /dev/sda4
  • fat: # mkfs.fat /dev/sda4
  • minix: # mkfs.minix /dev/sda4
  • msdos: # mkfs.msdos /dev/sda4
  • vfat: # mkfs.vfat /dev/sda4
  • xfs: # mkfs.xfs /dev/sda4

We select the type of FS and proceed to pharmaceutical preparation.

Check partition size in Unix/Linux

After formatting a new partition, you can check the size of the partition using the “s” flag and the FDISK command. This way you can check the size of any specific device. For example:

# fdisk -s /dev/sda3 5285413

The size is displayed in blocks.

To output in gigabytes (GB), use:

# echo "`fdisk -s /dev/sda2`/(2^20)" | bc # echo "`fdisk -s /dev/sda1`/(2^20)" | bc # echo "`fdisk -s /dev/sda`/(2^20)" | bc

Looks usable.

Enable/Disable loading flag (*) from partition in Unix/Linux

When marking, I usually allocate 100MB for the partition in which the bootloader will be stored. And for this purpose, it is necessary to indicate that it will be downloaded from it. And therefore, you need to indicate the desired section. Let's start:

# fdisk /dev/sda

The first step is to view the available partitions to see the current partition table. As you remember, this is the “p” option. In my screenshot below, you can see that the boot partition is on the /dev/sda1 disk (you can see that this is the boot flag - the asterisk ‘*’), as shown below:

To enable the boot flag, use the “a” option. After clicking, you will need to enter the number of the partition that will be loaded (for example, 1st) and put everything back in place.

Delete a partition in Unix/Linux

If you would like to remove a specific partition (i.e. /dev/sda4) from a specific block device (i.e. - /dev/sda). You must do:

# fdisk /dev/sda

Then, enter 'd' to enter the delete partition menu. After entering, they ask you to enter the number of the section that I want to delete. Suppose I need to delete the 4th partition, then I enter “4”. That's it, the section has been deleted. Enter 'w' to record all changes! New changes will only work after the next system reboot. This can be easily understood from the below output.

Warning: Be careful when performing this step because using the 'd' option will completely remove the partition from the system and you may lose all data on the partition!

Using fdisk in bash scripts

If you know the commands well, you can write a small bash script or simply use this construction in the shell shell, which would emulate the user’s work:

Fdisk /dev/hda<< EOF n p 1 a 1 t c w EOF

I will not explain what these letters do, because I described all the options above.

The topic is "How to use FDISK command on Linux", I tried my best to cover almost all the basic commands of fdisk, but still FDISK contains many other commands you can use them by typing "x". For more details, enter the command 'man Fdisk' in a terminal.