磁盘分区扩容

VirtualBox项目磁盘扩容

# 进入VitualBox根目录
$ VBoxManage showhdinfo "D:\VirtualBoxVMs\CentOS7\CentOS7.vdi"
UUID:           f4665c38-a187-45bb-8f8a-5b751d30cce3
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       D:\VirtualBoxVMs\CentOS7\CentOS7.vdi
Storage format: VDI
Format variant: dynamic default
Capacity:       8192 MBytes
Size on disk:   1416 MBytes
Encryption:     disabled
In use by VMs:  CentOS7 (UUID: 662c7511-1e68-4d91-8b95-e66c02ca8532)

计算磁盘扩容后的字节大小:1024 x 1024 x 1024 x 20 = 21474836480

λ VBoxManage modifymedium "D:\VirtualBoxVMs\CentOS7\CentOS7.vdi" --resizebyte 21474836480
0%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100%

查看分区与磁盘情况

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda1        40G   34G  4.2G  89% /
devtmpfs        3.9G     0  3.9G   0% /dev
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           3.9G  656K  3.9G   1% /run
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
tmpfs           783M     0  783M   0% /run/user/0

[root@iZwz9849t7myjoqjiyopodZ wwwroot]# df -TH
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda1      ext4       43G   37G  4.0G  91% /
devtmpfs       devtmpfs  4.1G     0  4.1G   0% /dev
tmpfs          tmpfs     4.2G     0  4.2G   0% /dev/shm
tmpfs          tmpfs     4.2G  672k  4.2G   1% /run
tmpfs          tmpfs     4.2G     0  4.2G   0% /sys/fs/cgroup
tmpfs          tmpfs     821M     0  821M   0% /run/user/0

/dev/vda1使用89%

查看文件夹使用大小

[root@iZwz9849t7myjoqjiyopodZ home]# du -h --max-depth=1 wwwroot/
666M    wwwroot/yxkwx-admin
88K wwwroot/default
4.9G    wwwroot/yxkwx-res2
1.2G    wwwroot/yxkwx
6.8G    wwwroot/
[root@iZwz9849t7myjoqjiyopodZ home]# du -sm * | sort -n;
1   default
666 yxkwx-admin
1271    yxkwx
5319    yxkwx-res2

查看分区

$ lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    253:0    0  300G  0 disk 
└─vda1 253:1    0   40G  0 part /

分区扩容

[root@iZwz9849t7myjoqjiyopodZ home]# fdisk -l

Disk /dev/vda: 322.1 GB, 322122547200 bytes, 629145600 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: 0x000a7708

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    83884031    41940992   83  Linux

查看分区正在使用的用户

[root@iZwz9849t7myjoqjiyopodZ ~]# fuser -h
Usage: fuser [-fMuvw] [-a|-s] [-4|-6] [-c|-m|-n SPACE] [-k [-i] [-SIGNAL]] NAME...
       fuser -l
       fuser -V
Show which processes use the named files, sockets, or filesystems.

  -a,--all              display unused files too
  -i,--interactive      杀死进程之前进行询问
  -k,--kill             终止访问命名文件得进程
  -l,--list-signals     list available signal names
  -m,--mount            显示所有进程正在使用已命名得文件系统或块设备
  -M,--ismountpoint     fulfill request only if NAME is a mount point
  -n,--namespace SPACE  search in this name space (file, udp, or tcp)
  -s,--silent           silent operation
  -SIGNAL               send this signal instead of SIGKILL
  -u,--user             display user IDs
  -v,--verbose          输出详情
  -w,--writeonly        kill only processes with write access
  -V,--version          display version information
  -4,--ipv4             search IPv4 sockets only
  -6,--ipv6             search IPv6 sockets only
  -                     reset options

  udp/tcp names: [local_port][,[rmt_host][,[rmt_port]]]

查看当前分区下正在文件情况

[root@iZwz9849t7myjoqjiyopodZ ~]# fuser -m -v /
                     USER        PID ACCESS COMMAND
