项目运行时间越久,特别是图片服务器,存储的多了,就会遇到磁盘满的问题。
学会如何给linux分区扩容很有必要。
默认linux只允许有4个主分区。
展现下目前我机器的分区情况和硬盘情况。
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 17G 14G 3.1G 83% /
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 8.7M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 1.6G 0 1.6G 0% /run/user/1005
tmpfs 1.6G 0 1.6G 0% /run/user/0
可以看到/dev/mapper/centos-root使用83%了,该给它加点空间啦!
[root@localhost ~]# fdisk -l
磁盘 /dev/sda:64.4 GB, 64424509440 字节,125829120 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00000242
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
磁盘 /dev/mapper/centos-root:18.2 GB, 18249416704 字节,35643392 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
目前我这里有三块磁盘,/dev/sda下有64G,分点给/dev/mapper/centos-root吧
首先执行
[root@localhost ~]# fdisk /dev/sda
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
命令(输入 m 获取帮助):m
命令操作
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
在这里我们新建一个分区,输入n,p为主分区,因为我已经有/dev/sda1,和/dev/sda2,而linux只允许有4个主分区,从下面的提示也知道,2free(还可以再建两个主分区),一直默认下去,我们就可以建立3号分区。
命令(输入 m 获取帮助):n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
分区号 (3,4,默认 3):
起始 扇区 (41943040-125829119,默认为 41943040):
将使用默认值 41943040
Last 扇区, +扇区 or +size{K,M,G} (41943040-125829119,默认为 125829119):
将使用默认值 125829119
分区 3 已设置为 Linux 类型,大小设为 40 GiB
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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)
正在同步磁盘。
再次用fdisk -l 查看,可以看到多了一个/dev/sda3
[root@localhost ~]# fdisk -l
磁盘 /dev/sda:64.4 GB, 64424509440 字节,125829120 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x00000242
设备 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 41943039 19921920 8e Linux LVM
/dev/sda3 41943040 125829119 41943040 83 Linux
磁盘 /dev/mapper/centos-root:18.2 GB, 18249416704 字节,35643392 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘 /dev/mapper/centos-swap:2147 MB, 2147483648 字节,4194304 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
创建物理卷
执行pvcreate /dev/sda3,如果tab补全找不到sda3,可以先执行partprobe
[root@localhost ~]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created.
使用vgscan查询物理卷
[root@localhost ~]# vgscan
Reading volume groups from cache.
Found volume group "centos" using metadata type lvm2
我的group为centos
使用新增物理卷扩展
[root@localhost ~]# vgextend centos /dev/sda3
Volume group "centos" successfully extended
接下来给/dev/mapper/centos-root增加10G,之前的是17G(从df -h可以知道)
[root@localhost ~]# lvextend -L +10G /dev/mapper/centos-root
Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to <27.00 GiB (6911 extents).
Logical volume centos/root successfully resized.
直接查询是不能看到增加到27G了
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 17G 14G 3.1G 83% /
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 8.8M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 1.6G 0 1.6G 0% /run/user/1005
tmpfs 1.6G 0 1.6G 0% /run/user/0
因为我们的系统还不认识刚刚添加进来的磁盘的文件系统,所以还需要对文件系统进行扩容
[root@localhost ~]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=1113856 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=4455424, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 4455424 to 7076864
再次查看,已经可以看到变成了27G了。
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 27G 14G 14G 52% /
devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs 7.8G 0 7.8G 0% /dev/shm
tmpfs 7.8G 8.8M 7.8G 1% /run
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/sda1 1014M 143M 872M 15% /boot
tmpfs 1.6G 0 1.6G 0% /run/user/1005
tmpfs 1.6G 0 1.6G 0% /run/user/0