当前位置: > CentOS > CentOS入门 >

CentOS使用fdisk进行分区

时间:2014-12-15 01:47来源:linux.it.net.cn 作者:IT

1.fdisk -l 列出所有的分区
列如:
[root@xiancentos ~]# fdisk -l

Disk /dev/hdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hdb1 * 1 13 104391 83 Linux
/dev/hdb2 14 523 4096575 83 Linux
/dev/hdb3 524 905 3068415 83 Linux
/dev/hdb4 906 1044 1116517+ 5 Extended
/dev/hdb5 906 1032 1020096 82 Linux swap
[root@xiancentos ~]#

从这些信息中我们的磁盘hdb 总大小8589MB,总柱面有1044个(cylinder),最后一个分区,分区5使用到1032个
说明我们还有1044-1032*8225280 = 94 MB的大小没有用完,你可能不理解,没关系,首先hdb1,hdb2,hdb3都是主分区
hdb4是扩展分区 hdb5是逻辑分区,扩展分区包括逻辑分区。

2.开始对94MB的空间进行分区
使用fdisk /dev/hdb
例:
[root@xiancentos ~]# fdisk /dev/hdb

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

Command (m for help): n
First cylinder (1033-1044, default 1033): 1044

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: 设备或资源忙.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.

[root@xiancentos ~]# fdisk -l

Disk /dev/hdb: 8589 MB, 8589934592 bytes
255 heads, 63 sectors/track, 1044 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/hdb1 * 1 13 104391 83 Linux
/dev/hdb2 14 523 4096575 83 Linux
/dev/hdb3 524 905 3068415 83 Linux
/dev/hdb4 906 1044 1116517+ 5 Extended
/dev/hdb5 906 1032 1020096 82 Linux swap
/dev/hdb6 1044 1044 8032+ 83 Linux

我们成功的进行了分区 /dev/hdb6 当然在分区填写大小的时候我们也可以使用+100M,重新启动机器(或执行partprobe命令)

3.接下来,我们要对新的分区进行格式化,格式化我们所需要的文件系统
使用mk2fs命令
[root@xiancentos ~]# ls /sbin/mk*
mkbootdisk mkdosfs mke2fs mkfs mkfs.cramfs mkfs.ext4 mkfs.ext3 mkfs.msdos mkfs.vfat mkinitrd mkswap mkzonedb

mkfs -t ext2 和 mke2fs 还有mkfs.ext2都一样 格式化成ext2文件系统

对/dev/hdb6进行格式化

[root@xiancentos ~]# mkfs.ext4 /dev/hdb6
Filesystem label=test
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
2008 inodes, 8032 blocks
401 blocks (4.99%) reserved for the super user
First data block=1
Maximum filesystem blocks=8388608
1 block group
8192 blocks per group, 8192 fragments per group
2008 inodes per group

Writing inode tables: done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@xiancentos ~]#

使用dumpe2fs 查看分区信息
例:
[root@xiancentos ~]# dumpe2fs /dev/hdb6
dumpe2fs 1.35 (28-Feb-2004)
Filesystem volume name: test
Last mounted on:  
Filesystem UUID: 98b678dd-c6ef-4617-b02f-48105e0ef2d5
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: resize_inode filetype sparse_super
Default mount options: (none)
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 2008
Block count: 8032
Reserved block count: 401
Free blocks: 7731
Free inodes: 1997
First block: 1
Block size: 1024
Fragment size: 1024
Reserved GDT blocks: 31
Blocks per group: 8192
Fragments per group: 8192
Inodes per group: 2008
Inode blocks per group: 251
Filesystem created: Sat Aug 6 14:32:57 2011
Last mount time: n/a
Last write time: Sat Aug 6 14:32:57 2011
Mount count: 0
Maximum mount count: 39
Last checked: Sat Aug 6 14:32:57 2011
Check interval: 15552000 (6 months)
Next check after: Thu Feb 2 14:32:57 2012
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 128
Default directory hash: tea
Directory Hash Seed: 3abb5a31-64ef-4cd8-813f-b3c95a573b9f

Group 0: (Blocks 1-8031)
Primary superblock at 1, Group descriptors at 2-2
Block bitmap at 34 (+33), Inode bitmap at 35 (+34)
Inode table at 36-286 (+35)
7731 free blocks, 1997 free inodes, 2 directories
Free blocks: 301-8031
Free inodes: 12-2008
[root@xiancentos ~]#

4.对分区进行挂载
mount 命令
mount -t ext4 /dev/hdb6 /app 将分区挂载到/app下

例:
[root@xiancentos ~]# mount -t ext4 /dev/hdb6 /app
[root@xiancentos ~]# df -k
Filesystem 1K-块 已用 可用 已用% 挂载点
/dev/hdb2 4032124 2815956 1011340 74% /
/dev/hdb1 101086 8536 87331 9% /boot
none 257780 0 257780 0% /dev/shm
/dev/hdb3 3020172 39240 2827512 2% /home
/dev/hdb6 7776 45 7330 1% /app
[root@xiancentos ~]#

这样我们就可以使用我们的新分区了

5.使用umount 卸载 /dev/hdb6

[root@xiancentos ~]# umount /app
umount: /app: device is busy
umount: /app: device is busy
[root@xiancentos ~]#

使用fuser或lsof 查看/app 为什么busy

[root@xiancentos ~]# fuser /app
/app: 4344c
[root@xiancentos ~]# lsof /app/
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
bash 4344 root cwd DIR 3,70 1024 2 /app/
[root@xiancentos ~]#

查到是4344这个进程 将其kill掉
[root@xiancentos ~]# umount /app
[root@xiancentos ~]# df -k
Filesystem 1K-块 已用 可用 已用% 挂载点
/dev/hdb2 4032124 2815952 1011344 74% /
/dev/hdb1 101086 8536 87331 9% /boot
none 257780 0 257780 0% /dev/shm
/dev/hdb3 3020172 39240 2827512 2% /home
[root@xiancentos ~]# 

卸载成功

6.使用UUID实现开机自动挂载
查看设备UUID简单的方法:
ls -l /dev/disk/by-uuid/ 或者 blkid /dev/hd6
将自动挂载信息写入fstab
vi /etc/fstab
按照已有的格式对应写入即可,这样每次开机都会进行自动挂载了。




(责任编辑:IT)
------分隔线----------------------------
栏目列表
推荐内容