Vagrant学习
vagrant安装
安装软件
在 vagrant官网 选择适合当前操作系统的版本,然后傻瓜式安装
安装完成后,可以通过如下命令查看版本
$ vagrant version
Installed Version: 2.3.4
Latest Version: 2.3.4
You're running an up-to-date version of Vagrant!
注意: 如果在 windows 下安装了 vagrant, vagrant 所有命令需在 GitBash 下执行。
镜像下载
- Vagrant的镜像下载网站 https://app.vagrantup.com/boxes/search
- CentOS 的镜像下载网站 http://cloud.centos.org/centos
- Ubuntu 的镜像下载网站 http://cloud-images.ubuntu.com
- Ubuntu 清华大学的镜像站 https://mirror.tuna.tsinghua.edu.cn/ubuntu-cloud-images/vagrant/
注意: 下载的镜像要和使用的虚拟机相匹配, 如使用 VirtualBox 虚拟机则下载时选择 virtual 镜像
下载的文件以.box结尾, 如:
trusty-server-cloudimg-amd64-vagrant-disk1.boxCentOS-7-x86_64-Vagrant-2004_01.VirtualBox.box
添加镜像
Vagrant 没有 GUI,只能从命令行访问,先使用 GitBash 启动一个命令行,然后执行:
$ vagrant box list
There are no installed boxes! Use `vagrant box add` to add some.
提示现在还没有 box,需要进行添加。
$ pwd
/k/Linux/virtualMachines/Vagrant/work
# $ vagrant box add e:\Downloads\CentOS-7.box --name centos-7
# $ vagrant box add --provider virtualbox centos7 /k/Linux/virtualMachines/Vagrant/vagrant-centos-7.2.box
$ vagrant box add centos7 /k/Linux/virtualMachines/Vagrant/vagrant-centos-7.2.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'centos7' (v0) for provider:
box: Unpacking necessary files from: file:///K:/Linux/virtualMachines/Vagrant/vagrant-centos-7.2.box
box:
==> box: Successfully added box 'centos7' (v0) for 'virtualbox'!
再次查询,就可以看到添加的box
$ vagrant box list
centos7 (virtualbox, 0)
基本操作
新建虚拟机
创建一个目录,先执行 vagrant init:
$ mkdir demo
$ cd demo
$ vagrant init centos7
A `Vagrantfile` has been placed in this directory. You are now ready to `vagrant up` your first virtual environment! Please read the comments in the Vagrantfile as well as documentation on `vagrantup.com` for more information on using Vagrant.
启动虚拟机
我们等会再来细看这个文件,现在直接按照提示执行 vagrant up:
$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos-7'...
==> default: Matching MAC address for NAT networking...
==> default: Setting the name of the VM: demo_default_1588406874156_65036
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
==> default: Forwarding ports...
default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
default: SSH address: 127.0.0.1:2222
default: SSH username: vagrant
default: SSH auth method: private key
正常的情况下,不到一分钟应该就能启动成功了。
注意到这里包含的信息:
- 虚拟机名称:demo_default_1588406874156_65036
- 网卡:Adapter 1: nat,第一块网卡,NAT 模式,这是固定的
- 端口转发:22 (guest) => 2222 (host) (adapter 1),把虚拟机的 22 端口,映射到宿主机的 2222 端口上,这样就可以通过 127.0.0.1:2222 访问虚拟机了
- SSH 用户名:vagrant,这里使用 private key 登录
- 密码也是 vagrant,但是密码方式仅供直接登录,是不能通过 SSH 登录的。
查看虚拟机状态
vagrant status
Current machine states:
default running (virtualbox)
The VM is running. To stop this VM, you can run `vagrant halt` to shut it down forcefully, or you can run `vagrant suspend` to simply suspend the virtual machine. In either case, to restart it again, simply run `vagrant up`.
连接虚拟机
如果启动没问题,接下来执行 vagrant ssh 就能以 vagrant 用户直接登入虚拟机中。
root 用户没有默认密码,也不能直接登录。需要 root 权限的命令可以通过在命令前添加 sudo 来执行,也可以执行 sudo -i 直接切换到 root 用户。
这时候打开 VirtualBox 程序,可以看到自动创建的虚拟机,我们也可以在 VirtualBox 的终端上登录系统,默认的登录用户名和密码都是 vagrant。
当然还可以使用其它的 SSH 连接工具例如 XShell,SecureCRT 连接,但是这里默认网卡使用的是 NAT 模式,没有指定 IP,实际应用并不方便,我们在后面介绍网络配置时再详细介绍如何连接虚拟机。
停止虚拟机
执行下面的命令可以关闭虚拟机:
vagrant halt
直接在 VirtualBox 上关闭虚拟机,或者直接在虚拟机内部执行 poweroff 命令也都是可以的。
暂停虚拟机
执行下面的命令可以暂停虚拟机:
vagrant suspend
恢复虚拟机
执行下面的命令把暂停状态的虚拟机恢复运行:
vagrant resume
注意: 不管虚拟机是关闭还是暂停状态,甚至是 error 状态,都可以执行 vagrant up 来让虚拟机恢复运行。
重载虚拟机
执行下面的命令会重启虚拟机,并且重新加载 Vagrantfile 中的配置信息:
vagrant reload
删除虚拟机
最后,执行下面的命令可以彻底删除虚拟机,包括整个虚拟机文件:
vagrant destroy
注意: 在当前这个小例子中,上面所有的 vagrant 命令都需要在 Vagrantfile 所在的目录下执行。
Vagrantfile
初识 Vagrantfile
先来认识一下默认的 Vagrantfile 文件,使用带语法高亮的文本编辑器(例如 VSCode) 打开:
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "centos7"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
# config.vm.provider "virtualbox" do |vb|
# # Display the VirtualBox GUI when booting the machine
# vb.gui = true
#
# # Customize the amount of memory on the VM:
# vb.memory = "1024"
# end
#
# View the documentation for the provider you are using for more
# information on available options.
# Enable provisioning with a shell script. Additional provisioners such as
# Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
end
这是一个 Ruby 语法的文件,因为 Vagrant 就是用 Ruby 编写的。如果编辑器没有语法高亮可以手动设置文件类型为 Ruby。
配置 Vagrantfile
网络配置
Vagrant的网络有三种模式:
- 较为常用是端口映射,就是将虚拟机中的端口映射到宿主机对应的端口直接使用,在Vagrantfile中配置:guest: 80 表示虚拟机中的80端口, host: 8080 表示映射到宿主机的8080端口。
config.vm.network :forwarded_port, guest: 80, host: 8080 - 如果需要自己自由的访问虚拟机,但是别人不需要访问虚拟机,可以使用 private_network,并为虚拟机设置IP ,在Vagrantfile中配置:192.168.1.104 表示虚拟机的IP,多台虚拟机的话需要互相访问的话,设置在相同网段即可
config.vm.network :private_network, ip: "192.168.1.104" - 如果需要将虚拟机作为当前局域网中的一台计算机,由局域网进行DHCP,那么在Vagrantfile中配置:
config.vm.network :public_network
配置同步文件夹
默认的,vagrant 将共享你的工作目录(即Vagrantfile所在的目录)到虚拟机中的 /vagrant, 所以一般不需配置即可,如你需要可配置:
# "src/":物理机目录;"/srv/website"虚拟机目录
config.vm.synced_folder "src/", "/srv/website"
默认情况下,当前的工作目录,会被映射到虚拟机的 /vagrant 目录,当前目录下的文件可以直接在 /vagrant 下进行访问,当然也可以在通过 ln 创建软连接,如 ln -fs /vagrant/wwwroot /var/www 来进行目录映射,当然,从自动化配置的角度,能不进系统就不需要进系统,所以在Vagrant也可以进行目录映射的操作:
# "wwwroot/" 表示的是本地的路径, 这里使用对于工作目录的相对路径,这里也可以使用绝对路径,比如: "d:/www/"
# "/var/www" 表示虚拟机中对应映射的目录
config.vm.synced_folder "wwwroot/", "/var/www"
更改虚拟机规格
VirtualBox 等虚拟机软件在 Vagrant 中被称为 Provider,虚拟机的规格等配置是和 Provider 相关的。因为 VirtualBox 用的最多,所以默认的配置提示是以 VirtualBox 举例。
添加 CPU 的配置,同时修改内存大小:
config.vm.provider "virtualbox" do |vb|
vb.cpus = 2
vb.memory = 2048
end
Provision
Provision 是指在虚拟机初次创建的时候,Vagrant 自动去执行的构造任务,比如安装软件,更新系统配置等。
因为 box 往往只提供基础的系统(虽然我们可以自定义 box,但是并不是每次都要这么做,而且这样做会丧失一部分灵活性),有些东西仍然需要在创建虚拟机的时候完成。
# Enable provisioning with a shell script. Additional provisioners such as
# Ansible, Chef, Docker, Puppet and Salt are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
因为这部分完全是个开放的内容,所以我们这里不过多讨论,来看一下什么情况下会触发 provision 的操作:
- 某个环境初次执行 vagrant up 的时候
- 执行 vagrant provision 命令
- 重启的时候 vagrant reload --provision,带上 --provision 选项
除了上面这些默认提示给出的配置项,Vagrantfile 还支持其它很多配置,具体请 查看文档。
执行内部脚本:
Vagrant::Config.run do |config|
config.vm.provision :shell, :inline => "echo abc > /tmp/test"
end
执行外部脚本:
Vagrant.configure("2") do |config|
config.vm.provision :shell, :path => "script.sh" # 脚本的路径相对于项目根,也可使用绝对路径
end
镜像打包
当你配置好开发环境后,退出并关闭虚拟机。在终端里对开发环境进行打包:
打包命令
$ vagrant package
打包完成后会在当前目录生成一个 package.box 的文件,将这个文件传给其他用户,其他用户只要添加这个 box 并用其初始化自己的开发目录就能得到一个一模一样的开发环境了。
$ vagrant halt
==> default: Attempting graceful shutdown of VM...
==> default: Forcing shutdown of VM...
$ # vagrant package –-base [虚拟机名称] –-output [打包后的box名称]
$ vagrant package --base cdh1 --output springcloud.box
==> default: Clearing any previously set forwarded ports...
==> default: Exporting VM...
==> default: Compressing package to: K:/Linux/virtualMachines/Vagrant/work/centos-dev
$ vagrant box list
centos7 (virtualbox, 0)
命令的具体说明:
vagrant package -hUsage: vagrant package [options] [name]
Options:
--base NAME virtualbox程序里面的虚拟机的名称,不是box的名字也不是Vagrantfile里面的虚拟机名称.默认是打包当前目录下面的虚拟机。
--output NAME 要打包成的box名称,不会自动添加.box后缀,要手动加. 默认值package.box--include FILE... 打包时包含的文件名,你可以把.box文件理解为一个压缩包
--vagrantfile FILE 打包时包含的Vagrantfile文件,原理和上面类似
-h, --help Print this help
例子:vagrant package –base virtualbox_vm_name –output newbox_name.box
注意:如果网络模式中使用 private_network 的话,在打包之前需要清除一下 private_network 的设置,避免不必要的错误:
sudo rm -f /etc/udev/rule.d/70-persistent-net.rules
制作完成之后直接将box文件拿到其他计算机上配置即可使用, 更多信息可以参考 官方文档。
缩小镜像体积
# 清理yum缓存
yum clean all
# 关闭 SWAP
du /swapfile # 查看是否有占用
swapoff -a # 关闭 SWAP
rm -f /swapfile
# 使用 df -h 命令查看磁盘占用情况
df -h
物理操作方法
使用zerofree清理文件
- 虚拟机中的系统先安装zerofree
yum install -y zerofree - 进入虚拟机系统执行
umount /dev/sda1 zerofree -v /dev/sda1
zerofree要求操作的磁盘设备不能以rw的方式mount,所以要进入单用户模式(Ubuntu进入单用户模式可以在启动时长按Shift键,然后会出现grub菜单,选择recover模式,进入root shell)
使用dd的方式清理
不使用zerofree的情况下,使用dd的方式清理
- vagrant ssh
- 碎片整理
sudo dd if=/dev/zero of=/EMPTY bs=1M sudo rm -f /EMPTY
压缩磁盘
- vagrant halt
- 压缩镜像大小
- 如果你的虚拟硬盘是 VirtualBox 的 VDI 格式,找到你的虚拟硬盘文件,执行命令:
VBoxManage modifyhd mydisk.vdi --compact - 如果你的虚拟硬盘是 Vmware 的 VMDK 格式,那就要麻烦点,因为 VirtualBox 不支持直接压缩 VMDK 格式,但是可以变通下:先转换成 VDI 并压缩,再转回 VMDK。执行命令:
VBoxManage clonehd "source.vmdk" "cloned.vdi" --format vdi VBoxManage modifyhd cloned.vdi --compact VBoxManage clonehd "cloned.vdi" "compressed.vmdk" --format vmdk - VMDK 的压缩,也可以使用 vmware-vdiskmanager,只需要一条命令
vmware-vdiskmanager -k disk.vmdk
- 如果你的虚拟硬盘是 VirtualBox 的 VDI 格式,找到你的虚拟硬盘文件,执行命令:
- virtualbox 中找到虚拟机 菜单:设置->存储 原有的 vmdk 镜像删掉,重新添加新转换后的镜像 然后确定
- vagrant up 尝试下能否启动
- vagrant package 导出。镜像明显减小。
清理脚本
!/bin/sh
# Credits to:
# - http://vstone.eu/reducing-vagrant-box-size/
# - https://github.com/mitchellh/vagrant/issues/343
# - https://gist.github.com/adrienbrault/3775253
## for vagrant related tasks, uncomment vagrant comments
# vagrant: Unmount project
#printf "STEP: vagrant: Unmount project\n"
#umount /vagrant
# Remove APT cache
printf "STEP: Remove APT cache\n"
apt-get clean -y
apt-get autoclean -y
# Zero free space to aid VM compression
printf "STEP: Zero free space to aid VM compression\n"
dd if=/dev/zero of=/EMPTY bs=1M
rm -f /EMPTY
# Remove APT files
printf "STEP: Remove APT files\n"
find /var/lib/apt -type f | xargs rm -f
# Remove documentation files
printf "STEP: Remove documentation files\n"
find /var/lib/doc -type f | xargs rm -f
# vagrant: Remove Virtualbox specific files
#printf "STEP: vagrant: Remove Virtualbox specific files\n"
#rm -rf /usr/src/vboxguest* /usr/src/virtualbox-ose-guest*
# Remove Linux headers
printf "STEP: Remove Linux headers\n"
rm -rf /usr/src/linux-headers*
# Remove Unused locales (edit for your needs, this keeps only en* and pt_BR)
printf "STEP: Remove Unused locales (edit for your needs, this keeps only en* and pt_BR and zh_CN)
find\n"
find /usr/share/locale/{af,am,ar,as,ast,az,bal,be,bg,bn,bn_IN,br,bs,byn,ca,cr,cs,csb,cy,da,de,de_AT,dz,el,en_AU,en_CA,eo,es,et,et_EE,eu,fa,fi,fo,fr,fur,ga,gez,gl,gu,haw,he,hi,hr,hu,hy,id,is,it,ja,ka,kk,km,kn,ko,kok,ku,ky,lg,lt,lv,mg,mi,mk,ml,mn,mr,ms,mt,nb,ne,nl,nn,no,nso,oc,or,pa,pl,ps,qu,ro,ru,rw,si,sk,sl,so,sq,sr,sr*latin,sv,sw,ta,te,th,ti,tig,tk,tl,tr,tt,ur,urd,ve,vi,wa,wal,wo,xh,zh,zh_HK,zh_TW,zu} -type d -delete
# Remove bash history
printf "STEP: Remove bash history\n"
unset HISTFILE
rm -f /root/.bash_history
# vagrant: Remove bash history
#printf "STEP: vagrant: Remove bash history\n"
#rm -f /home/vagrant/.bash_history
# Cleanup log files
printf "STEP: Cleanup log files\n"
find /var/log -type f | while read f; do echo -ne '' > $f; done;
# Whiteout root
printf "STEP: Whiteout root\n"
count=`df --sync -kP / | tail -n1 | awk -F ' ' '{print $4}'`;
count=$((count -= 1))
dd if=/dev/zero of=/tmp/whitespace bs=1024 count=$count;
rm /tmp/whitespace;
# Whiteout /boot
printf "STEP: Whiteout /boot\n"
count=`df --sync -kP /boot | tail -n1 | awk -F ' ' '{print $4}'`;
count=$((count -= 1))
dd if=/dev/zero of=/boot/whitespace bs=1024 count=$count;
rm /boot/whitespace;
# Whiteout swap
printf "STEP: Whiteout swap\n"
swappart=`cat /proc/swaps | tail -n1 | awk -F ' ' '{print $1}'`
swapoff $swappart;
dd if=/dev/zero of=$swappart;
mkswap $swappart;
swapon $swappart;
可以保存成 purge.sh, 然后执行即可。
插件
Vagrant 的插件主要托管在RubyGems仓库,在国内几乎无法访问。万幸的是国内已经有 RubyChina镜像。不过 Vagrant 使用这个镜像安装插件的方法有些特殊:
安装插件
# 安装插件:
vagrant plugin install vagrant-disksize
vagrantfile中这样设置:
Vagrant.configure(‘2’) do |config|
|
config.vm.box = 'centos/7'
config.disksize.size = '50GB'
磁盘扩容
新建分区
[root@localhost vagrant]# fdisk -l
Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0000ca5e
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 20479999 9726976 8e Linux LVM
[root@localhost vagrant]# fdisk /dev/sda
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.
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): 3
First sector (20480000-104857599, default 20480000):
Using default value 20480000
Last sector, +sectors or +size{K,M,G} (20480000-104857599, default 104857599):
Using default value 104857599
Partition 3 of type Linux and of size 40.2 GiB is set
Command (m for help): p
Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x0000ca5e
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 1026047 512000 83 Linux
/dev/sda2 1026048 20479999 9726976 8e Linux LVM
/dev/sda3 20480000 104857599 42188800 83 Linux
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.
刷新分区
[root@localhost vagrant]# partprobe
[root@localhost vagrant]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 50G 0 disk
├─sda1 8:1 0 500M 0 part /boot
├─sda2 8:2 0 9.3G 0 part
│ ├─centos-root 253:0 0 8.3G 0 lvm /
│ └─centos-swap 253:1 0 1000M 0 lvm [SWAP]
└─sda3 8:3 0 40.2G 0 part
创建物理卷
[root@localhost vagrant]# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created
[root@localhost vagrant]# pvdisplay
--- Physical volume ---
PV Name /dev/sda2
VG Name centos
PV Size 9.28 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 2374
Free PE 10
Allocated PE 2364
PV UUID grTv3L-mzhC-TS8a-z5ln-y6rS-3kJi-qX82hr
"/dev/sda3" is a new physical volume of "40.23 GiB"
--- NEW Physical volume ---
PV Name /dev/sda3
VG Name
PV Size 40.23 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID ysH18d-R0ML-XDBA-xM7f-rZ1i-qqN8-Pu8X0V
[root@localhost vagrant]# vgextend centos /dev/sda3 //添加新的物理卷到卷组centos
Volume group "centos" successfully extended
[root@localhost vagrant]# lvresize -L +40G /dev/mapper/centos-root //增加卷组centos的大小
Size of logical volume centos/root changed from 8.26 GiB (2114 extents) to 48.26 GiB (12354 extents).
Logical volume root successfully resized.
如果出现错误信息:Device /dev/sda3 not found (or ignored by filtering).
解决方法: 扩展逻辑卷
刷新逻辑卷centos-root
注意:(1)如果此逻辑卷上的文件系统是EXT3/EXT4类型,需要使用resize2fs工具;(2)如果此逻辑卷上的文件系统是XFS类型,需要使用 xfs_growfs
[root@localhost vagrant]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=256 agcount=4, agsize=541184 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0 finobt=0
data = bsize=4096 blocks=2164736, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
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 2164736 to 12650496
//查看扩容是否成功
[root@localhost vagrant]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 49G 1.5G 47G 3% /
devtmpfs 911M 0 911M 0% /dev
tmpfs 921M 0 921M 0% /dev/shm
tmpfs 921M 8.3M 912M 1% /run
tmpfs 921M 0 921M 0% /sys/fs/cgroup
/dev/sda1 497M 164M 334M 33% /boot
tmpfs 185M 0 185M 0% /run/user/1000
扩展逻辑卷
如果上一步执行成功,则不需要执行此步骤
# 查看是否有该分区
[root@localhost vagrant]# fdisk -l
# 执行命令:partprobe,或reboot命令重启机器
[root@localhost vagrant]# partprobe
# 查看VG Name,我自己的VG Name是centos
[root@localhost vagrant]# sudo pvdisplay | grep "VG Name"
# 将新分区扩展到centos这个组
[root@localhost vagrant]# vgextend centos /dev/sda3
Volume group "centos" sucessfully extended
# 9. 扩展逻辑分区
[root@localhost vagrant]# lvextend /dev/mapper/centos-root /dev/sda3
Size of logical volume centos/root changed from 8.26 GiB (2114 extents) to 18.02 GiB (4613 extents).
Logical volume root successfully resized.
# 10. resize并且生效
[root@localhost vagrant]# resize2fs /dev/mapper/centos-root
# 在centos 7下,这一步会出错:Couldn't find valid filesystem superblock.
# 只需要使用 xfs_growfs 命令替换 resize2fs 命令就行了,xfs_growfs /dev/mapper/centos-root
参考: