GNU/Linux >> Tutoriels Linux >  >> Linux

Commande Fdisk sous Linux (Gérer les partitions)

Fdisk est un utilitaire basé sur du texte utilisé sous Linux pour gérer les partitions de disque. À l'aide de fdisk, vous pouvez afficher toutes les partitions de disque, créer une nouvelle partition, supprimer une partition de disque dur existante et afficher la taille de la partition.

En utilisant fdisk, vous êtes autorisé à créer un maximum de quatre partitions principales et un nombre quelconque de partitions logiques, en fonction de la taille du disque. Gardez à l'esprit que toute partition nécessite une taille minimale de 40 Mo.

Dans ce tutoriel, apprenons à utiliser la commande fdisk sous Linux avec quelques exemples.

1) Afficher toutes les partitions de disque existantes

Avant de créer une nouvelle partition ou de modifier une partition existante, vous souhaiterez peut-être afficher toutes les partitions disponibles dans le système.

La commande listera les partitions de tous les disques durs connectés. Lorsque vous avez plusieurs disques sur le système, la liste des partitions est triée par le nom /dev du périphérique. Par exemple, /dev/sda, /dev/sdb, /dev/sdc et ainsi de suite.

Utilisez fdisk -l pour afficher toutes les partitions disponibles comme suit :

# fdisk -l

Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xf6edf6ed

Device Boot Start End Blocks Id System
/dev/sda1 1 1959 15735636 c W95 FAT32 (LBA)
/dev/sda2 1960 5283 26700030 f W95 Ext'd (LBA)
/dev/sda3 5284 6528 10000462+ 7 HPFS/NTFS
/dev/sda4 6529 9729 25712032+ c W95 FAT32 (LBA)
/dev/sda5 * 1960 2661 5638752 83 Linux
/dev/sda6 2662 2904 1951866 83 Linux
/dev/sda7 2905 3147 1951866 83 Linux
/dev/sda8 3148 3264 939771 82 Linux swap / Solaris
/dev/sda9 3265 5283 16217586 b W95 FAT32

2) Afficher les partitions d'un disque dur spécifique

Pour afficher toutes les partitions du disque dur '/dev/sda', procédez comme suit.

# fdisk -l /dev/sda

Afficher toutes les commandes fdisk à l'aide de la commande fdisk m

Utilisez la commande fdisk m , pour afficher toutes les commandes fdisk disponibles comme indiqué ci-dessous :

# fdisk /dev/sda

The number of cylinders for this disk is set to 9729.
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): 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)

3) Supprimer une partition de disque dur

Supposons que vous aimiez combiner plusieurs partitions (par exemple, /dev/sda6, /dev/sda7 et /dev/sda8) en une seule partition de disque. Pour ce faire, vous devez d'abord supprimer toutes ces partitions individuelles, comme indiqué ci-dessous.

# fdisk /dev/sda

The number of cylinders for this disk is set to 9729.
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): p

Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xf6edf6ed

Device Boot Start End Blocks Id System
/dev/sda1 1 1959 15735636 c W95 FAT32 (LBA)
/dev/sda2 1960 5283 26700030 f W95 Ext'd (LBA)
/dev/sda3 5284 6528 10000462+ 7 HPFS/NTFS
/dev/sda4 6529 9729 25712032+ c W95 FAT32 (LBA)
/dev/sda5 * 1960 2661 5638752 83 Linux
/dev/sda6 2662 2904 1951866 83 Linux
/dev/sda7 2905 3147 1951866 83 Linux
/dev/sda8 3148 3264 939771 82 Linux swap / Solaris
/dev/sda9 3265 5283 16217586 b W95 FAT32

Command (m for help): d
Partition number (1-9): 8

Command (m for help): d
Partition number (1-8): 7

Command (m for help): d
Partition number (1-7): 6

Command (m for help): w
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 or after you run partprobe(8) or kpartx(8)
Syncing disks.

4) Créer une nouvelle partition de disque

Une fois que vous avez supprimé toutes les partitions existantes, vous pouvez créer une nouvelle partition en utilisant tout l'espace disponible, comme indiqué ci-dessous.

# fdisk /dev/sda

The number of cylinders for this disk is set to 9729.
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): n
First cylinder (2662-5283, default 2662):
Using default value 2662
Last cylinder, +cylinders or +size{K,M,G} (2662-3264, default 3264):
Using default value 3264
In the above example, fdisk n command is used to create new partition with the specific size. While creating a new partition, it expects following two inputs.
Starting cylinder number of the partition to be create (First cylinder).
Size of the partition (or) the last cylinder number (Last cylinder, +cylinders or +size ).
Please keep in mind that you should issue the fdisk write command (w) after any modifications.
Command (m for help): w
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 or after you run partprobe(8) or kpartx(8)
Syncing disks.

Une fois la partition créée, formatez-la à l'aide de la commande mkfs comme indiqué ci-dessous.

# mkfs.ext3 /dev/sda7

5) Afficher la taille d'une partition existante

Comme indiqué ci-dessous, fdisk -s affiche la taille de la partition en blocs.

Dans l'exemple suivant, la taille '/dev/sda7' est de 4900 Mo.

# fdisk -s /dev/sda7
4843566

6) Basculer l'indicateur de démarrage d'une partition

La commande Fdisk affiche l'indicateur de démarrage de chaque partition. Lorsque vous souhaitez désactiver ou activer l'indicateur de démarrage sur la partition correspondante, procédez comme suit.

