Linux 給根目錄新增磁盤空間
最近安裝虛擬機時老是發現磁盤空間不夠,但是因為網上的方法感覺都不是很全,因此整理一份詳細的攻略,以作留存。
虛擬機軟件:Oracle VM VirtualBox v5
系統:centos 7
1.關閉虛擬機,找到虛擬機的安裝目錄
cmd 進入安裝目錄下
C:\Users\ft>d: D:\>cd D:\ft D:\ft>
2.查看虛擬機硬盤狀態,找到需要改變的虛擬機,我這邊是salve1
D:\ft>VBoxManage list hdds UUID: 43fe3803-36e7-4db7-9877-63f19dec6ee0 Parent UUID: baseState: locked write Type: normal (base) Location: C:\Users\ft\VirtualBox VMs\master\master.vdi Storage format: VDI Capacity: 8192 MBytes Encryption: disabled UUID: f100d1ba-bd50-40ac-a608-8c5c4b428995 Parent UUID: base State: created Type: normal (base) Location: C:\Users\ft\VirtualBox VMs\slave2\slave2.vdi Storage format: VDI Capacity:8192 MBytes Encryption: disabled UUID: dc4a20c8-ef7a-4376-b535-7a586b25820d Parent UUID: base State: created Type: normal (base) Location: C:\Users\ft\VirtualBox VMs\slave1\slave1.vdi Storage format: VDI Capacity: 8192 MBytes Encryption: disabled
slave1原本為8G,現在將其改為18G,記錄下它的UUID
3.增加硬盤容量
D:\fangtao>VBoxManage modifyhd dc4a20c8-ef7a-4376-b535-7a586b25820d --resize 181920%...10%...20%...30%...40%...50%...60%...70%...80%...90%...100% D:\fangtao>
其中,dc開頭的代碼為需要修改虛擬機的UUID, 18192 為修改後的虛擬機大小,再次查看虛擬機硬盤狀態,發現硬盤已經變大
D:\ft>VBoxManage list hdds UUID: 43fe3803-36e7-4db7-9877-63f19dec6ee0 Parent UUID: base State: locked write Type: normal (base) Location: C:\Users\ft\VirtualBox VMs\master\master.vdi Storage format: VDI Capacity: 8192 MBytes Encryption: disabled UUID: f100d1ba-bd50-40ac-a608-8c5c4b428995 Parent UUID: base State: created Type: normal (base) Location: C:\Users\ft\VirtualBox VMs\slave2\slave2.vdi Storage format: VDI Capacity: 8192 MBytes Encryption: disabled UUID: dc4a20c8-ef7a-4376-b535-7a586b25820d Parent UUID: base State: created Type: normal (base) Location: C:\Users\ft\VirtualBox VMs\slave1\slave1.vdi Storage format: VDI Capacity: 18192 MBytes Encryption: disabled
4.創建分區
打開虛擬機(若原本是休眠狀態的,需要重啟)
[root@slave1 ~]# df -h #容量依舊是6G 文件系統 容量 已用 可用 已用% 掛載點 /dev/mapper/cl-root 6.2G 5.8G 441M 94% / devtmpfs 481M 0 481M 0% /dev tmpfs 497M 156K 497M 1% /dev/shm tmpfs 497M 7.0M 490M 2% /run tmpfs 497M 0 497M 0% /sys/fs/cgroup /dev/sda1 1014M 173M 842M 18% /boot tmpfs 100M 28K 100M 1% /run/user/1000
[root@slave1 ~]# fdisk -l /dev/sda # 查看磁盤分區表
磁盤 /dev/sda:19.1 GB, 19075694592 字節,37257216 個扇區 #新增的
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節
磁盤標簽類型:dos
磁盤標識符:0x0000f022
設備 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 16777215 7339008 8e Linux LVM
[root@slave1 ~]# fdisk /dev/sda
歡迎使用 fdisk (util-linux 2.23.2)。
更改將停留在內存中,直到您決定將更改寫入磁盤。
使用寫入命令前請三思。
命令(輸入 m 獲取幫助):n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): p
分區號 (3,4,默認 3):
起始 扇區 (16777216-37257215,默認為 16777216):
將使用默認值 16777216
Last 扇區, +扇區 or +size{K,M,G} (16777216-37257215,默認為 37257215):
將使用默認值 37257215
分區 3 已設置為 Linux 類型,大小設為 9.8 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)
正在同步磁盤。
[root@slave1 ~]# fdisk -l /dev/sda
磁盤 /dev/sda:19.1 GB, 19075694592 字節,37257216 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節
磁盤標簽類型:dos
磁盤標識符:0x0000f022
設備 Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 16777215 7339008 8e Linux LVM
/dev/sda3 16777216 37257215 10240000 83 Linux #新增
[root@slave1 ~]#
[root@slave1 ~]# partprobe #接收新的分區表信息 也可以直接重啟
[root@slave1 ~]# mkfs.ext4 /dev/sda3 #將分區格式化為ext4格式
mke2fs 1.42.9 (28-Dec-2013)
文件系統標簽=
OS type: Linux
塊大小=4096 (log=2)
分塊大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
640848 inodes, 2560000 blocks
128000 blocks (5.00%) reserved for the super user
第一個數據塊=0
Maximum filesystem blocks=2151677952
79 block groups
32768 blocks per group, 32768 fragments per group
8112 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables: 完成
正在寫入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
[root@slave1 ~]#
[root@slave1 ~]# vgdisplay #查看卷組名
--- Volume group ---
VG Name cl #記錄下cl
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 1
Act PV 1
VG Size 7.00 GiB
PE Size 4.00 MiB
Total PE 1791
Alloc PE / Size 1791 / 7.00 GiB
Free PE / Size 0 / 0
VG UUID FCGIuD-oCl3-NOws-Gpot-eyHi-41zz-p4mxmG
[root@slave1 ~]# pvcreate /dev/sda3 #創建新物理卷
WARNING: ext4 signature detected on /dev/sda3 at offset 1080. Wipe it? [y/n]: y
Wiping ext4 signature on /dev/sda3.
Physical volume "/dev/sda3" successfully created.
[root@slave1 ~]# vgextend cl /dev/sda3 #擴展到卷組
Volume group "cl" successfully extended
[root@slave1 ~]# lvdisplay #查看根分區
--- Logical volume ---
LV Path /dev/cl/swap
LV Name swap
VG Name cl
LV UUID 95DKbg-Uh4p-bc7P-e6ql-L4Zc-wLJV-McwpRQ
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2019-02-18 16:05:35 +0800
LV Status available
# open 2
LV Size 820.00 MiB
Current LE 205
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:1
--- Logical volume ---
LV Path /dev/cl/root #記錄下
LV Name root
VG Name cl
LV UUID p7T17B-ozhs-Cqez-dG1W-LrHk-X1SM-lJhzwf
LV Write Access read/write
LV Creation host, time localhost.localdomain, 2019-02-18 16:05:35 +0800
LV Status available
# open 1
LV Size 6.20 GiB
Current LE 1586
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:0
[root@slave1 ~]# lvextend /dev/cl/root /dev/sda3 #擴展到容量邏輯分區
Size of logical volume cl/root changed from 6.20 GiB (1586 extents) to 15.96 GiB (4085 extents).
Logical volume cl/root successfully resized.
[root@slave1 ~]# resize2fs /dev/cl/root # 刷新邏輯分區
resize2fs 1.42.9 (28-Dec-2013)
resize2fs: Bad magic number in super-block 當嘗試打開 /dev/cl/root 時
找不到有效的文件系統超級塊.
[root@slave1 ~]# cat /etc/fstab #若報錯,查看文件系統是不是xfs 的
#
# /etc/fstab
# Created by anaconda on Mon Feb 18 16:05:37 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
#
/dev/mapper/cl-root / xfs defaults 0 0
UUID=8307e61c-9c4f-4aa0-abe8-fe985cbe6a5d /boot xfs defaults 0 0
/dev/mapper/cl-swap swap swap defaults 0 0
[root@slave1 ~]# xfs_growfs /dev/cl/root #若是,則用該命令
meta-data=/dev/mapper/cl-root isize=512 agcount=4, agsize=406016 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=1624064, 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 1624064 to 4183040
[root@slave1 ~]# df -h #查看根目錄
文件系統 容量 已用 可用 已用% 掛載點
/dev/mapper/cl-root 16G 5.8G 11G 37% /
devtmpfs 481M 0 481M 0% /dev
tmpfs 497M 84K 497M 1% /dev/shm
tmpfs 497M 7.0M 490M 2% /run
tmpfs 497M 0 497M 0% /sys/fs/cgroup
/dev/sda1 1014M 173M 842M 18% /boot
tmpfs 100M 16K 100M 1% /run/user/42
tmpfs 100M 0 100M 0% /run/user/1000
tmpfs 100M 0 100M 0% /run/user/0
[root@slave1 ~]# reboot #重啟
Linux 給根目錄新增磁盤空間