GNU/Linux >> Znalost Linux >  >> Linux

Příkaz Fdisk v Linuxu (Správa oddílů)

Fdisk je textový nástroj používaný v Linuxu ke správě diskových oddílů. Pomocí fdisk můžete zobrazit všechny diskové oddíly, vytvořit nový oddíl, odstranit existující oddíl pevného disku a zobrazit velikost oddílu.

Pomocí fdisk můžete vytvořit maximálně čtyři primární oddíly a libovolný počet logických oddílů v závislosti na velikosti disku. Mějte na paměti, že každý jednotlivý oddíl vyžaduje minimální velikost 40 MB.

V tomto tutoriálu se naučíme, jak používat příkaz fdisk v linuxu s několika příklady.

1) Zobrazit všechny existující diskové oddíly

Než vytvoříte nový oddíl nebo upravíte existující oddíl, možná budete chtít zobrazit všechny dostupné oddíly v systému.

Příkaz zobrazí seznam oddílů ze všech připojených pevných disků. Pokud máte v systému více než jeden disk, seznam oddílů je uspořádán podle názvu zařízení /dev. Například /dev/sda, /dev/sdb, /dev/sdc a tak dále.

Použijte fdisk -l pro zobrazení všech dostupných oddílů následovně:

# 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) Zobrazení oddílů konkrétního pevného disku

Chcete-li zobrazit všechny oddíly pevného disku '/dev/sda', proveďte následující.

# fdisk -l /dev/sda

Zobrazit všechny příkazy fdisk Pomocí příkazu fdisk m

Použijte příkaz fdisk m , pro zobrazení všech dostupných příkazů fdisk, jak je uvedeno níže:

# 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) Smažte oddíl pevného disku

Předpokládejme, že chcete kombinovat několik oddílů (například /dev/sda6, /dev/sda7 a /dev/sda8) do jednoho diskového oddílu. Chcete-li to provést, měli byste nejprve odstranit všechny tyto jednotlivé oddíly, jak je znázorněno níže.

# 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) Vytvořte nový diskový oddíl

Jakmile smažete všechny existující oddíly, můžete vytvořit nový oddíl s využitím veškerého dostupného místa, jak je znázorněno níže.

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

Po vytvoření oddílu jej naformátujte pomocí příkazu mkfs, jak je uvedeno níže.

# mkfs.ext3 /dev/sda7

5) Zobrazení velikosti existujícího oddílu

Jak je uvedeno níže, fdisk -s zobrazí velikost oddílu v blocích.

V následujícím příkladu ukazuje velikost '/dev/sda7' jako 4900 MB.

# fdisk -s /dev/sda7
4843566

6) Přepněte příznak spouštění oddílu

Příkaz Fdisk zobrazí spouštěcí příznak každého oddílu. Pokud chcete zakázat nebo povolit spouštěcí příznak na odpovídajícím oddílu, proveďte následující.

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

Jak je vidět výše, příznak spouštění je na oddílu '/dev/sda5' zakázán.

7) Opravte pořadí tabulky oddílů

Když odstraníte logický oddíl a znovu jej vytvoříte, můžete zaznamenat problém „oddíl mimo provoz“. tj. chybová zpráva „Položky tabulky oddílů nejsou v pořadí disků“.

Když například odstraníte tři logické oddíly (sda6, sda7 a sda8) a vytvoříte nový oddíl, můžete očekávat, že název nového oddílu bude sda6. Systém však mohl vytvořit nový oddíl jako sda7. Je to proto, že po odstranění oddílů byl oddíl 'sda9' přesunut jako sda6 a volné místo se přesune na konec.

Chcete-li vyřešit tento problém s pořadím oddílů a přiřadit sda6 k nově vytvořenému oddílu, spusťte expertní příkaz f jak je uvedeno níže.

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

Jakmile bude pořadí tabulky oddílů opraveno, již se vám nebude zobrazovat chybová zpráva „Položky tabulky oddílů nejsou v pořadí disků“.

# 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

Závěr

V tomto tutoriálu jsme se naučili používat příkaz fdisk v Linuxu ke správě oddílů. Doufám, že se vám čtení líbilo a zanechte prosím svůj návrh v poli komentářů níže. Poznámka:Neodstraňujte, neupravujte ani nepřidávejte oddíl, pokud nevíte, co děláte. Můžete skončit ztrátou dat. Buďte opatrní.


Linux
  1. Jak rozdělit disk v Linuxu

  2. Jak zálohovat nebo klonovat oddíly Linuxu pomocí příkazu „cat“.

  3. Jak použít fdisk k rozdělení disku v Linuxu

  1. Jak odstranit oddíl v Linuxu

  2. Jak vytvořit oddíly v Linuxu

  3. Jak vytvořit diskové oddíly pomocí příkazu Parted v Linuxu

  1. 5 nejlepších nástrojů pro sledování využití disku a oddílů Linuxu

  2. Příkaz Fdisk v Linuxu (Vytvoření diskových oddílů)

  3. Jak klonovat disky pomocí příkazu Linux dd