/:                   root     kernel mount /
                     root          1 .rce. systemd
                     root          2 .rc.. kthreadd

关闭掉使用分区的服务

[root@iZwz9849t7myjoqjiyopodZ ~]# fdisk /dev/vda
Command (m for help): m
a  toggle a booable 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)

卸载掉磁盘重新分区

[root@iZwz9849t7myjoqjiyopodZ ~]# fdisk /dev/vda1

# 查看磁柱号,显示磁盘当前的分区表。
Command (m for help): p
# 删除之前分区
Command (m for help): d
# 建立新分区
Command (m for help): n
# 主分区
Command (m for help): p
# 第一个主分区
Command (m for help): 1
# 保存分区并退出
Command (m for help): wq

注意:删除之前得分区然后建立新分区,注意开始得磁柱要和原来的保持一致,以保证数据不丢失的关键步骤,结束的磁柱号默认,回车使用全部磁盘。

将新分区修改未LVM卷的文件系统

[root@iZwz9849t7myjoqjiyopodZ ~]# fdisk /dev/vda1
Command (m for help): t
Command (m for help): 8e
Command (m for help): w

重启

[root@iZwz9849t7myjoqjiyopodZ ~]# reboot

格式化分区

[root@iZwz9849t7myjoqjiyopodZ ~]# mkfs.ext4 /dev/vda1

新增主分区

[root@iZwz9849t7myjoqjiyopodZ ~]# fdisk /dev/vda1
# 查看磁柱号,显示磁盘当前的分区表。
Command (m for help): p
# 建立新分区
Command (m for help): n
# 主分区
Command (m for help): p
# 第一个主分区
Command (m for help): 1
# 保存分区并退出
Command (m for help): wq

创建PV物理卷

# 刷新分区
[root@iZwz9849t7myjoqjiyopodZ ~]# partprobe
[root@iZwz9849t7myjoqjiyopodZ ~]# partprobe /dev/vda

创建物理卷

[root@iZwz9849t7myjoqjiyopodZ ~]# pvcreate /dev/vda1

查询物理卷,获取本机物理卷名称。本例默认为centos。

[root@iZwz9849t7myjoqjiyopodZ ~]# vgscan

查看物理卷组

[root@iZwz9849t7myjoqjiyopodZ ~]# pvdisplay

查看当前卷组情况

[root@iZwz9849t7myjoqjiyopodZ ~]# vgdisplay

将新的物理卷加载到根目录分区centos中(原始卷组),使用新增的物理卷扩展本机物理卷组centos。

[root@iZwz9849t7myjoqjiyopodZ ~]# vgextend centos /dev/vda1

查看当前逻辑卷的空间状态

[root@iZwz9849t7myjoqjiyopodZ ~]# lvdisplay

将VG中多余的空间添加到分区中

[root@iZwz9849t7myjoqjiyopodZ ~]# lvextend -L +500M /dev/vda1
···

将卷组中的空闲空间扩展到根分区逻辑卷
···shell
[root@iZwz9849t7myjoqjiyopodZ ~]# lvextend -l +100%FREE /dev/vda1

对文件系统进行扩容,重新识别centos大小

[root@iZwz9849t7myjoqjiyopodZ ~]# xfs_growfs /dev/mapper/centos-root
# 或者使用
[root@iZwz9849t7myjoqjiyopodZ ~]# resize2fs -f /dev/mapper/centos-root

检查分区信息

[root@iZwz9849t7myjoqjiyopodZ ~]# e2fsck -f /dev/vda1

文件系统重定义大小,根据机器配置不同所需时间也不同。

[root@iZwz9849t7myjoqjiyopodZ ~]# resize2fs /dev/vda1

重新挂载分区

[root@iZwz9849t7myjoqjiyopodZ ~]# mount /

查看分区结果

[root@iZwz9849t7myjoqjiyopodZ ~]# df -h

