Linux磁盘管理实例
1、调整逻辑卷大小
- 预先创建2GiB的分区/dev/vdb1,并用于创建卷组testvg
- 创建大小为200MiB的逻辑卷/dev/testvg/vo,格式化为xfs文件系统,并挂载在/mnt/vo上
- 将逻辑卷/dev/testvg/vo及其文件系统大小调整到300MiB,确保文件系统内容保持不变
- // 准备工作
- [root@jlin ~]# fdisk /dev/vdb
- Welcome to fdisk (util-linux 2.32.1).
- Changes will remain in memory only, until you decide to write them.
- Be careful before using the write command.
- Device does not contain a recognized partition table.
- Created a new DOS disklabel with disk identifier 0xae75bf0a.
-
- Command (m for help): n
- Partition type
- p primary (0 primary, 0 extended, 4 free)
- e extended (container for logical partitions)
- Select (default p):
- Using default response p.
- Partition number (1-4, default 1):
- First sector (2048-10485759, default 2048):
- Last sector, +sectors or +size{K,M,G,T,P} (2048-10485759, default 10485759): +2G
- Created a new partition 1 of type 'Linux' and of size 2 GiB.
-
- Command (m for help): w
- The partition table has been altered.
- Calling ioctl() to re-read partition table.
- Syncing disks.
-
- [root@jlin ~]# pvcreate /dev/vdb1 #创建物理卷
- [root@jlin ~]# vgcreate testvg /dev/vdb1 #创建卷组并将vdb1加入到卷组
- [root@jlin ~]# lvcreate -n vo -L 200M testvg #在卷组中分出逻辑卷
- [root@jlin ~]# mkfs.xfs /dev/testvg/vo #格式化为xfs
- [root@jlin ~]# blkid /dev/testvg/vo #查看uuid
- /dev/testvg/vo: UUID="fe323058-93db-428d-883c-2c0210cd10c6" TYPE="xfs"
- [root@jlin ~]# mkdir /mnt/vo
- [root@jlin ~]# vim /etc/fstab
- UUID="fe323058-93db-428d-883c-2c0210cd10c6" /mnt/vo xfs defaults 0 0
- [root@jlin ~]# mount -a
-
- // 扩展操作
- [root@jlin ~]# df -hT /dev/testvg/vo # 查看文件系统的类型和大小
- Filesystem Type Size Used Avail Use% Mounted on
- /dev/mapper/testvg-vo xfs 195M 12M 183M 6% /mnt/vo
- [root@jlin ~]# lvextend -L 300M /dev/testvg/vo
- [root@jlin ~]# lvs
- // 扩展文件系统,ext类型的文件系统用resize2fs /dev/testvg/vo ,后面接的是逻辑卷的路径。
- [root@jlin ~]# xfs_growfs /mnt/vo # 后面接的是挂载点的路径
- [root@jlin ~]# df -hT /dev/testvg/vo
- Filesystem Type Size Used Avail Use% Mounted on
- /dev/mapper/testvg-vo xfs 295M 13M 283M 5% /mnt/vo
2、添加交换分区
在系统上添加⼀个512MiB的交换分区,设置交换分区应在系统启动时自动挂载,不要删除或修改系统 上已存在的交换分区
- [root@jlin ~]# fdisk /dev/vdb
- Welcome to fdisk (util-linux 2.32.1).
- Changes will remain in memory only, until you decide to write them.
- Be careful before using the write command.
-
- Command (m for help): n
- Partition type
- p primary (1 primary, 0 extended, 3 free)
- e extended (container for logical partitions)
- Select (default p):
- Using default response p.
- Partition number (2-4, default 2):
- First sector (4196352-10485759, default 4196352):
- Last sector, +sectors or +size{K,M,G,T,P} (4196352-10485759, default 10485759):
- +512M
- Created a new partition 2 of type 'Linux' and of size 512 MiB.
-
- Command (m for help) : t
- Partition number ( 1,2,default 2):
- Hex code (type L to list all codes) : 82
- Changed type of partition 'Linux' to 'Linux swap / Solaris '.
-
- Command (m for help): w
- The partition table has been altered.
- Syncing disks.
-
- [root@jlin ~]# mkswap /dev/vdb2
- [root@jlin ~]# blkid /dev/vdb2
- /dev/vdb2: UUID="f626f11c-4363-4caf-aba6-5a418ea04079" TYPE="swap"
- PARTUUID="ae75bf0a-02"
- [root@jlin ~]# vim /etc/fstab
- UUID=f626f11c-4363-4caf-aba6-5a418ea04079 swap swap defaults 0 0
- [root@jlin ~]# swapon -a
- [root@jlin ~]# swapon -s
- Filename Type Size Used Priority
- /dev/vdb2 partition 524284 0 -2
3、创建逻辑卷
根据以下要求,创建新的逻辑卷:
- 逻辑卷的名字为mylv,属于myvg卷组,大小为50个pe
- 卷组myvg中的逻辑卷的pe大小应当为16MiB
- 使用vfat文件系统将逻辑卷mylv格式化
- 此逻辑卷应当在系统启动时自动挂载到/mnt/mydata目录下
- [root@jlin ~]# fdisk /dev/vdb
- Welcome to fdisk (util-linux 2.32.1).
- Changes will remain in memory only, until you decide to write them.
- Be careful before using the write command.
-
- Command (m for help): n
- Partition type
- p primary (2 primary, 0 extended, 2 free)
- e extended (container for logical partitions)
- Select (default p):
- Using default response p.
- Partition number (3,4, default 3):
- First sector (5244928-10485759, default 5244928):
- Last sector, +sectors or +size{K,M,G,T,P} (5244928-10485759, default 10485759):
- +1G
- Created a new partition 3 of type 'Linux' and of size 1 GiB.
-
- Command (m for help) : t
- Partition number ( i-3,default 3):
- Hex code (type L to list all codes) : 8e
- Changed type of partition 'Linux' to 'Linux LVM' .
-
- Command (m for help): w
- The partition table has been altered.
- Syncing disks.
-
- [root@jlin ~]# pvcreate /dev/vdb3
- [root@jlin ~]# vgcreate -s 16M myvg /dev/vdb3
- [root@jlin ~]# lvcreate -l 50 -n mylv myvg
- [root@jlin ~]# mkfs.vfat /dev/myvg/mylv
- [root@jlin ~]#blkid /dev/ myvg/mylv
- /dev/myvg/mylv: UUID="5AA3-4EB3" TYPE="vfat"
- [root@jlin ~]# vim /etc/fstab
- UUID="5AA3-4EB3" /mnt/mydata vfat defaults 0 0
- [root@jlin ~]# mkdir /mnt/mydata
- [root@jlin ~]# mount -a
- [root@jlin ~]# df -h /mnt/mydata/
- Filesystem Size Used Avail Use% Mounted on
- /dev/myvg/mylv 799M 4.0K 799M 1% /mnt/mydata
4、创建VDO卷
根据如下要求,创建新的VDO卷:
- 使⽤未分区的磁盘(/dev/vdc)
- 此VDO卷的名称为myvdo
- 此VDO卷的逻辑大小为50G
- 此VDO卷使用xfs文件系统格式化
- 此VDO卷在系统启动自动挂载到/vblock目录下
- [root@jlin ~]# vdo create --name=myvdo --device=/dev/vdc --vdoLogicalSize=50G
- // 命令man vdo 找example
- Creating VDO myvdo
- Starting VDO myvdo
- Starting compression on VDO myvdo
- VDO instance 0 volume is ready at /dev/mapper/myvdo
- [root@jlin ~]# mkfs.xfs /dev/mapper/myvdo
- [root@jlin ~]# blkid /dev/ mapper/myvdo
- /dev/mapper/myvdo: UUID="0alf2efd-141b-4e7c-98ff-e75ff73515a3" TYPE="xfs"
- [root@jlin ~]# vim /etc/fstab
- UUID="0alf2efd-141b-4e7c-98ff-e75ff73515a3" /vblock xfs defaults,x-systemd.requires=vdo.service 0 0
- [root@jlin ~]# mkdir /vblock
- [root@jlin ~]# mount -a
- [root@jlin ~]# df -h /vblock
- Filesystem Size Used Avail Use% Mounted on
- /dev/mapper/myvdo 50G 390M 50G 1% /vblock