1.添加磁盘
物理机:购买硬盘并连接至设备
虚拟机:添加磁盘
2.reboot重启,然后查看磁盘是否添加成功
lsblk //查看磁盘分区情况
磁盘为sda、sdb以此类推
举例:
esu@esu:~$ lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
fd0 2:0 1 4K 0 disk
sda 8:0 0 16G 0 disk
├─sda1 8:1 0 487M 0 part /boot
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 15.5G 0 part
├─esu--vg-root 252:0 0 13.5G 0 lvm /
└─esu--vg-swap_1 252:1 0 2G 0 lvm [SWAP]
sdb 8:16 0 4G 0 disk
sr0 11:0 1 825M 0 rom
sdb为新增加的磁盘,还未分区
3.为新增加的磁盘分区
fdisk /dev/sdb
fdisk命令详解:
DOS (MBR)
a toggle a bootable flag
b edit nested BSD disklabel
c toggle the dos compatibility flag
Generic
d delete a partition //删除一个分区
F list free unpartitioned space
l list known partition types
n add a new partition //新增一个分区
p print the partition table //显示分区表
t change a partition type //更改分区类型
v verify the partition table
i print information about a partition
Misc
m print this menu //显示帮助菜单
u change display/entry units
x extra functionality (experts only)
Script
I load disk layout from sfdisk script file
O dump disk layout to sfdisk script file
Save & Exit
w write table to disk and exit //保存更改并退出
q quit without saving changes
Create a new label
g create a new empty GPT partition table
G create a new empty SGI (IRIX) partition table
o create a new empty DOS partition table
s create a new empty Sun partition table
输入 n 新建分区后,依次选择分区类型、分区号、分区头、分区尾,即可创建新分区
Command (m for help): p
Disk /dev/sdb: 4 GiB, 4294967296 bytes, 8388608 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
Disklabel type: dos
Disk identifier: 0x7290aae2
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 8388607 8386560 4G 83 Linux
sdb1即为新建的分区
4.格式化分区,为分区分配文件系统
mkfs -t ext4 /dev/sdb1
5.将新分区临时挂载至需要的文件目录下
mount /dev/sdb1 /home/esu/newdisk/
将新分区永久挂载至需要的文件目录下
vim /etc/fstab
按格式添加分区与挂载目录并保存退出
执行以下命令使更改生效
mount -a
6.查看磁盘使用情况
df -h
查看指定目录的磁盘使用情况
du -h [path]
新增将新分区加入vg-lv的相关内容:
新分区建立完成后,不要格式化该分区为ext4,重新进入分区界面
fdisk /dev/sdb
输入t将分区格式改为lvm
Command (m for help): t
Partition number (1-4, default 4):
Partition type or alias (type L to list all): lvm
Changed type of partition 'Linux filesystem' to 'Linux LVM'.
注:如输入lvm不成功则输入L列出所有类型,然后选择lvm对应的序号
输入w写入分区表
Command (m for help): w
The partition table has been altered.
Syncing disks.
创建PV(物理卷)
$ sudo pvcreate /dev/sdb1
Physical volume "/dev/sda4" successfully created.
查看PV
$ sudo pvdisplay
"/dev/sda4" is a new physical volume of "9.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 4.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
扩容vg(卷组)
$ sudo vgextend ubuntu-vg /dev/sda4
Volume group "ubuntu-vg" successfully extended
注:命令中"ubuntu-vg","/dev/sda4"等要做针对性修改,修改为自己实际的卷组名称和分区
查看卷组
$ sudo vgdisplay
--- Volume group ---
VG Name ubuntu-vg
System ID
Format lvm2
VG Size 23.24 GiB
PE Size 4.00 MiB
Total PE 5950
Alloc PE / Size 3647 / <14.25 GiB
Free PE / Size 2303 / <9.00 GiB
扩容lv(逻辑卷)
$ sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
Size of logical volume ubuntu-vg/ubuntu-lv changed from <14.25 GiB (3647 extents) to 23.24 GiB (5950 extents).
Logical volume ubuntu-vg/ubuntu-lv successfully resized.
查看逻辑卷
$ sudo lvdisplay
--- Volume group ---
VG Name ubuntu-vg
System ID
Format lvm2
VG Size 23.24 GiB
PE Size 4.00 MiB
Total PE 5950
Alloc PE / Size 5950 / 23.24 GiB
Free PE / Size 0 / 0
执行修改
$ sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
resize2fs 1.46.5 (30-Dec-2021)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 6092800 (4k) blocks long.
查看结果
$ df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 197M 1.6M 195M 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv 23G 9.2G 13G 43% /
tmpfs 983M 0 983M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
/dev/sda2 1.7G 253M 1.4G 16% /boot
tmpfs 197M 4.0K 197M 1% /run/user/1000