Linux基本知识点总结——硬盘分区及LVM
以下实验在虚拟机上进行,实验平台为CentOS 6.2。
新增两个硬盘sdb,sdc,其容量大小均为8G。
实验要求:
分别在两个硬盘上创建一个大小为5G的主分区sdb1,sdc1;
创建卷组vg_data,使其包括sdb1,sdc1两个分区;
在卷组vg_data上创建一个大小为8G的逻辑卷lv_share,并使lv_share加载可用。
一、系统当前的分区信息
[root@itpro ~]# more /proc/partitions
major minor #blocks name
8 0 16777216 sda
8 1 204800 sda1
8 2 16571392 sda2
8 16 8388608 sdb
8 32 8388608 sdc
253 0 10485760 dm-0
253 1 2097152 dm-1
253 2 2097152 dm-2
(注:也可以用fdisk –l查看更加详细的信息,会有Disk /dev/sdb doesn’t contain a valid partition table之类的信息。)
[root@itpro ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 vg_itpro lvm2 a– 15.80g 1.80g
[root@itpro ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg_itpro 1 3 0 wz–n- 15.80g 1.80g
[root@itpro ~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
lv_data vg_itpro -wi-ao 2.00g
lv_root vg_itpro -wi-ao 10.00g
lv_swap vg_itpro -wi-ao 2.00g
(注:可用pvscan、vgscan、lvscan和pvdisplay、vgdisplay、lvdisplay查看更详细的信息)
二、在新增加的硬盘上建立分区
1.在sdb上创建大小为5G的主分区
[root@itpro ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x650c58ea.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won’t be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
switch off the mode (command ‘c’) and change display units to
sectors (command ‘u’).
Command (m for help): n #输入n,表示创建新分区
Command action
e extended
p primary partition (1-4)
p #输入p,表示创建主分区
Partition number (1-4): 1 #输入1,表示第一个主分区
First cylinder (1-1044, default 1): #这个不填,使用默认值
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1044, default 1044): +5G #设置分区大小为5G
Command (m for help): w #输入w,表示保存退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
2.在sdc上创建大小为5G的主分区
[root@itpro ~]# fdisk /dev/sdc
(操作过程同上,略)
3.查看此时的分区表
[root@itpro ~]# more /proc/partitions
major minor #blocks name
8 0 16777216 sda
8 1 204800 sda1
8 2 16571392 sda2
8 16 8388608 sdb
8 17 5253223 sdb1
8 32 8388608 sdc
8 33 5253223 sdc1
253 0 10485760 dm-0
253 1 2097152 dm-1
253 2 2097152 dm-2
(注:如果没有sdb1、sdc1的信息,则需要使用命令partx -a /dev/sdx,
[root@itpro ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
[root@itpro ~]# partx -a /dev/sdc
BLKPG: Device or resource busy
error adding partition 1
)三、接下来为了加载使用磁盘空间,需依次创建pv、vg、lv
只有lv逻辑卷才可以建立文件系统并被加载到指定目录里。
1、创建物理卷
[root@itpro ~]# pvcreate /dev/sdb1 /dev/sdc1
Writing physical volume data to disk “/dev/sdb1”
Physical volume “/dev/sdb1” successfully created
Writing physical volume data to disk “/dev/sdc1”
Physical volume “/dev/sdc1” successfully created
[root@itpro ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 vg_itpro lvm2 a– 15.80g 1.80g
/dev/sdb1 lvm2 a– 5.01g 5.01g
/dev/sdc1 lvm2 a– 5.01g 5.01g
(注:使用pvs命令查看pv信息,此时,sda2属于vg_itpro,sdb1和sdc1还不属于任何卷组)
2、创建卷组
卷组名为vg_add,并将sdb1、sdc1加入到里面。
[root@itpro ~]# vgcreate vg_add /dev/sdb1 /dev/sdc1
Volume group “vg_add” successfully created
[root@itpro ~]# vgs
VG #PV #LV #SN Attr VSize VFree
vg_add 2 0 0 wz–n- 10.02g 10.02g
vg_itpro 1 3 0 wz–n- 15.80g 1.80g
(注:使用vgs命令查看vg信息,可以看到新创建的vg_add,其大小为10G;)
[root@itpro ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 vg_itpro lvm2 a– 15.80g 1.80g
/dev/sdb1 vg_add lvm2 a– 5.01g 5.01g
/dev/sdc1 vg_add lvm2 a– 5.01g 5.01g
(注:可以看到,此时的sdb1和sdc1属于卷组vg_add)
3、创建逻辑卷
在卷组vg_add上创建逻辑卷lv_share,大小为8G(即卷组vg_add还有2G空间保留未用)。
[root@itpro ~]# lvcreate -n lv_share -L 8G /dev/vg_add
Logical volume “lv_share” created
[root@itpro ~]# lvs
LV VG Attr LSize Origin Snap% Move Log Copy% Convert
lv_share vg_add -wi-a- 8.00g
lv_data vg_itpro -wi-ao 2.00g
lv_root vg_itpro -wi-ao 10.00g
lv_swap vg_itpro -wi-ao 2.00g
(注:使用lvs命令查看lv大致信息)
4、格式化,文件系统格式为ext4
[root@itpro ~]# mkfs -t ext4 /dev/vg_add/lv_share
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
524288 inodes, 2097152 blocks
104857 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2147483648
64 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
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
5、挂载
建立挂载点,
[root@itpro ~]# mkdir -p /share
编辑fstab,加入lv_share,并保存退出,
[root@itpro ~]# vi /etc/fstab
# /etc/fstab
/dev/mapper/vg_itpro-lv_root / ext4 defaults 1 1
/dev/vg_itpro/lv_data /data ext4 defaults 0 0
UUID=3344f006-b05c-4eaa-86db-541135f6c698 /boot ext4 defaults 1 2
/dev/mapper/vg_itpro-lv_swap swap swap defaults 0 0
/dev/vg_add/lv_share /share ext4 defaults 0 0
……省略部分输出……
挂载分区,
[root@itpro ~]# mount -a
[root@itpro ~]# ls /share/
lost+found
(注:share里面出现蓝色的 lost+found 表示读到的是逻辑卷lv_share 的内容,即挂载成功)
至此,挂载操作顺利完成。四、拆除硬盘sdb
假设,发现硬盘sdb异常,或由于其他原因,希望拆除。
1、此时要先分析硬盘的使用情况,判断是否可行。
两硬盘的实际容量:sdb 8G, sdc 8G;
两硬盘的可用分区:sdb1 5G,sdc1 5G (注:每个硬盘各自还有3G空间没用);
卷组Vg_data:包括sdb1和sdc1,在上面划分了一个逻辑卷lv_share;
逻辑卷lv_share:大小为8G,实际数据大小为3.4G;
[root@itpro share]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_itpro-lv_root
9.9G 1.7G 7.7G 18% /
tmpfs 504M 0 504M 0% /dev/shm
/dev/mapper/vg_itpro-lv_data
2.0G 68M 1.9G 4% /data
/dev/sda1 194M 26M 159M 14% /boot
/dev/mapper/vg_add-lv_share
7.9G 3.4G 4.2G 46% /share
Lv_share的大小为8G,并不是平均分摊在sdb1和sdc1上的,而是在sdb1上使用5G,再在sdc1上使用3G,通过命令pvs可以查看(注:可以看到,此时sdb1上剩余空间为0G,sdc1上剩余空间为2G)
[root@itpro ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 vg_itpro lvm2 a– 15.80g 1.80g
/dev/sdb1 vg_add lvm2 a– 5.01g 0
/dev/sdc1 vg_add lvm2 a– 5.01g 2.02g
通过以上数据,可以知,sdb可以移除,但必须先调整lvm,使得sdc1的可用空间大小sdb1的已用空间。
2.在当前lv_share大小为8G,占用sdb1 5G、sdc1 3G的情况下,无法将数据由sdb1往sdc1移。
[root@itpro ~]# pvmove /dev/sdb1
Insufficient free space: 1282 extents needed, but only 516 available
Unable to allocate mirror extents for pvmove0.
Failed to convert pvmove LV to mirrored
解决方法是,
a.先将lv_share缩小到5G(注:这个数值一定要大于实际数据大小,本实验中为3.4G)
[root@itpro ~]# umount /dev/vg_add/lv_share /share/
umount: /share: not mounted
[root@itpro ~]# resize2fs /dev/vg_add/lv_share 5G
resize2fs 1.41.12 (17-May-2010)
Please run ‘e2fsck -f /dev/vg_add/lv_share’ first.
(注:RHEL 6是要求使用命令e2fsck –f命令作检查的)
[root@itpro ~]# e2fsck -f /dev/vg_add/lv_share
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg_add/lv_share: 44/524288 files (27.3% non-contiguous), 915087/2097152 blocks
[root@itpro ~]# resize2fs /dev/vg_add/lv_share 5G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg_add/lv_share to 1310720 (4k) blocks.
The filesystem on /dev/vg_add/lv_share is now 1310720 blocks long.
[root@itpro ~]# lvresize /dev/vg_add/lv_share -L 5G
WARNING: Reducing active logical volume to 5.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lv_share? [y/n]: y
Reducing logical volume lv_share to 5.00 GiB
Logical volume lv_share successfully resized
[root@itpro ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 vg_itpro lvm2 a– 15.80g 1.80g
/dev/sdb1 vg_add lvm2 a– 5.01g 8.00m
/dev/sdc1 vg_add lvm2 a– 5.01g 5.01g
(注:查看此时的硬盘使用情况,可以看到,lv_share使用的全部是sdb1的容量,sdc1的空间没有使用到)
b.移动数据,使sdb1置空
[root@itpro ~]# pvmove /dev/sdb1
/dev/sdb1: Moved: 0.6%
/dev/sdb1: Moved: 35.7%
/dev/sdb1: Moved: 70.4%
/dev/sdb1: Moved: 100.0%
[root@itpro ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 vg_itpro lvm2 a– 15.80g 1.80g
/dev/sdb1 vg_add lvm2 a– 5.01g 5.01g
/dev/sdc1 vg_add lvm2 a– 5.01g 8.00m
(注:可以看到,此时lv_share使用的全部是sdc1的容量,sdb1的空间没有使用到)
c.移除sdb1
[root@itpro ~]# vgreduce /dev/vg_add /dev/sdb1
Removed “/dev/sdb1” from volume group “vg_add”
[root@itpro ~]# pvremove /dev/sdb1
Labels on physical volume “/dev/sdb1” successfully wiped
[root@itpro ~]# pvs
PV VG Fmt Attr PSize PFree
/dev/sda2 vg_itpro lvm2 a– 15.80g 1.80g
/dev/sdc1 vg_add lvm2 a– 5.01g 8.00m
d.挂载并查看此时的lv_share内容,可以看到,并没有数据丢失
[root@itpro ~]# mount /dev/vg_add/lv_share /share/
[root@itpro ~]# ls /share/
lost+found t03 t06 t09 t12 t15 t18 t21 t24 t27 t30 t33
t01 t04 t07 t10 t13 t16 t19 t22 t25 t28 t31
t02 t05 t08 t11 t14 t17 t20 t23 t26 t29 t32
[root@itpro ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/vg_itpro-lv_root
9.9G 1.7G 7.7G 18% /
tmpfs 504M 0 504M 0% /dev/shm
/dev/mapper/vg_itpro-lv_data
2.0G 68M 1.9G 4% /data
/dev/sda1 194M 26M 159M 14% /boot
/dev/mapper/vg_add-lv_share
5.0G 3.4G 1.4G 72% /share
以上操作完成之后,关闭系统,拔除sdb硬盘,开机后,系统将如常使用。