# fdisk /dev/sda

The number of cylinders for this disk is set to 9729.
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): p

Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xf6edf6ed

Device Boot Start End Blocks Id System
/dev/sda1 1 1959 15735636 c W95 FAT32 (LBA)
/dev/sda2 1960 5283 26700030 f W95 Ext'd (LBA)
/dev/sda3 5284 6528 10000462+ 7 HPFS/NTFS
/dev/sda4 6529 9729 25712032+ c W95 FAT32 (LBA)
/dev/sda5 * 1960 2661 5638752 83 Linux
/dev/sda6 3265 5283 16217586 b W95 FAT32
/dev/sda7 2662 3264 4843566 83 Linux

Partition table entries are not in disk order

Command (m for help): a
Partition number (1-7): 5

Command (m for help): p

Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xf6edf6ed

Device Boot Start End Blocks Id System
/dev/sda1 1 1959 15735636 c W95 FAT32 (LBA)
/dev/sda2 1960 5283 26700030 f W95 Ext'd (LBA)
/dev/sda3 5284 6528 10000462+ 7 HPFS/NTFS
/dev/sda4 6529 9729 25712032+ c W95 FAT32 (LBA)
/dev/sda5 1960 2661 5638752 83 Linux
/dev/sda6 3265 5283 16217586 b W95 FAT32
/dev/sda7 2662 3264 4843566 83 Linux

Partition table entries are not in disk order

Command (m for help):

Comme vu ci-dessus, le drapeau de démarrage est désactivé sur la partition '/dev/sda5'.

7) Corriger l'ordre des tables de partition

Lorsque vous supprimez une partition logique et que vous la recréez à nouveau, vous pouvez rencontrer le problème de "partition hors service". c'est-à-dire le message d'erreur "Les entrées de la table de partition ne sont pas dans l'ordre du disque".

Par exemple, lorsque vous supprimez trois partitions logiques (sda6, sda7 et sda8) et créez une nouvelle partition, vous pouvez vous attendre à ce que le nom de la nouvelle partition soit sda6. Mais, le système a peut-être créé la nouvelle partition en tant que sda7. En effet, après la suppression des partitions, la partition 'sda9' a été déplacée en tant que sda6 et l'espace libre est déplacé vers la fin.

Pour résoudre ce problème d'ordre de partition et attribuer sda6 à la partition nouvellement créée, exécutez la commande experte f comme indiqué ci-dessous.

$ fdisk /dev/sda

The number of cylinders for this disk is set to 9729.
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): p

Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xf6edf6ed

Device Boot Start End Blocks Id System
/dev/sda1 1 1959 15735636 c W95 FAT32 (LBA)
/dev/sda2 1960 5283 26700030 f W95 Ext'd (LBA)
/dev/sda3 5284 6528 10000462+ 7 HPFS/NTFS
/dev/sda4 6529 9729 25712032+ c W95 FAT32 (LBA)
/dev/sda5 * 1960 2661 5638752 83 Linux
/dev/sda6 3265 5283 16217586 b W95 FAT32
/dev/sda7 2662 3264 4843566 83 Linux

Partition table entries are not in disk order

Command (m for help): x

Expert command (m for help): f
Done.

Expert command (m for help): w
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 or after you run partprobe(8) or kpartx(8)
Syncing disks.

Une fois l'ordre de la table de partition fixé, vous n'obtiendrez plus le message d'erreur "Les entrées de la table de partition ne sont pas dans l'ordre du disque".

# fdisk -l

Disk /dev/sda: 80.0 GB, 80026361856 bytes
255 heads, 63 sectors/track, 9729 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk identifier: 0xf6edf6ed

Device Boot Start End Blocks Id System
/dev/sda1 1 1959 15735636 c W95 FAT32 (LBA)
/dev/sda2 1960 5283 26700030 f W95 Ext'd (LBA)
/dev/sda3 5284 6528 10000462+ 7 HPFS/NTFS
/dev/sda4 6529 9729 25712032+ c W95 FAT32 (LBA)
/dev/sda5 * 1960 2661 5638752 83 Linux
/dev/sda6 2662 3264 4843566 83 Linux
/dev/sda7 3265 5283 16217586 b W95 FAT32

Conclusion

Dans ce didacticiel, nous avons appris à utiliser la commande fdisk sous Linux pour gérer les partitions. J'espère que vous avez apprécié la lecture et s'il vous plaît laissez votre suggestion dans la boîte de commentaires ci-dessous. Remarque :ne supprimez, ne modifiez ou n'ajoutez pas de partition si vous ne savez pas ce que vous faites. Vous risquez de perdre vos données. Soyez prudent.


Linux
  1. Comment partitionner un disque sous Linux

  2. Comment supprimer une partition sous Linux

  3. Comment créer des partitions sous Linux

  4. Comment sauvegarder ou cloner des partitions Linux à l'aide de la commande 'cat'

  5. Comment utiliser fdisk pour partitionner un disque sous Linux

Comment répertorier les partitions de disque sous Linux

Comment créer et gérer des partitions Linux à l'aide de Parted

Comment créer des partitions de disque sous Linux

15 exemples de commande Linux sfdisk pour gérer les partitions de disque

Comment afficher les partitions de disque Linux (table de partition)

Exemples de commandes fdisk sous Linux