CentOS根分区扩容方法

2017/5/26 posted in  linux

Linux的磁盘管理是很重要的一部分,这里面涉及到比较多的部分需要理解。在安装之处可以设定相应的磁盘空间,但是新增磁盘的时候,挂盘、查询、挂载、格式化、mount,这样的操作还是比较多的。

当前我们采用vmware下面的CentOS的操作系统,现在的需求是创建一块新的磁盘,能够用户扩展虚拟机的根分区。

初始的磁盘状态就不在这里演示了。

vmware磁盘加盘

vmware的添加磁盘也在这里说一下,还是挺有意思的。

添加磁盘的最后一步,有一个选项,如下所示

5C5E6DEF-4C71-42A0-B966-E6A96CA05908

可以看到虚拟设备节点 SCSI(0:1) ,这里应该表示3个插槽,每个插槽有15个

连接到SCSI接口的设备使用ID号进行区别,SCSI设备ID号为0~15,SCSI接口卡本身的ID号是7。Linux对连接到SCSI接口卡的硬盘使用_dev_sdx的方式命名,x的值可以是a、b、c、d等,即ID号为0的SCSI硬盘名为_dev_sda,ID号为1的SCSI硬盘名为_dev_sdb,以此类推。LINUX对SCSI硬盘最多支持15个分区。

所以这里可以看到SCSI(0:7) 不存在。

在虚拟机中刷新fdisk 表

[[vmware 下 ubuntu的挂盘和卸载盘]] 文章中介绍了可以动态的刷新盘,按照这个步骤来操作即可。

echo "- - -" > /sys/class/scsi_host/host2/scan

然后fdisk -l就可以看到刷新出来的盘

[root@jku-server-slave ~]# fdisk -l

Disk /dev/sda: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00042985

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64       26109   209202176   8e  Linux LVM

Disk /dev/mapper/VolGroup-lv_root: 53.7 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/VolGroup-lv_swap: 8405 MB, 8405385216 bytes
255 heads, 63 sectors/track, 1021 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/mapper/VolGroup-lv_home: 152.1 GB, 152127406080 bytes
255 heads, 63 sectors/track, 18495 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000


Disk /dev/sdb: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

查看df -h

[root@jku-server-slave ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root   50G  7.4G   40G  16% /
tmpfs                         3.9G     0  3.9G   0% /dev/shm
/dev/sda1                     485M   39M  421M   9% /boot
/dev/mapper/VolGroup-lv_home  140G  188M  133G   1% /home

磁盘分区和挂载

磁盘分区

[root@jku-server-slave ~]# 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 0x8d6de234.
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
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1958, default 1): 1
Last cylinder, +cylinders or +size{K,M,G} (1-1958, default 1958): 1958

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

sdb2分区格式化

[root@jku-server-slave ~]# mkfs.ext4 /dev/sdb1
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
983040 inodes, 3931900 blocks
196595 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4026531840
120 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

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.

格式化sdb2分区为物理卷

[root@jku-server-slave ~]# pvcreate /dev/sdb1
  dev_is_mpath: failed to get device for 8:17
  Physical volume "/dev/sdb1" successfully created

查看物理卷的情况

[root@jku-server-slave ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               VolGroup
  PV Size               199.51 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              51074
  Free PE               0
  Allocated PE          51074
  PV UUID               0wfker-mZQI-yQKG-b3qT-fFF0-3xq9-0K9xWZ

  "/dev/sdb1" is a new physical volume of "15.00 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sdb1
  VG Name
  PV Size               15.00 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               6YTfQU-gbQJ-eMYH-c97g-x2Rv-VaSj-Te7Xxt

查看卷组状态

[root@jku-server-slave ~]# vgdisplay
  --- Volume group ---
  VG Name               VolGroup
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               199.51 GiB
  PE Size               4.00 MiB
  Total PE              51074
  Alloc PE / Size       51074 / 199.51 GiB
  Free  PE / Size       0 / 0
  VG UUID               eSpwQ5-jGFv-OkVH-XSiw-YmVC-JZk9-d1CJgC

可以看到当前卷组是199G

将sdb2转换为扩展分区

[root@jku-server-slave ~]# vgextend VolGroup /dev/sdb1
  Volume group "VolGroup" successfully extended

查看当前逻辑卷

[root@jku-server-slave ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/VolGroup/lv_root
  LV Name                lv_root
  VG Name                VolGroup
  LV UUID                QwMJ1h-F5Ba-iPRK-v4Vm-o1sN-zhjG-y1jTsd
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2016-10-18 10:38:47 +0800
  LV Status              available
  # open                 1
  LV Size                50.00 GiB
  Current LE             12800
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/VolGroup/lv_home
  LV Name                lv_home
  VG Name                VolGroup
  LV UUID                u5IFJS-xX2L-cU81-Rl6r-xqJ2-Z33W-ryVNWm
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2016-10-18 10:39:13 +0800
  LV Status              available
  # open                 1
  LV Size                141.68 GiB
  Current LE             36270
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:2

  --- Logical volume ---
  LV Path                /dev/VolGroup/lv_swap
  LV Name                lv_swap
  VG Name                VolGroup
  LV UUID                pqNOBG-fVI7-e3q4-hgLe-5yaj-JWXi-3gneoO
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2016-10-18 10:40:11 +0800
  LV Status              available
  # open                 1
  LV Size                7.83 GiB
  Current LE             2004
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

查看扩展后卷组情况

[root@jku-server-slave ~]# vgdisplay
  --- Volume group ---
  VG Name               VolGroup
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  5
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                3
  Open LV               3
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               214.50 GiB
  PE Size               4.00 MiB
  Total PE              54913
  Alloc PE / Size       51074 / 199.51 GiB
  Free  PE / Size       3839 / 15.00 GiB
  VG UUID               eSpwQ5-jGFv-OkVH-XSiw-YmVC-JZk9-d1CJgC

可以看到扩展后的卷组大小已经加上了

将逻辑卷则新增到/分区中

[root@jku-server-slave ~]# lvextend -L +11G /dev/VolGroup/lv_root
  Extending logical volume lv_root to 61.00 GiB
  Logical volume lv_root successfully resized
[root@jku-server-slave ~]# e2fsck -f /dev/VolGroup/lv_root
e2fsck 1.41.12 (17-May-2010)
/dev/VolGroup/lv_root is mounted.
e2fsck: Cannot continue, aborting.


[root@jku-server-slave ~]# resize2fs /dev/VolGroup/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required
old desc_blocks = 4, new_desc_blocks = 4
Performing an on-line resize of /dev/VolGroup/lv_root to 15990784 (4k) blocks.
The filesystem on /dev/VolGroup/lv_root is now 15990784 blocks long.

最后查看绑定情况

[root@jku-server-slave ~]# df -h
Filesystem                    Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root   61G  7.4G   50G  13% /
tmpfs                         3.9G     0  3.9G   0% /dev/shm
/dev/sda1                     485M   39M  421M   9% /boot
/dev/mapper/VolGroup-lv_home  140G  188M  133G   1% /home

tips: 最后想办法把这个整理到ubuntu中去

经测试,ubuntu所有命令相同,同样适用。