VMware虚拟机下为Ubuntu添加磁盘

时间:2023-03-10 07:23:55
VMware虚拟机下为Ubuntu添加磁盘

20G的磁盘还是不够用啊,正好复习下磁盘分区和逻辑卷。

关闭虚拟机,打开VMware,右键虚拟机点击设置,点下下方的添加,就可以添加磁盘了。

进入虚拟机,查看:

root@ubuntu:/# fdisk -l
Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 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: 0xe0f58c4a Device Boot Start End Sectors Size Id Type
/dev/sda1 * 2048 39845887 39843840 19G 83 Linux
/dev/sda2 39847934 41940991 2093058 1022M 5 Extended
/dev/sda5 39847936 41940991 2093056 1022M 82 Linux swap / Solaris Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 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

新的磁盘/dev/sdb已添加。

创建分区

创建一个5G分区:

root@ubuntu:/# fdisk /dev/sdb
Command (m for help): n
Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 3
First sector (2048-20971519, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971519, default 20971519): +5G Created a new partition 3 of type 'Linux' and of size 5 GiB.

格式修改为8e(逻辑卷必须):

Command (m for help): t

Selected partition 3
Partition type (type L to list all types): 8e
Changed type of partition 'Linux' to 'Linux LVM'. Command (m for help): p
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 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: 0x1df1b6c7 Device Boot Start End Sectors Size Id Type
/dev/sdb3 2048 10487807 10485760 5G 8e Linux LVM

保存:

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

创建物理卷PV

root@ubuntu:/# pvcreate /dev/sdb3
Physical volume "/dev/sdb3" successfully created
root@ubuntu:/# pvdisplay
"/dev/sdb3" is a new physical volume of "5.00 GiB"
--- NEW Physical volume ---
PV Name /dev/sdb3
VG Name
PV Size 5.00 GiB
Allocatable NO
PE Size 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID 2rydZg-wXnl-vBlo-EhZ7-T2YC-CRoc-Gw3NyP root@ubuntu:/# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb3 lvm2 --- 5.00g 5.00g

创建卷组VG

root@ubuntu:/# vgcreate vgvg /dev/sdb3
Volume group "vgvg" successfully created
root@ubuntu:/# vgdisplay
--- Volume group ---
VG Name vgvg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 1
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 1
Act PV 1
VG Size 5.00 GiB
PE Size 4.00 MiB
Total PE 1279
Alloc PE / Size 0 / 0
Free PE / Size 1279 / 5.00 GiB
VG UUID t9Ehku-ALdl-FCjl-sakI-sqDe-vx4H-lLqmIz root@ubuntu:/# vgs
VG #PV #LV #SN Attr VSize VFree
vgvg 1 0 0 wz--n- 5.00g 5.00g

创建逻辑卷LV

root@ubuntu:/# lvcreate -L 4G -n lvlv vgvg
Logical volume "lvlv" created.
root@ubuntu:/# lvdisplay
--- Logical volume ---
LV Path /dev/vgvg/lvlv
LV Name lvlv
VG Name vgvg
LV UUID nP2sGc-KGyU-zJZ1-IFk7-aMjC-39tm-sdgkue
LV Write Access read/write
LV Creation host, time ubuntu, 2018-01-06 10:20:19 -0800
LV Status available
# open 0
LV Size 4.00 GiB
Current LE 1024
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0 root@ubuntu:/# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lvlv vgvg -wi-a----- 4.00g

格式化并且挂载

root@ubuntu:/# mkfs.ext4 /dev/vgvg/lvlv
mke2fs 1.42.13 (17-May-2015)
Creating filesystem with 1048576 4k blocks and 262144 inodes
Filesystem UUID: fcffe231-a2b5-429c-b95c-1884cef6cfeb
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736 Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done root@ubuntu:/# mkdir /lvmdata
root@ubuntu:/# mount /dev/vgvg/lvlv /lvmdata

快照卷命令:

lvcreate
-s :指定快照卷;
-p r:限定快照卷为只读;
格式: lvcreate -L SIZE-s -p r -n LV_NAME /path/to/lv

创建快照卷:

lvcreate -L 100M -s -p r -n lvm-snap /dev/vgvg/lvlv

挂载:

mount -o ro /dev/vgvg/lvm-snap  /media/

删除:

umount /media/
lvremove /dev/vgvg/lvm-snap

要恢复快照的话,首先卸载:

umount /lvmdata/

然后使用快照恢复:

root@ubuntu:~# lvconvert --merge /dev/vgvg/lvm-snap
Merging of volume lvm-snap started.
lvlv: Merged: 100.0%
lvlv: Merged: 100.0%

再挂载即可:

root@ubuntu:~# mount /dev/vgvg/lvlv /lvmdata/

内容已恢复快照内容。