磁盘分区是将一个硬盘驱动器划分为若干个逻辑驱动器,它是将硬盘上连续的区块当作一个独立的磁盘使用。分区表是一个硬盘分区的索引,分区的信息都会写进分区表。
使用多个分区的好处主要有:
1 防止数据丢失,若系统中只有一个分区,那么如果这个分区损坏,用户将丢失所有的数据。
2 增加磁盘使用率,可以使用不同的区块大小来格式化分区,若磁盘区块大小为4K,但有很多1K的小文件,那么每存储一个文件将浪费3K空间。
3 将系统盘与数据盘分开,这可以避免如果由于数据太多导致数据盘撑满时,系统盘仍然可用,不影响操作系统正常工作。
Linux使用fdisk这个命令来实现磁盘分区的功能,fdisk的主要参数如下,
p、打印分区表。
n、新建一个新分区。
d、删除一个分区。
q、退出不保存。
w、把分区写进分区表,保存并退出。
今天碰巧在搭建集群环境时遇到需要对磁盘分区的场景,下面主要介绍我的磁盘分区步骤。
1 df -h只能看到操作系统盘看不到数据盘,使用fdisk -l可以看到额外6块盘。
[root@esgyn003 ~]# fdisk -l
WARNING: GPT (GUID Partition Table) detected on '/dev/sdb'! The util fdisk doesn't support GPT. Use GNU Parted.
Disk /dev/sdb: 1200.2 GB, 1200210141184 bytes
255 heads, 63 sectors/track, 145917 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 262144 bytes / 262144 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sdb1 1 145918 1172080215+ ee GPT
Partition 1 does not start on physical sector boundary.
WARNING: GPT (GUID Partition Table) detected on '/dev/sdc'! The util fdisk doesn't support GPT. Use GNU Parted.
Disk /dev/sdc: 1200.2 GB, 1200210141184 bytes
255 heads, 63 sectors/track, 145917 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 262144 bytes / 262144 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sdc1 1 145918 1172080215+ ee GPT
Partition 1 does not start on physical sector boundary.
WARNING: GPT (GUID Partition Table) detected on '/dev/sda'! The util fdisk doesn't support GPT. Use GNU Parted.
Disk /dev/sda: 1200.2 GB, 1200210141184 bytes
255 heads, 63 sectors/track, 145917 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 262144 bytes / 262144 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sda1 1 145918 1172080215+ ee GPT
Partition 1 does not start on physical sector boundary.
WARNING: GPT (GUID Partition Table) detected on '/dev/sde'! The util fdisk doesn't support GPT. Use GNU Parted.
Disk /dev/sde: 1200.2 GB, 1200210141184 bytes
255 heads, 63 sectors/track, 145917 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 262144 bytes / 262144 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sde1 1 145918 1172080215+ ee GPT
Partition 1 does not start on physical sector boundary.
WARNING: GPT (GUID Partition Table) detected on '/dev/sdd'! The util fdisk doesn't support GPT. Use GNU Parted.
Disk /dev/sdd: 1200.2 GB, 1200210141184 bytes
255 heads, 63 sectors/track, 145917 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 262144 bytes / 262144 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sdd1 1 145918 1172080215+ ee GPT
Partition 1 does not start on physical sector boundary.
WARNING: GPT (GUID Partition Table) detected on '/dev/sdg'! The util fdisk doesn't support GPT. Use GNU Parted.
Disk /dev/sdg: 1200.2 GB, 1200210141184 bytes
255 heads, 63 sectors/track, 145917 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 262144 bytes / 262144 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sdg1 1 145918 1172080215+ ee GPT
Partition 1 does not start on physical sector boundary.
WARNING: GPT (GUID Partition Table) detected on '/dev/sdf'! The util fdisk doesn't support GPT. Use GNU Parted.
Disk /dev/sdf: 1200.2 GB, 1200210141184 bytes
255 heads, 63 sectors/track, 145917 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 262144 bytes / 262144 bytes
Disk identifier: 0x00000000
Device Boot Start End Blocks Id System
/dev/sdf1 1 145918 1172080215+ ee GPT
Partition 1 does not start on physical sector boundary.
Disk /dev/mapper/VolGroup-lv_root: 1182.3 GB, 1182290411520 bytes
255 heads, 63 sectors/track, 143738 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 262144 bytes / 262144 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/VolGroup-lv_swap: 17.2 GB, 17179869184 bytes
255 heads, 63 sectors/track, 2088 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 262144 bytes / 262144 bytes
Disk identifier: 0x00000000
2 使用fdisk对每块盘进行磁盘分区,以下使用一块盘作为例子
fdisk /dev/sdb
Command (m for help): d
Selected partition 1
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-145917, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-145917, default 145917):
Using default value 145917
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
3 格式化每块磁盘
[root@esgyn003 ~]# mkfs.ext4 /dev/sdc1
mke2fs 1.41.12 (17-May-2010)
/dev/sdc1 alignment is offset by 229888 bytes.
This may result in very poor performance, (re)-partitioning suggested.
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=64 blocks, Stripe width=64 blocks
73261056 inodes, 293019567 blocks
14650978 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
8943 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 27 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
4 创建目录并使用mount挂载每块磁盘
mkdir /data1
mount /dev/sdb1 /data1
5 修改fstab使修改永久生效
ll /dev/disk/by-uuid/ | grep sdb1 | awk '{print "UUID="$9"\t/data1\text4\tdefaults\t0 0"}' >> /etc/fstab
ll /dev/disk/by-uuid/ | grep sdc1 | awk '{print "UUID="$9"\t/data2\text4\tdefaults\t0 0"}' >> /etc/fstab
ll /dev/disk/by-uuid/ | grep sdd1 | awk '{print "UUID="$9"\t/data3\text4\tdefaults\t0 0"}' >> /etc/fstab
ll /dev/disk/by-uuid/ | grep sde1 | awk '{print "UUID="$9"\t/data4\text4\tdefaults\t0 0"}' >> /etc/fstab
ll /dev/disk/by-uuid/ | grep sdf1 | awk '{print "UUID="$9"\t/data5\text4\tdefaults\t0 0"}' >> /etc/fstab
ll /dev/disk/by-uuid/ | grep sdg1 | awk '{print "UUID="$9"\t/data6\text4\tdefaults\t0 0"}' >> /etc/fstab