linux /home目录扩容
文章目录
前言
背景:目前home目录空间不足,急需扩容。
环境:由于home目录是单独分区在sda1,sda2分区挂载 到跟目录的, 并且进去boot引导也在sda2中
目的:需要保留home的数据,并且对/home扩容。
一、使用fdisk 进行磁盘分区
1.fdisk /dev/sda 进入磁盘/dev/sda
[root@weizhongkai-NeoKylin ~]# fdisk /dev/sda
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').
输入p打印目前磁盘分区情况
Command (m for help): p
Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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: 0x0000b864
Device Boot Start End Blocks Id System
/dev/sda1 1 5222 41943040 83 Linux
/dev/sda2 * 5222 6528 10484736 83 Linux
/dev/sda3 6528 13054 52427455 5 Extended
/dev/sda5 6528 6659 1059586 82 Linux swap / Solaris
上面可以看到磁盘只用到了6659 cylinders, 总共13054 cylinders, 表示还有剩余的空闲空间未分区
输入n, 新建磁盘
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
# 输入p创建主分区, 由于当前磁盘分区都已经使用所以提示“No free sectors available”
p
Selected partition 4
No free sectors available
重新输入n, 新建磁盘
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
# 输入l创建逻辑分区
l
First cylinder (6660-13054, default 6660):
# 回传使用默认值6660 作为分区的开始
Using default value 6660
Last cylinder, +cylinders or +size{K,M,G} (6660-13054, default 13054):
# 13054 作为分区的结束(使用了全部空闲空间)
Using default value 13054
输入w , 保存分区修改
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: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
2. 输入,partprobe 重新加载分区
不然无法看到最新分区的/dev/sda6
[root@weizhongkai-NeoKylin ~]# partprobe
二、创建lvm 逻辑卷分区
1、首先创建LVM的基本存储逻辑块(physicalvolume,后续简称pv)
使用命令 pvcreate [device]
[root@weizhongkai-NeoKylin ~]# pvcreate /dev/sda6
dev_is_mpath: failed to get device for 8:6
Physical volume "/dev/sda6" successfully created
pvdisplay查看 pv 基本情况
[root@weizhongkai-NeoKylin ~]# pvdisplay
"/dev/sda6" is a new physical volume of "48.99 GiB"
--- NEW Physical volume ---
PV Name /dev/sda6
VG Name
PV Size 48.99 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 0vx7Po-PTLA-oTK0-lhh3-MwkG-ZMdD-N77jQ0
2、然后创建Volume Group,后续简称vg, 使用命令 “vgcreate [vg-name] [device]”
[root@weizhongkai-NeoKylin ~]# vgcreate vg01 /dev/sda6
Volume group "vg01" successfully created
pvdisplay查看 pv 基本情况 ,卷组名已经显示为vg01
[root@weizhongkai-NeoKylin ~]# pvdisplay
--- Physical volume ---
PV Name /dev/sda6
VG Name vg01
PV Size 48.99 GiB / not usable 3.87 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 12540
Free PE 12540
Allocated PE 0
PV UUID 0vx7Po-PTLA-oTK0-lhh3-MwkG-ZMdD-N77jQ0
3.然后创建LVM逻辑卷类,后续简称lv
lvcreate -l 100%VG(使用100%卷组空间) -n [lv Name] [vg Name]
[root@weizhongkai-NeoKylin ~]# lvcreate -l 100%VG -n lv_home vg01
Logical volume "lv_home" created
查看lvm卷组
[root@weizhongkai-NeoKylin ~]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv_home vg01 -wi-a----- 48.98g
4.格式化卷组 “mkfs.ext4 [lvm-device]”
lvm设备路径规则 /dev/[vg Name]/[lv Name]
[root@weizhongkai-NeoKylin ~]# mkfs.ext4 /dev/vg01/lv_home
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
3211264 inodes, 12840960 blocks
642048 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
392 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
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 38 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
查看分区情况
[root@weizhongkai-NeoKylin ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 9.9G 5.3G 4.1G 57% /
tmpfs tmpfs 1.9G 76K 1.9G 1% /dev/shm
/dev/sda1 ext4 40G 38G 326M 100% /home
[root@weizhongkai-NeoKylin ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sr0 11:0 1 1024M 0 rom
sda 8:0 0 100G 0 disk
|___sda1 8:1 0 40G 0 part /home
|___sda2 8:2 0 10G 0 part /
|___sda3 8:3 0 1K 0 part
|___sda5 8:5 0 1G 0 part [SWAP]
|___sda6 8:6 0 49G 0 part
|___vg01-lv_home (dm-0) 253:0 0 49G 0 lvm
三、挂载新的lvm 分区
1.创建临时挂载目录
[root@weizhongkai-NeoKylin ~]# mkdir -p /mnt/home
2.挂载lvm分区到临时挂载目录
lvm分区设备路径规则:/dev/mapper/[vg Name]_[lv Name]
[root@weizhongkai-NeoKylin ~]# mount /dev/mapper/vg01-lv_home /mnt/home/
[root@weizhongkai-NeoKylin ~]# cd /
[root@weizhongkai-NeoKylin /]# ls /mnt/home/
lost+found
再次查看分区情况 /dev/mapper/vg01-lv_home 已经挂载到了/mnt/home
[root@weizhongkai-NeoKylin /]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 9.9G 5.3G 4.1G 57% /
tmpfs tmpfs 1.9G 76K 1.9G 1% /dev/shm
/dev/sda1 ext4 40G 38G 326M 100% /home
/dev/mapper/vg01-lv_home ext4 49G 180M 46G 1% /mnt/home
3.复制文件到新的分区
[root@weizhongkai-NeoKylin /]# cp -a /home/* /mnt/home/
[root@weizhongkai-NeoKylin /]# ls /mnt/home/
gc lost+found
4.卸载 /home/
强制清除 /home 目录的占用
[root@weizhongkai-NeoKylin /]# fuser -m -v -k /home/
USER PID ACCESS COMMAND
/home/: gc 1661 F.ce. watchDogService
gc 1949 F.ce. redis-server
gc 1958 F.ce. cyg-msgbus
gc 1971 F.ce. dbService
gc 1985 F.ce. GKServer
gc 2004 F.ce. mapservice
gc 2022 F.ce. backupService
gc 2037 F.ce. cyg-middle
gc 2051 F.ce. CYGCacheServer
gc 2067 F.ce. cyg-devicelog
gc 2083 F.ce. remoteUpgradeSe
gc 2103 F.ce. remoteUpgradeCl
gc 2124 F.ce. centerSyncServi
卸载 /dev/sda1
[root@weizhongkai-NeoKylin /]# umount /dev/sda1
再次查看分区情况
[root@weizhongkai-NeoKylin /]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 9.9G 5.3G 4.1G 57% /
tmpfs tmpfs 1.9G 80K 1.9G 1% /dev/shm
/dev/mapper/vg01-lv_home ext4 49G 38G 8.8G 81% /mnt/home
四、lvm 扩容
1.把/dev/sda1 转为pv
[root@weizhongkai-NeoKylin /]# pvcreate /dev/sda1
Physical volume "/dev/sda1" successfully created
pvdisplay查看 pv 基本情况,可以看到已经多了一个新的pv,/dev/sda1
[root@weizhongkai-NeoKylin /]# pvdisplay
--- Physical volume ---
PV Name /dev/sda6
VG Name vg01
PV Size 48.99 GiB / not usable 3.87 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 12540
Free PE 0
Allocated PE 12540
PV UUID 0vx7Po-PTLA-oTK0-lhh3-MwkG-ZMdD-N77jQ0
"/dev/sda1" is a new physical volume of "40.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sda1
VG Name
PV Size 40.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID t2YAql-5ZuK-xaWC-bKTP-1plB-pIP3-Srvkbs
2.vg卷组扩容,加入新的空间 /dev/sda1
注意:新加入的这部分空间会被清空数据的
vgextend [vg-name] [device]
[root@weizhongkai-NeoKylin /]# vgextend vg01 /dev/sda1
Volume group "vg01" successfully extended
输入vgs 可以看到, vg卷组有40.00g空闲空间
[root@weizhongkai-NeoKylin /]# vgs
VG #PV #LV #SN Attr VSize VFree
vg01 2 1 0 wz--n- 88.98g 40.00g
** pvdisplay查看 pv 基本情况,可以看到 /dev/sda1 已经属于 vg01**
[root@weizhongkai-NeoKylin /]# pvdisplay
--- Physical volume ---
PV Name /dev/sda6
VG Name vg01
PV Size 48.99 GiB / not usable 3.87 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 12540
Free PE 0
Allocated PE 12540
PV UUID 0vx7Po-PTLA-oTK0-lhh3-MwkG-ZMdD-N77jQ0
--- Physical volume ---
PV Name /dev/sda1
VG Name vg01
PV Size 40.00 GiB / not usable 4.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 10239
Free PE 10239
Allocated PE 0
PV UUID t2YAql-5ZuK-xaWC-bKTP-1plB-pIP3-Srvkbs
3.扩展lvm 增加指定容量
用lvextend命令 给 lvm设备( /dev/vg01/lv_home ) 增加39G容量
如果出现 Insufficient free space: 10240 extents needed, but only 10239 available 则表示增加的容量大于vg组剩余的空间,需要适当减少
[root@weizhongkai-NeoKylin /]# lvextend -L +39G /dev/vg01/lv_home
Extending logical volume lv_home to 87.98 GiB
Logical volume lv_home successfully resized
** 再次输入vgs 可以看到, vg卷组还有1020.00m空闲空间 **
[root@weizhongkai-NeoKylin /]# vgs
VG #PV #LV #SN Attr VSize VFree
vg01 2 1 0 wz--n- 88.98g 1020.00m
4.再次使用lvextend命令 给 lvm设备( /dev/vg01/lv_home ) 增加1020m容量
[root@weizhongkai-NeoKylin /]# lvextend -L +1020m /dev/vg01/lv_home
Extending logical volume lv_home to 88.98 GiB
Logical volume lv_home successfully resized
** 再次输入vgs 可以看到,vg卷组空余空间为0了**
[root@weizhongkai-NeoKylin /]# vgs
VG #PV #LV #SN Attr VSize VFree
vg01 2 1 0 wz--n- 88.98g 0
5.resize2fs 刷新lvm设备( /dev/vg01/lv_home )的容量
[root@weizhongkai-NeoKylin /]# resize2fs /dev/vg01/lv_home
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg01/lv_home is mounted on /mnt/home; on-line resizing required
old desc_blocks = 4, new_desc_blocks = 6
Performing an on-line resize of /dev/vg01/lv_home to 23325696 (4k) blocks.
The filesystem on /dev/vg01/lv_home is now 23325696 blocks long.
[root@weizhongkai-NeoKylin /]# ls /mnt/home/
gc lost+found
查看分区情况, /dev/mapper/vg01-lv_home已经变成 88G
[root@weizhongkai-NeoKylin /]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext4 9.9G 5.3G 4.1G 57% /
tmpfs tmpfs 1.9G 80K 1.9G 1% /dev/shm
/dev/mapper/vg01-lv_home ext4 88G 38G 47G 45% /mnt/home
五、设置新添加的lvm分区自动挂载到homg目录
1.blkid 查询/dev/mapper/vg01-lv_home 的uuid
[root@weizhongkai-NeoKylin /]# blkid
/dev/sda2: UUID="799ecd75-294e-4522-9aa4-efb4f2e77266" TYPE="ext4"
/dev/sda1: UUID="t2YAql-5ZuK-xaWC-bKTP-1plB-pIP3-Srvkbs" TYPE="LVM2_member"
/dev/sda5: UUID="03fa895f-418a-45a1-86d8-74245a7929d2" TYPE="swap"
/dev/sda6: UUID="0vx7Po-PTLA-oTK0-lhh3-MwkG-ZMdD-N77jQ0" TYPE="LVM2_member"
/dev/mapper/vg01-lv_home: UUID="f5271c74-8295-41cd-9662-d0160749459a" TYPE="ext4"
2. 编辑 /etc/fstab ,修改 /home 的UUID,查看下面修改后的fstab文件
[root@weizhongkai-NeoKylin /]# vi /etc/fstab
[root@weizhongkai-NeoKylin /]# cat /etc/fstab
修改后的 fstab
#
# /etc/fstab
# Created by anaconda on Tue May 28 11:38:01 2019
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=799ecd75-294e-4522-9aa4-efb4f2e77266 / ext4 defaults 1 1
#UUID=5e3ac7e6-f028-4cbb-ac95-f04fd70f1126
UUID=f5271c74-8295-41cd-9662-d0160749459a /home ext4 defaults 1 2
UUID=03fa895f-418a-45a1-86d8-74245a7929d2 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
六、完成,重启系统,查看是否正常挂载
[root@weizhongkai-NeoKylin /]# reboot