CentOS7 LVM根分区扩容

df -hT查看现有分区大小

[root@iZwz9849t7myjoqjiyopodZ wwwroot]# df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda1      ext4       40G   34G  3.8G  91% /
devtmpfs       devtmpfs  3.9G     0  3.9G   0% /dev
tmpfs          tmpfs     3.9G     0  3.9G   0% /dev/shm
tmpfs          tmpfs     3.9G  656K  3.9G   1% /run
tmpfs          tmpfs     3.9G     0  3.9G   0% /sys/fs/cgroup
tmpfs          tmpfs     783M     0  783M   0% /run/user/0

lsblk查看块设备信息

[root@iZwz9849t7myjoqjiyopodZ wwwroot]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    253:0    0  300G  0 disk 
└─vda1 253:1    0   40G  0 part /

fdisk /dev/vda创建分区

[root@iZwz9849t7myjoqjiyopodZ wwwroot]# fdisk /dev/vda
Command (m for help): p
Command (m for help): n
Command (m for help): p
Command (m for help): w

刷新分区

[root@iZwz9849t7myjoqjiyopodZ wwwroot]# partprobe /dev/vda

创建物理卷

[root@iZwz9849t7myjoqjiyopodZ wwwroot]# pvcreate /dev/vda3

查看卷组名称即卷组使用情况

[root@iZwz9849t7myjoqjiyopodZ wwwroot]# vgdisplay

将物理卷扩展到卷组

[root@iZwz9849t7myjoqjiyopodZ wwwroot]# vgextend centos /dev/vda3

查看当前逻辑卷的空间状态

[root@iZwz9849t7myjoqjiyopodZ wwwroot]# lvdisplay

将卷组中的空闲空间扩展到根分区逻辑卷

[root@iZwz9849t7myjoqjiyopodZ wwwroot]# lvextend -l +100%FREE /dev/centos/root

刷新根分区

[root@iZwz9849t7myjoqjiyopodZ wwwroot]# xfs_growfs /dev/centos/root

查看磁盘使用情况

[root@iZwz9849t7myjoqjiyopodZ wwwroot]# df -hT

CentOS 非LVM跟分区扩容

查看现有分区大小

[root@iZwz9849t7myjoqjiyopodZ ~]# df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda1      ext4       40G   34G  3.7G  91% /
devtmpfs       devtmpfs  3.9G     0  3.9G   0% /dev
tmpfs          tmpfs     3.9G     0  3.9G   0% /dev/shm
tmpfs          tmpfs     3.9G  656K  3.9G   1% /run
tmpfs          tmpfs     3.9G     0  3.9G   0% /sys/fs/cgroup
tmpfs          tmpfs     783M     0  783M   0% /run/user/0

查看扩容后磁盘状态

[root@iZwz9849t7myjoqjiyopodZ ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    253:0    0  300G  0 disk 
└─vda1 253:1    0   40G  0 part /

进行分区扩展磁盘,记住根分区起始位置和结束位置。

[root@iZwz9849t7myjoqjiyopodZ ~]# fdisk /dev/vda
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): p

Disk /dev/vda: 322.1 GB, 322122547200 bytes, 629145600 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: 0x000a7708

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    83884031    41940992   83  Linux

删除根分区,切记不要保存。

Command (m for help): d

创建新分区,使用之前分区的起始位置。

Command (m for help): n

保存退出

Command (m for help): w

若首次划分文件未被格式化,则应首先对其进行格式化。

格式化分区,如果有其它文件系统创建在此分区必须添加-f来覆盖。

[root@iZwz9849t7myjoqjiyopodZ ~]# mkfs.xfs -f /dev/vda1

首次划分分区,若未挂载文件系统,则需首先挂载文件系统。

[root@iZwz9849t7myjoqjiyopodZ ~]# mount -t xfs /dev/vda1 /data

永久挂载

