linux下分区(fdisk , parted ,逻辑卷)

@努力的小王  February 12, 2018

linux下fdisk命令操作磁盘详解--添加、删除、转换分区等,基本原理是修改分区表,硬盘的分区需要掌握的概念:

1.MBR

 MBR 被称为主引导记录,是整个硬盘最重要的区域,一旦MBR物理实体损坏,基本硬盘就报废
 
 MBR 有512个字节,可以分为两部分,第一部分有446字节,用于存放引导代码(bootloader)。第二部分有64字节,用于存放磁盘的分区表
 其中每个分区的信息用16字节(这也是一块硬盘最多四个主分区的原因),最后2个字节为保留位
 通常分区我们一般采用这样的分区方式:
   
   P + P + P + P    or    P + P + P + E

2.更多分区知识可参考: https://baike.baidu.com/item/linux%E7%A3%81%E7%9B%98%E5%88%86%E5%8C%BA/5203467?fr=aladdin

二. fdisk

fdisk常用参数:

d delete a partition 注:这是删除一个分区的动作;
l list known partition types 注:l 是列出分区类型,以供我们设置相应分区的类型;
m print this menu 注:m 是列出帮助信息;
n add a new partition 注:添加一个分区;
o create a new empty DOS partition table
p print the partition table 注:p 列出分区表;
q quit without saving changes 注:不保存退出;
s create a new empty Sun disklabel
t change a partition's system id 注:t 改变分区类型;
u change display/entry units
v verify the partition table
w write table to disk and exit 注:把分区表写入硬盘并退出;
x extra functionality (experts only) 注:扩展应用,专家功能;
其实我们常用的只有注有中文的,其它的功能我们不常用;x 扩展功能,也不是常用的;一般的情况下只要懂得 d l m p q t w 就行

Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux -----------逻辑分区
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 287 2097152 82 Linux swap / Solaris -------solaris 逻辑分区
Partition 2 does not end on cylinder boundary.
/dev/sda3 287 6528 50125824 8e Linux LVM -------这是我后面要写的逻辑卷分区
/dev/sda1 190M 36M 145M 20% /boot ------------boot分区

三.针对4TB及以上容量硬盘的分区方法

Parted 突破MBR 4个主分区限制,每个磁盘最多支持128个分区。支持大于2T的分区,最大卷可达18EB

Parted 命令分为两种模式:命令行模式和交互模式。
1、命令行模式: parted [option] device [command] ,该模式可以直接在命令行下对磁盘进行分区操作,比较适合编程应用。如:

parted /dev/sdb print –显示磁盘/dev/sdb分区。

2、交互模式:parted [option] device

parted /dev/sdb –进入交互模式,建议使用交互模式,尤其是对parted命令不是很熟悉的情况下

[root@mysever2 ~]# parted /dev/sdb
GNU Parted 2.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt
Warning: The existing disk label on /dev/sdb will be destroyed and all
data on this disk will be lost. Do you want to continue?
Yes/No? yes
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags

(parted) mkpart primary 0GB 20GB
(parted) mkpart primary
File system type? [ext2]? ext4
Start? 0
End? 100%
Warning: You requested a partition from 0.00B to 21.5GB.
The closest location we can manage is 17.4kB to 1048kB.
Is this still acceptable to you?
Yes/No? yes
Warning: The resulting partition is not properly aligned for best
performance.
Ignore/Cancel? Ignore
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 21.5GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number Start End Size File system Name Flags
2 17.4kB 1049kB 1031kB primary
1 1049kB 20.0GB 20.0GB primary

(parted) quit
Information: You may need to update /etc/fstab.

[root@mysever2 ~]# mkfs.ext4 /dev/sdb
mke2fs 1.41.12 (17-May-2010)
/dev/sdb is entire device, not just one partition!
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5242880 blocks
262144 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
160 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

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

This filesystem will be automatically checked every 28 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@mysever2 ~]# mount /dev/sdb /soft
mount: mount point /soft does not exist
[root@mysever2 ~]# mkdir /soft
[root@mysever2 ~]# mount /dev/sdb /soft
[root@mysever2 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_mysever2-LogVol00

                   20G  1.9G   17G  10% /

tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 190M 36M 145M 20% /boot
/dev/mapper/vg_mysever2-LogVol01

                   28G   44M   26G   1% /data

/dev/sdb 20G 44M 19G 1% /soft
四.逻辑卷

特点:

Logical Volume Manager
可以弹性的调整文件系统的容量
不能保证数据的读写性能和可靠性
整合多个磁盘或分区的容量
可以拉伸和缩小文件系统的大小

PV—物理卷
磁盘或分区(8e)
VG —卷组
由PV组合而成
最多可以容纳65534个PE
LV— 逻辑卷
/dev/vgname/lvname
PE —LVM的最小存储单位
默认大小是4M

pv相关的命令

pvcreate 把普通分区创建成PV
pvscan 查看系统中有哪些pv分区
pvdisplay 查看系统中PV的状态属性
pvremove 删除PV的属性

vg相关的命令:

vgcreate 创建成VG
vgscan 查看系统中有哪些vg分区
vgdisplay 查看系统中vg的状态属性
vgextend 在VG中增加PV
vgreduce 在VG中移除PV
vgchange 设定VG是否启动(active)
vgremove 删除VG

lv相关的命令:

lvcreate 创建LV
lvscan 搜索系统内的LV
lvdisplay LV的详细信息
lvextend 拉伸
lvreduce 压缩
lvremove 删除LV
lvresize 调整大小,可大可小

关闭lvm:

卸载系统上的lvm文件系统(包括快照)
使用lvremove移除LV
使用vgchange -a n vgname 让vgname这个VG不具有active的标志
使用vgremove移除VG
使用pvremove移除PV
修改分区ID

经验案例
公司一台mysql数据库服务器,有多个库,每个库对应一个目录,
由于事先估计到了某些生产业务的实际问题,事先在安装系统时创建好了逻辑卷,
由于某些业务极具增长,导致data目录使用容量已经超过80%,而其他目录使用只有20%,此时就可以使
80%,此时就可以使用逻辑卷进行管理

逻辑卷.png

lvm 1.pnglvm 2.png


添加新评论