GNU/Linux >> Linux Esercitazione >  >> Linux

Comando Fdisk in Linux (Gestisci partizioni)

Fdisk è un'utilità basata su testo utilizzata in Linux per gestire le partizioni del disco. Usando fdisk puoi visualizzare tutte le partizioni del disco, creare una nuova partizione, eliminare una partizione del disco rigido esistente e visualizzare la dimensione della partizione.

Utilizzando fdisk è possibile creare un massimo di quattro partizioni primarie e un numero qualsiasi di partizioni logiche, in base alla dimensione del disco. Tieni presente che ogni singola partizione richiede una dimensione minima di 40 MB.

In questo tutorial, impariamo come usare il comando fdisk in Linux con alcuni esempi.

1) Visualizza tutte le partizioni del disco esistenti

Prima di creare una nuova partizione o modificare una partizione esistente, potresti voler visualizzare tutte le partizioni disponibili nel sistema.

Il comando elencherà le partizioni di tutti i dischi rigidi collegati. Quando hai più di un disco sul sistema, l'elenco delle partizioni viene ordinato in base al nome /dev del dispositivo. Ad esempio, /dev/sda, /dev/sdb, /dev/sdc e così via.

Usa fdisk -l per visualizzare tutte le partizioni disponibili come segue:

# 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) Visualizza le partizioni di un disco rigido specifico

Per visualizzare tutte le partizioni del disco rigido '/dev/sda', procedi come segue.

# fdisk -l /dev/sda

Visualizza tutti i comandi di fdisk Usando il comando di fdisk m

Usa il comando fdisk m , per visualizzare tutti i comandi fdisk disponibili come mostrato di seguito:

# 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) Elimina una partizione del disco rigido

Supponiamo che ti piaccia combinare più partizioni (ad esempio, /dev/sda6, /dev/sda7 e /dev/sda8) in un'unica partizione del disco. Per fare ciò, dovresti prima eliminare tutte quelle singole partizioni, come mostrato di seguito.

# 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) Crea una nuova partizione del disco

Dopo aver eliminato tutte le partizioni esistenti, puoi creare una nuova partizione utilizzando tutto lo spazio disponibile come mostrato di seguito.

# 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.

Dopo aver creato la partizione, formattala usando il comando mkfs come mostrato di seguito.

# mkfs.ext3 /dev/sda7

5) Visualizza la dimensione di una partizione esistente

Come mostrato di seguito, fdisk -s mostra la dimensione della partizione in blocchi.

Nell'esempio seguente mostra la dimensione '/dev/sda7' come 4900 MB.

# fdisk -s /dev/sda7
4843566

6) Attiva/disattiva il flag di avvio di una partizione

Il comando Fdisk mostra il flag di avvio di ciascuna partizione. Quando vuoi disabilitare o abilitare il flag di avvio sulla partizione corrispondente, procedi come segue.

# 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):

Come visto sopra, il flag di avvio è disabilitato sulla partizione '/dev/sda5'.

7) Correzione dell'ordine della tabella delle partizioni

Quando si elimina una partizione logica e la si ricrea di nuovo, è possibile che venga visualizzato il problema della "partizione non funzionante". ad es. messaggio di errore "Le voci della tabella delle partizioni non sono nell'ordine del disco".

Ad esempio, quando si eliminano tre partizioni logiche (sda6, sda7 e sda8) e si crea una nuova partizione, è possibile che il nome della nuova partizione sia sda6. Ma il sistema potrebbe aver creato la nuova partizione come sda7. Questo perché, dopo che le partizioni sono state eliminate, la partizione 'sda9' è stata spostata come sda6 e lo spazio libero è stato spostato alla fine.

Per risolvere questo problema di ordine della partizione e assegnare sda6 alla partizione appena creata, eseguire il comando esperto f come mostrato di seguito.

$ 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.

Una volta corretto l'ordine della tabella delle partizioni, non riceverai più il messaggio di errore "Le voci della tabella delle partizioni non sono nell'ordine del disco".

# 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

Conclusione

In questo tutorial, abbiamo imparato come usare il comando fdisk in Linux per gestire le partizioni. Spero che ti sia piaciuto leggere e per favore lascia il tuo suggerimento nella casella dei commenti qui sotto. Nota:non eliminare, modificare o aggiungere partizioni se non sai cosa stai facendo. Potresti finire per perdere i tuoi dati. Stai attento.


Linux
  1. Come partizionare un disco in Linux

  2. Come eliminare la partizione in Linux

  3. Come creare partizioni in Linux

  4. Come eseguire il backup o clonare le partizioni Linux utilizzando il comando "cat".

  5. Come usare fdisk per partizionare un disco in Linux

Come elencare le partizioni del disco in Linux

Come creare e gestire partizioni Linux utilizzando Parted

Come creare partizioni del disco su Linux

15 Esempi di comandi Linux sfdisk per gestire le partizioni del disco

Come visualizzare le partizioni del disco Linux (tabella delle partizioni)

Esempi di comandi fdisk in Linux