您好,登錄后才能下訂單哦!
CentOS中怎么擴容虛擬機根分區磁盤,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
一、關閉你的虛擬機系統
查看鏡像大小
qemu-img info test.img
給虛擬機鏡像擴容40G
dd if =/dev/zero bs=1024M count=40 >>test.img
使用qemu-img info再看看鏡像大小
二、啟動虛擬機使用linux下的fdisk工具進行分區
用root用戶登錄到你的linux系統,查看你系統的分區
# fdisk -l
Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00008d26
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 2611 20458496 8e Linux LVM
Disk /dev/mapper/VolGroup-lv_root: 18.9 GB, 18865979392 bytes
255 heads, 63 sectors/track, 2293 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/VolGroup-lv_swap: 2080 MB, 2080374784 bytes
255 heads, 63 sectors/track, 252 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
根據提示信息可以判斷出此系統的磁盤接口為SCSI,對應“sda”如果上面的紅色字體是“hda”,那么此系統的磁盤接口為IDE對應“hda”所以我做一下操作:
# fdisk /dev/sda
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): m //列出fdisk的幫助
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
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)
Command (m for help): n //命令n用于添加新分區
Command action
e extended
p primary partition (1-4)
p //選擇創建主分區"此時
Partition number (1-4): 3 //fdisk會讓你選擇主分區的編號,如果已經有了主分區sda1,sda2,那么編號就選3,即要創建的該分區為sda3
First cylinder (2611-7832, default 2611): //此時,fdisk又會讓你選擇該分區的開始值這個就是分區的Start 值(start cylinder);這里最好直接按回車
Using default value 2611
Last cylinder, +cylinders or +size{K,M,G} (2611-7832, default 7832): //此時,fdisk又會讓你選擇該分區的開始值這個就是分區的End 值這里最好直接按回車
Using default value 7832
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.
三、我們的新建分區/dev/sda3,卻不是LVM的。所以,接下來使用fdisk將其改成LVM的
# fdisk /dev/sda
Command (m for help): m
Command (m for help): t //改變分區系統id
Partition number (1-4): 3 //指定分區號
Hex code (type L to list codes): 8e //指定要改成的id號,8e代表LVM
Changed system type of partition 3 to 8e (Linux LVM)
Command (m for help): w
四、重啟系統后,登陸系統。(一定要重啟系統,否則無法擴充新分區)
五、格式化該新添加的分區
# fdisk -l
Disk /dev/sda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00008d26
Device Boot Start End Blocks Id System
/dev/sda1 * 1 64 512000 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 64 2611 20458496 8e Linux LVM
/dev/sda3 2611 7832 41939020 8e Linux LVM
Disk /dev/mapper/VolGroup-lv_root: 18.9 GB, 18865979392 bytes
255 heads, 63 sectors/track, 2293 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
Disk /dev/mapper/VolGroup-lv_swap: 2080 MB, 2080374784 bytes
255 heads, 63 sectors/track, 252 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
你會發現多了一塊分區
# mkfs -t ext3 /dev/sda3 //在硬盤分區“/dev/sda3”上創建“ext3”文件系統
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
2621440 inodes, 10484755 blocks
524237 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
320 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 23 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
此時我們就可以使用該新增分區啦:
六、擴充新分區
# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv_root VolGroup -wi-ao---- 17.57g
lv_swap VolGroup -wi-ao---- 1.94g
# pvcreate /dev/sda3
Physical volume "/dev/sda3" successfully created
# pvcreate /dev/sda3 //pvcreate指令用于將物理硬盤分區初始化為物理卷,以便被LVM使用。要創建物理卷必須首先對硬盤進行分區,并且將硬盤分區的類型設置為“8e”后,才能使用pvcreat指令將分區初始化為物理卷
Physical volume "/dev/sda3" successfully created
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 18G 1.7G 15G 10% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/sda1 485M 32M 428M 7% /boot
# vgextend /dev/mapper/VolGroup-lv_root /dev/sda3 //其中是當前需要擴充的lvm組名,可以通過df -h查看,例如我的是: /dev/mapper/VolGroup-lv_root) //vgextend指令用于動態的擴展卷組,它通過向卷組中添加物理卷來增加卷組的容量
Volume group name VolGroup/lv_root has invalid characters
# vgextend /dev/mapper/VolGroup /dev/sda3 //原來這個組名,是-之前的字符串——這要是沒有文章看著對比分析,誰知道啊?
Volume group "VolGroup" successfully extended
#vgdisplay //用于顯示LNM卷組的元數據信息
--- Volume group ---
VG Name VolGroup
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 4
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 2
Open LV 2
Max PV 0
Cur PV 2
Act PV 2
VG Size 59.50 GiB
PE Size 4.00 MiB
Total PE 15232
Alloc PE / Size 4994 / 19.51 GiB
Free PE / Size 10238 / 39.99 GiB
VG UUID Ecv3ml-86eg-LTsH-mALD-mLxW-v3Ao-GEqiFc
(主要查看Free PE / Size 10238 / 39.99 GiB,說明我們最多可以有39.99GB的擴充空間。我一般選擇小于等于39.99GB)
# lvextend -L +39.99G /dev/VolGroup/lv_root /dev/sda3
Rounding size to boundary between physical extents: 39.99 GiB
Extending logical volume lv_root to 57.56 GiB
Logical volume lv_root successfully resized
# e2fsck -f /dev/VolGroup/lv_root
e2fsck 1.41.12 (17-May-2010)
/dev/VolGroup/lv_root is mounted.
e2fsck: Cannot continue, aborting.
# resize2fs /dev/VolGroup/lv_root(如果這一步報錯了改用xfs_growfs/dev/VolGroup/lv_root原因據說是xfs文件系統)
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 4
Performing an on-line resize of /dev/VolGroup/lv_root to 15089664 (4k) blocks.
The filesystem on /dev/VolGroup/lv_root is now 15089664 blocks long.
# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root 57G 1.7G 53G 3% /
tmpfs 1.9G 0 1.9G 0% /dev/shm
/dev/sda1 485M 32M 428M 7% /boot
關于CentOS中怎么擴容虛擬機根分區磁盤問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。