赛题
2.2.3、配置动态LVM卷,允许动态调整磁盘空间大小;
l lnxserver2上安装lvm2组件,并配置如下信息:
l 在已经安装好的虚拟机上,增加一块新的硬盘,空间大小为10GB;挂载到系统上;定义为:sdb;
l 创建两个逻辑卷,大小都为4GB,/dev/sdb1,/dev/sdb2;Hex code:2d;加入到一个卷组中;默认PE SIZE:4M;分割5GB空间到逻辑卷lvdata01;剩余空间用于后期不够用后调整;
l 磁盘格式化为ext4分区;挂载到系统的lvdata下,设置成开启自动挂载;
l 在lvdata文件下生成二个大小为2.5GB和2GB大小的文件;文件名“01”,“02”;文件在文件系统中的显示大小为对应大小,但是并不实际占用block;
1.安装服务,添加磁盘
开机状态下添加硬盘,需要重启下虚拟机
root@lnxserver2:~# apt install -y lvm2
root@lnxserver2:~# reboot
root@lnxserver2:~# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 20G 0 disk
├─sda1 8:1 0 19G 0 part /
├─sda2 8:2 0 1K 0 part
└─sda5 8:5 0 975M 0 part [SWAP]
sdb 8:16 0 10G 0 disk
sr0 11:0 1 45.1G 0 rom
root@lnxserver2:~#
2.创建2个逻辑卷
root@lnxserver2:~# 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): 1
First sector (2048-20971519, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-20971519, default 20971519): +4G
Created a new partition 1 of type 'Linux' and of size 4 GiB.
Command (m for help): n
Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (8390656-20971519, default 8390656):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (8390656-20971519, default 20971519): +4G
Created a new partition 2 of type 'Linux' and of size 4 GiB.
Command (m for help): p
Disk /dev/sdb: 10 GiB, 10737418240 bytes, 20971520 sectors
Disk model: VMware Virtual S
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: 0xec6b45eb
Device Boot Start End Sectors Size Id Type
/dev/sdb1 2048 8390655 8388608 4G 83 Linux
/dev/sdb2 8390656 16779263 8388608 4G 83 Linux
Command (m for help): w
3.创建LVM卷
root@lnxserver2:~# vgcreate vg /dev/sdb[1,2]
Physical volume "/dev/sdb1" successfully created.
Physical volume "/dev/sdb2" successfully created.
Volume group "vg" successfully created
root@lnxserver2:~# lvcreate -L +5G -n lv vg
Logical volume "lv" created.
4.格式化,开机自动挂载
格式化
root@lnxserver2:~# mkfs.ext4 /dev/vg/lv
mke2fs 1.44.5 (15-Dec-2018)
Creating filesystem with 1310720 4k blocks and 327680 inodes
Filesystem UUID: 4b10c074-d349-4953-8800-e782e66ddfcd
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
root@lnxserver2:~#
创建文件夹挂载
root@lnxserver2:~# mkdir /lvdata
root@lnxserver2:~# mount /dev/vg/lv /lvdata/
root@lnxserver2:~# df -Th
/dev/mapper/vg-lv ext4 4.9G 20M 4.6G 1% /lvdata
开机自动挂载
root@lnxserver2:~# vim /etc/fstab
/dev/vg/lv /lvdata ext4 defaults 0 0
5.生成2个GB大小文件
只能用方法1,2和3会占用磁盘空间
方法1
root@lnxserver2:/lvdata# truncate -s 2560M 01
root@lnxserver2:/lvdata# truncate -s 2048M 02
root@lnxserver2:/lvdata# ls -lh
total 16K
-rw-r--r-- 1 root root 2.5G Feb 16 20:55 01
-rw-r--r-- 1 root root 2.0G Feb 16 20:55 02
drwx------ 2 root root 16K Feb 16 20:27 lost+found
root@lnxserver2:/lvdata# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg-lv ext4 4.9G 20M 4.6G 1% /lvdata
root@lnxserver2:/lvdata#
方法2
root@lnxserver2:~# cd /lvdata/
root@lnxserver2:/lvdata# dd if=/dev/zero of=01 bs=1M count=2560
2560+0 records in
2560+0 records out
2684354560 bytes (2.7 GB, 2.5 GiB) copied, 2.81672 s, 953 MB/s
root@lnxserver2:/lvdata# dd if=/dev/zero of=02 bs=1M count=2048
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB, 2.0 GiB) copied, 2.1628 s, 993 MB/s
root@lnxserver2:/lvdata# ls -l
total 4718616
-rw-r--r-- 1 root root 2684354560 Feb 16 20:46 01
-rw-r--r-- 1 root root 2147483648 Feb 16 20:46 02
drwx------ 2 root root 16384 Feb 16 20:27 lost+found
root@lnxserver2:/lvdata# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg-lv ext4 4.9G 4.6G 76M 99% /lvdata
方法3
root@lnxserver2:/lvdata# fallocate -l 2.5G 01
root@lnxserver2:/lvdata# fallocate -l 2G 02
root@lnxserver2:/lvdata# ls -lh
total 4.5G
-rw-r--r-- 1 root root 2.5G Feb 16 20:49 01
-rw-r--r-- 1 root root 2.0G Feb 16 20:49 02
drwx------ 2 root root 16K Feb 16 20:27 lost+found
root@lnxserver2:/lvdata# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg-lv ext4 4.9G 4.6G 76M 99% /lvdata