[root@iZwz9849t7myjoqjiyopodZ ~]# vim /etc/fstab
/dev/sda3 /data xfs defaults 0 0

检查坏道

[root@iZwz9849t7myjoqjiyopodZ ~]# badblocks -v /dev/vda1> /data/badblock.txt

刷新分区

[root@iZwz9849t7myjoqjiyopodZ ~]# partprobe /dev/vda

查看分区状态

[root@iZwz9849t7myjoqjiyopodZ ~]# lsblk

刷新根分区

[root@iZwz9849t7myjoqjiyopodZ ~]# xfs_growfs /dev/vda1

查看文件大小

[root@iZwz9849t7myjoqjiyopodZ ~]# df -hT

启动分区扩容

[root@iZwz9849t7myjoqjiyopodZ ~]# df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda1      ext4       40G   34G  3.6G  91% /
devtmpfs       devtmpfs  3.9G     0  3.9G   0% /dev
tmpfs          tmpfs     3.9G     0  3.9G   0% /dev/shm
tmpfs          tmpfs     3.9G  656K  3.9G   1% /run
tmpfs          tmpfs     3.9G     0  3.9G   0% /sys/fs/cgroup
tmpfs          tmpfs     783M     0  783M   0% /run/user/0

[root@iZwz9849t7myjoqjiyopodZ ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    253:0    0  300G  0 disk 
└─vda1 253:1    0   40G  0 part /

查看现有分区大小

[root@iZwz9849t7myjoqjiyopodZ ~]# df -hT
Filesystem     Type      Size  Used Avail Use% Mounted on
/dev/vda1      ext4       40G   34G  3.7G  91% /
devtmpfs       devtmpfs  3.9G     0  3.9G   0% /dev
tmpfs          tmpfs     3.9G     0  3.9G   0% /dev/shm
tmpfs          tmpfs     3.9G  656K  3.9G   1% /run
tmpfs          tmpfs     3.9G     0  3.9G   0% /sys/fs/cgroup
tmpfs          tmpfs     783M     0  783M   0% /run/user/0

查看扩容后磁盘状态

[root@iZwz9849t7myjoqjiyopodZ ~]# lsblk
NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
vda    253:0    0  300G  0 disk 
└─vda1 253:1    0   40G  0 part /

查看分区挂载情况

[root@iZwz9849t7myjoqjiyopodZ /]# cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed Dec 12 07:53:08 2018
#
# 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=d67c3b17-255b-4687-be04-f29190d37396 /                       ext4    defaults        1 1
[root@iZwz9849t7myjoqjiyopodZ /]# fdisk /dev/vda
Command (m for help): p
Command (m for help): n
Command (m for help): p
Command (m for help): w

[root@iZwz9849t7myjoqjiyopodZ /]# partprobe /dev/vda

[root@iZwz9849t7myjoqjiyopodZ /]# mkfs.ext4 /dev/vda2

# 挂载会覆盖原始文件
[root@iZwz9849t7myjoqjiyopodZ /]# mount -t ext4 /dev/vda2 /home
# 还原挂载文件
[root@iZwz9849t7myjoqjiyopodZ /]# umount -v /dev/vda2 

# 创建新文件进行挂载,挂载后拷贝文件。
[root@iZwz9849t7myjoqjiyopodZ /]# mkdir /project
[root@iZwz9849t7myjoqjiyopodZ /]# cp -r olddir newdir
[root@iZwz9849t7myjoqjiyopodZ /]# mount -t ext4 /dev/vda2 /project

[root@iZwz9849t7myjoqjiyopodZ /]# blkid
/dev/vda1: UUID="d67c3b17-255b-4687-be04-f29190d37396" TYPE="ext4" 

[root@iZwz9849t7myjoqjiyopodZ /]# vi /etc/fstab
/dev/vda2 /project ext4 defaults 0 0

清空日志文件

[root@iZwz9849t7myjoqjiyopodZ /]# > test.log

你可能感兴趣的:(磁盘分区扩容)