fdisk是linux下一个很实用的分区命令:

查看所有磁盘分区情况

[root@erpdevdb /]# fdisk -l

Disk /dev/sda: 1048.5 GB, 1048576000000 bytes
255 heads, 63 sectors/track, 127482 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 65 522081 83 Linux
/dev/sda2 66 2676 20972857+ 8e Linux LVM
/dev/sda3 2677 5287 20972857+ 8e Linux LVM
/dev/sda4 5288 127482 981531337+ 5 Extended
/dev/sda5 5288 7898 20972826 8e Linux LVM
/dev/sda6 7899 20400 100422283+ 8e Linux LVM

Disk /dev/sdb: 1198.8 GB, 1198899855360 bytes
255 heads, 63 sectors/track, 145757 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 37349 300005811 83 Linux

从以上输出我们可以看到,目前磁盘是/dev/sda和/dev/sdb两块,当然这里看到的不是物理盘数,一般是做过raid后重新划分好的。其中/dev/sda已经有了1到6共6个分区,而/dev/sdb只有一个分区。还可以看到起始位置、块数、分区类型等信息。

下面我们填加一个新的LVM分区:

[root@erpdevdb /]# fdisk /dev/sda

The number of cylinders for this disk is set to 127482.
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: 1048.5 GB, 1048576000000 bytes
255 heads, 63 sectors/track, 127482 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 65 522081 83 Linux
/dev/sda2 66 2676 20972857+ 8e Linux LVM
/dev/sda3 2677 5287 20972857+ 8e Linux LVM
/dev/sda4 5288 127482 981531337+ 5 Extended
/dev/sda5 5288 7898 20972826 8e Linux LVM
/dev/sda6 7899 20400 100422283+ 8e Linux LVM

Command (m for help): n
First cylinder (20401-127482, default 20401):
Using default value 20401
Last cylinder or +size or +sizeM or +sizeK (20401-127482, default 127482): +102400M

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.
Syncing disks.

[root@erpdevdb /]# fdisk -l

Disk /dev/sda: 1048.5 GB, 1048576000000 bytes
255 heads, 63 sectors/track, 127482 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 65 522081 83 Linux
/dev/sda2 66 2676 20972857+ 8e Linux LVM
/dev/sda3 2677 5287 20972857+ 8e Linux LVM
/dev/sda4 5288 127482 981531337+ 5 Extended
/dev/sda5 5288 7898 20972826 8e Linux LVM
/dev/sda6 7899 20400 100422283+ 8e Linux LVM
/dev/sda7 20401 32850 100004593+ 83 Linux

Disk /dev/sdb: 1198.8 GB, 1198899855360 bytes
255 heads, 63 sectors/track, 145757 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 37349 300005811 83 Linux

可以看到,fdisk命令可以针对某个盘进行分区操作,先进入到命令行的形式,几个常用的命令如下:

Command (m for help):
d delete a partition 删除一个分区
l list known partition types 列出分区类型
m print this menu 列出帮助
n add a new partition 添加一个分区
p print the partition table 列出分区表
q quit without saving changes 不保存退出
t change a partition's system id 改变分区类型
w write table to disk and exit 把分区表写入硬盘并退出

注意,这里虽然通过fdisk -l能看到新增的分区,但是要重启后才能写入分区表,比如这时要创建一个PV(物理卷)会提示找不到分区:

[root@erpdevdb /]# pvcreate /dev/sda7
Device /dev/sda7 not found (or ignored by filtering).

改变新加的分区/dev/sda7的类型:

[root@erpdevdb /]# fdisk /dev/sda

The number of cylinders for this disk is set to 127482.
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): t
Partition number (1-7): 7
Hex code (type L to list codes): L

0 Empty 1e Hidden W95 FAT1 75 PC/IX be Solaris boot
1 FAT12 24 NEC DOS 80 Old Minix bf Solaris
2 XENIX root 39 Plan 9 81 Minix / old Lin c1 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 82 Linux swap c4 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 83 Linux c6 DRDOS/sec (FAT-
5 Extended 41 PPC PReP Boot 84 OS/2 hidden C: c7 Syrinx
6 FAT16 42 SFS 85 Linux extended da Non-FS data
7 HPFS/NTFS 4d QNX4.x 86 NTFS volume set db CP/M / CTOS / .
8 AIX 4e QNX4.x 2nd part 87 NTFS volume set de Dell Utility
9 AIX bootable 4f QNX4.x 3rd part 8e Linux LVM df BootIt
a OS/2 Boot 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 hi 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 b
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 Wizard hid ff BBT
1c Hidden W95 FAT3
Hex code (type L to list codes): 8e
Changed system type of partition 7 to 8e (Linux LVM)

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.
Syncing disks.

再查看,分区/dev/sda7类型已经变为8e (Linux LVM)

[root@erpdevdb /]# fdisk -l

Disk /dev/sda: 1048.5 GB, 1048576000000 bytes
255 heads, 63 sectors/track, 127482 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sda1 * 1 65 522081 83 Linux
/dev/sda2 66 2676 20972857+ 8e Linux LVM
/dev/sda3 2677 5287 20972857+ 8e Linux LVM
/dev/sda4 5288 127482 981531337+ 5 Extended
/dev/sda5 5288 7898 20972826 8e Linux LVM
/dev/sda6 7899 20400 100422283+ 8e Linux LVM
/dev/sda7 20401 32850 100004593+ 8e Linux LVM

Disk /dev/sdb: 1198.8 GB, 1198899855360 bytes
255 heads, 63 sectors/track, 145757 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/sdb1 1 37349 300005811 83 Linux

重启系统后,新的分区就能正常使用了:

[root@erpdevdb /]# reboot

Broadcast message from root (pts/1) (Tue Apr 14 17:02:59 2009):

The system is going down for reboot NOW!

新的分区一般要格式化为想要的文件系统并加载才能使用,以下是一个例子,格式化为linux下最常用的ext3文件系统:

[root@localhost orasoft]# mkfs.ext3 /dev/oraclevg/oracle
mke2fs 1.35 (28-Feb-2004)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
1310720 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
80 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632

Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost orasoft]# mkdir /u01
[root@localhost orasoft]# mount /dev/oraclevg/oracle /u01

 

One Response to Linux下磁盘分区命令fdisk的使用方法

  1. [...] 1、 创建分区 使用分区工具(如:fdisk等)创建LVM分区,方法和创建其他一般分区的方式是一样的,区别仅仅是LVM的分区类型为8e。具体操作步骤可参阅《Linux下磁盘分区命令fdisk的使用方法》 [...]

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>