2019独角兽企业重金招聘Python工程师标准>>>
一、背景
一般云主机的根目录都是在镜像制作时就确定下来了,在启动云主机时,选定的实例类型也会限定云主机的/目录大小。 偶尔可能会出现系统/目录不够用的情况,一般有两种方式来解决这个问题: 第一个是添加磁盘,将相关文件移至新增盘; 第二个是扩展/目录。
二、云主机启动的两种方式
云主机启动时也有两种方式
2.1. 方式一、从卷启动
这种方式启动的云主机,指定的卷大小必须不小于镜像的根目录大小,比如镜像的/目录大小为50G,此处指定为100G,但是云主机启动之后/目录还是为50G,这样就可以直接将这剩余的50G扩展到/目录下。这样做有个限制,扩展的大小有上限,取决于云主机创建时指定的大小。参考方式二,可以跨过这个限制。
查看分区情况
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 100G 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 49G 0 part
└─centos-root 253:0 0 49G 0 lvm /
将剩余空间分区
# fdisk /dev/vda
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
Partition number (3,4, default 3):
First sector (104857600-209715199, default 104857600):
Using default value 104857600
Last sector, +sectors or +size{K,M,G} (104857600-209715199, default 209715199):
Using default value 209715199
Partition 3 of type Linux and of size 50 GiB is set
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.
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 100G 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 49G 0 part
└─centos-root 253:0 0 49G 0 lvm /
# partprobe
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 100G 0 disk
├─vda1 252:1 0 1G 0 part /boot
├─vda2 252:2 0 49G 0 part
│ └─centos-root 253:0 0 49G 0 lvm /
└─vda3 252:3 0 50G 0 part
创建PV
# pvcreate /dev/vda3
Physical volume "/dev/vda3" successfully created.
添加物理卷
# vgscan
Reading volume groups from cache.
Found volume group "centos" using metadata type lvm2
# vgextend centos /dev/vda3
Volume group "centos" successfully extended
扩展物理卷
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 49G 1.6G 48G 4% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.5M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/vda1 1014M 145M 870M 15% /boot
tmpfs 379M 0 379M 0% /run/user/0
# lvextend -l +100%FREE /dev/mapper/centos-root
Size of logical volume centos/root changed from <49.00 GiB (12543 extents) to 98.99 GiB (25342 extents).
Logical volume centos/root successfully resized.
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 100G 0 disk
├─vda1 252:1 0 1G 0 part /boot
├─vda2 252:2 0 49G 0 part
│ └─centos-root 253:0 0 99G 0 lvm /
└─vda3 252:3 0 50G 0 part
└─centos-root 253:0 0 99G 0 lvm /
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 49G 1.6G 48G 4% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.5M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/vda1 1014M 145M 870M 15% /boot
tmpfs 379M 0 379M 0% /run/user/0
xfs在线扩容
# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=3211008 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=12844032, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=6271, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 12844032 to 25950208
扩展成功,重启云主机后/目录依旧为扩展后大小
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 99G 1.6G 98G 2% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.5M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/vda1 1014M 145M 870M 15% /boot
tmpfs 379M 0 379M 0% /run/user/0
2.2. 方式二、从镜像启动
直接从镜像启动,不创建卷,云主机启动后/目录的大小为镜像中默认大小,这样就得新增一块盘,直接将这块盘扩展到/目录即可
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 50G 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 49G 0 part
└─centos-root 253:0 0 49G 0 lvm /
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 50G 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 49G 0 part
└─centos-root 253:0 0 49G 0 lvm /
vdb 252:16 0 100G 0 disk
# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x5ee47290.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1):
First sector (2048-209715199, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-209715199, default 209715199):
Using default value 209715199
Partition 1 of type Linux and of size 100 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 50G 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 49G 0 part
└─centos-root 253:0 0 49G 0 lvm /
vdb 252:16 0 100G 0 disk
└─vdb1 252:17 0 100G 0 part
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 49G 1.6G 48G 4% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.5M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/vda1 1014M 145M 870M 15% /boot
tmpfs 379M 0 379M 0% /run/user/0
# pvcreate /dev/vdb1
Physical volume "/dev/vdb1" successfully created.
# vgscan
Reading volume groups from cache.
Found volume group "centos" using metadata type lvm2
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 49G 1.6G 48G 4% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.5M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/vda1 1014M 145M 870M 15% /boot
tmpfs 379M 0 379M 0% /run/user/0
# vgextend VolGroup /dev/vdb1
Volume group "VolGroup" not found
Cannot process volume group VolGroup
# vgextend centos /dev/vdb1
Volume group "centos" successfully extended
# lvextend -l +100%FREE /dev/mapper/centos-root
Size of logical volume centos/root changed from <49.00 GiB (12543 extents) to 148.99 GiB (38142 extents).
Logical volume centos/root successfully resized.
# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512 agcount=4, agsize=3211008 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=12844032, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal bsize=4096 blocks=6271, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
data blocks changed from 12844032 to 39057408
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 149G 1.6G 148G 2% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.5M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/vda1 1014M 145M 870M 15% /boot
tmpfs 379M 0 379M 0% /run/user/0
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 252:0 0 50G 0 disk
├─vda1 252:1 0 1G 0 part /boot
└─vda2 252:2 0 49G 0 part
└─centos-root 253:0 0 149G 0 lvm /
vdb 252:16 0 100G 0 disk
└─vdb1 252:17 0 100G 0 part
└─centos-root 253:0 0 149G 0 lvm /