一、普通磁盘分区管理方式
1.对磁盘进行分区
列出磁盘
# fdisk -l
# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).
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
Building a new DOS disklabel with disk identifier 0x4765885b.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-2097151999, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151999, default 2097151999):
Using default value 2097151999
Partition 1 of type Linux and of size 1000 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
# fdisk -l
Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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
Disk label type: dos
Disk identifier: 0x635e6c7d
Device Boot Start End Blocks Id System
/dev/vda1 2048 83886079 41942016 83 Linux
Disk /dev/vdb: 1073.7 GB, 1073741824000 bytes, 2097152000 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
Disk label type: dos
Disk identifier: 0x4765885b
Device Boot Start End Blocks Id System
/dev/vdb1 2048 2097151999 1048574976 83 Linux
2.对磁盘格式化成ext4格式
# mkfs.ext4 /dev/vdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536000 inodes, 262143744 blocks
13107187 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2409627648
8000 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, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
3.将新的磁盘挂载在/data目录下
mkdir /data
# mount /dev/vdb1 /data
查看是否挂载成功
# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/vda1 ext4 40G 1.3G 37G 4% /
devtmpfs devtmpfs 32G 0 32G 0% /dev
tmpfs tmpfs 32G 0 32G 0% /dev/shm
tmpfs tmpfs 32G 292K 32G 1% /run
tmpfs tmpfs 32G 0 32G 0% /sys/fs/cgroup
tmpfs tmpfs 6.3G 0 6.3G 0% /run/user/0
/dev/vdb1 ext4 985G 77M 935G 1% /data
4.添加自动挂载,重启系统后依然生效
vim /etc/fstab
/dev/vdb1 /data ext4 defaults 0 0
二、逻辑卷的方式
yum install -y lvm2 vim unzip iftop
# fdisk -l
Disk /dev/vda: 42.9 GB, 42949672960 bytes, 83886080 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
Disk label type: dos
Disk identifier: 0x635e6c7d
Device Boot Start End Blocks Id System
/dev/vda1 2048 83886079 41942016 83 Linux
Disk /dev/vdb: 2147.5 GB, 2147483648000 bytes, 4194304000 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
# fdisk /dev/vdb
Welcome to fdisk (util-linux 2.23.2).
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
Building a new DOS disklabel with disk identifier 0x31ab9b87.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-4194303999, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4194303999, default 4194303999):
Using default value 4194303999
Partition 1 of type Linux and of size 2 TiB is set
# 更加规范的做法是在分区时需要一个修改磁盘分区格式为linux lvm 代码为8e的操作
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
# cat /proc/partitions
major minor #blocks name
11 0 1048575 sr0
253 0 41943040 vda
253 1 41942016 vda1
253 16 2097152000 vdb
253 17 2097150976 vdb1
# pvcreate /dev/vdb1
Physical volume "/dev/vdb1" successfully created.
# vgcreate myvg /dev/vdb1
Volume group "myvg" successfully created
# vgdisplay
--- Volume group ---
VG Name myvg
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 1.95 TiB
PE Size 4.00 MiB
Total PE 511999
Alloc PE / Size 0 / 0
Free PE / Size 511999 / 1.95 TiB
VG UUID RpMeCU-gIV2-kEe5-fe6C-8iVI-xe0i-vf4ocp
# lvcreate -L 1.95T -n mylv myvg
Rounding up size to full physical extent 1.95 TiB
Logical volume "mylv" created.
# lvdisplay
--- Logical volume ---
LV Path /dev/myvg/mylv
LV Name mylv
VG Name myvg
LV UUID e1nL7K-0qyc-BHt8-x9oO-1gGC-6qWn-a52o6x
LV Write Access read/write
LV Creation host, time iZ2ze47of4mpxkhhzcgjheZ, 2017-01-07 12:18:26 +0800
LV Status available
# open 0
LV Size 1.95 TiB
Current LE 511181
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0
# mke2fs -t ext4 -b 2048 /dev/myvg/mylv
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=2048 (log=1)
Fragment size=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
130863104 inodes, 1046898688 blocks
52344934 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1315438592
63898 block groups
16384 blocks per group, 16384 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104,
2048000, 3981312, 5619712, 10240000, 11943936, 35831808, 39337984,
51200000, 107495424, 256000000, 275365888, 322486272, 967458816
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
# mkdir /data
# mount /dev/myvg/mylv /data
# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/vda1 ext4 40G 1.3G 37G 4% /
devtmpfs devtmpfs 32G 0 32G 0% /dev
tmpfs tmpfs 32G 0 32G 0% /dev/shm
tmpfs tmpfs 32G 356K 32G 1% /run
tmpfs tmpfs 32G 0 32G 0% /sys/fs/cgroup
tmpfs tmpfs 6.3G 0 6.3G 0% /run/user/0
/dev/mapper/myvg-mylv ext4 2.0T 24M 1.9T 1% /data
开机自启动挂载分区
vim /etc/fstab
加入以下内容:
/dev/myvg/mylv /data ext4 defaults,noatime 0 0
# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Tue May 3 13:48:10 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=80b9b662-0a1d-4e84-b07b-c1bf19e72d97 / ext4 defaults 1 1
/dev/myvg/mylv /data ext4 defaults,noatime 0 0
逻辑卷扩容的示例
# vgdisplay myvg
--- Volume group ---
VG Name myvg
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 1
Act PV 1
VG Size 120.00 GiB
PE Size 4.00 MiB
Total PE 30719
Alloc PE / Size 28160 / 110.00 GiB
Free PE / Size 2559 / 10.00 GiB
VG UUID v5LSS8-E5HW-htEL-ioet-Up3T-zUFz-vtCzlA
[root@mini2 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 30G 1.8G 29G 6% /
devtmpfs devtmpfs 907M 0 907M 0% /dev
tmpfs tmpfs 917M 0 917M 0% /dev/shm
tmpfs tmpfs 917M 8.6M 908M 1% /run
tmpfs tmpfs 917M 0 917M 0% /sys/fs/cgroup
/dev/mapper/centos-data xfs 40G 33M 40G 1% /data
/dev/mapper/myvg-mylv ext4 109G 19M 103G 1% /aliyun
/dev/sda1 xfs 297M 115M 183M 39% /boot
tmpfs tmpfs 184M 0 184M 0% /run/user/0
1.新增一块60G的硬盘,分区
# fdisk /dev/sdc
Welcome to fdisk (util-linux 2.23.2).
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
Building a new DOS disklabel with disk identifier 0x58d7841e.
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-104857599, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-104857599, default 104857599):
Using default value 104857599
Partition 1 of type Linux and of size 50 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
2.将新增加的分区/dev/sdc1扩容到物理卷myvg中
# vgextend myvg /dev/sdc1
Physical volume "/dev/sdc1" successfully created
Volume group "myvg" successfully extended
# 可以看到由10G扩展到了59.99G
[root@mini2 ~]# vgdisplay myvg
--- Volume group ---
VG Name myvg
System ID
Format lvm2
Metadata Areas 2
Metadata Sequence No 3
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 1
Open LV 1
Max PV 0
Cur PV 2
Act PV 2
VG Size 169.99 GiB
PE Size 4.00 MiB
Total PE 43518
Alloc PE / Size 28160 / 110.00 GiB
Free PE / Size 15358 / 59.99 GiB
VG UUID v5LSS8-E5HW-htEL-ioet-Up3T-zUFz-vtCzlA
# lvdisplay /dev/myvg/mylv
--- Logical volume ---
LV Path /dev/myvg/mylv
LV Name mylv
VG Name myvg
LV UUID 3H0Qgm-tDJr-BJ96-Y0Vh-TMOE-1EZv-DN3qUE
LV Write Access read/write
LV Creation host, time mini2, 2017-01-07 13:55:37 +0800
LV Status available
# open 1
LV Size 110.00 GiB
Current LE 28160
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
3.扩展物理边界为150G
[root@mini2 ~]# lvextend -L 150G /dev/myvg/mylv
Size of logical volume myvg/mylv changed from 110.00 GiB (28160 extents) to 150.00 GiB (38400 extents).
Logical volume mylv successfully resized.
可以看到已经有110变成了150G
[root@mini2 ~]# lvdisplay /dev/myvg/mylv
--- Logical volume ---
LV Path /dev/myvg/mylv
LV Name mylv
VG Name myvg
LV UUID 3H0Qgm-tDJr-BJ96-Y0Vh-TMOE-1EZv-DN3qUE
LV Write Access read/write
LV Creation host, time mini2, 2017-01-07 13:55:37 +0800
LV Status available
# open 1
LV Size 150.00 GiB
Current LE 38400
Segments 2
Allocation inherit
Read ahead sectors auto
- currently set to 8192
Block device 253:2
[root@mini2 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 30G 1.8G 29G 6% /
devtmpfs devtmpfs 907M 0 907M 0% /dev
tmpfs tmpfs 917M 0 917M 0% /dev/shm
tmpfs tmpfs 917M 8.6M 908M 1% /run
tmpfs tmpfs 917M 0 917M 0% /sys/fs/cgroup
/dev/mapper/centos-data xfs 40G 33M 40G 1% /data
/dev/mapper/myvg-mylv ext4 109G 19M 103G 1% /aliyun
/dev/sda1 xfs 297M 115M 183M 39% /boot
tmpfs tmpfs 184M 0 184M 0% /run/user/0
4.扩展逻辑边界,可以看到磁盘分区/aliyun由109G变成了148G
[root@mini2 ~]# resize2fs /dev/myvg/mylv
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/myvg/mylv is mounted on /aliyun; on-line resizing required
old_desc_blocks = 110, new_desc_blocks = 150
The filesystem on /dev/myvg/mylv is now 78643200 blocks long.
[root@mini2 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 30G 1.8G 29G 6% /
devtmpfs devtmpfs 907M 0 907M 0% /dev
tmpfs tmpfs 917M 0 917M 0% /dev/shm
tmpfs tmpfs 917M 8.6M 908M 1% /run
tmpfs tmpfs 917M 0 917M 0% /sys/fs/cgroup
/dev/mapper/centos-data xfs 40G 33M 40G 1% /data
/dev/mapper/myvg-mylv ext4 148G 17M 141G 1% /aliyun
/dev/sda1 xfs 297M 115M 183M 39% /boot
tmpfs tmpfs 184M 0 184M 0% /run/user/0
UCLOUD对数据盘扩容后需要控制台重启,并进入系统后进行如下操作:
//针对ext4文件格式的操作系统(如CentOS6):
umount /dev/vdb e2fsck -f /dev/vdb resize2fs /dev/vdb mount /dev/vdb /data
ucloud升级磁盘空间后需要做的操作:
具体:
[root@u04rdp01 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/vda1 ext4 40G 6.2G 32G 17% /
tmpfs tmpfs 7.8G 12K 7.8G 1% /dev/shm
/dev/vdb ext4 493G 492G 0 100% /data
[root@u04rdp01 ~]# umount /dev/vdb
[root@u04rdp01 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/vda1 ext4 40G 6.2G 32G 17% /
tmpfs tmpfs 7.8G 12K 7.8G 1% /dev/shm
[root@u04rdp01 ~]# e2fsck -f /dev/vdb
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vdb: 32929/32768000 files (0.5% non-contiguous), 131018675/131072000 blocks
[root@u04rdp01 ~]# resize2fs /dev/vdb
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vdb to 262144000 (4k) blocks.
The filesystem on /dev/vdb is now 262144000 blocks long.
[root@u04rdp01 ~]# mount /dev/vdb /data
[root@u04rdp01 ~]# df -Th
Filesystem Type Size Used Avail Use% Mounted on
/dev/vda1 ext4 40G 6.2G 32G 17% /
tmpfs tmpfs 7.8G 12K 7.8G 1% /dev/shm
/dev/vdb ext4 985G 492G 443G 53% /data
//针对xfs文件格式的操作系统(如CentOS7)//
umount /dev/vdb
xfs_repair /dev/vdb
mount /dev/vdb /data
xfs_growfs /data
本文介绍如何通过普通磁盘分区管理和逻辑卷管理(LVM)两种方式来管理磁盘空间。包括磁盘分区创建、格式化、挂载及逻辑卷的创建、扩展等关键步骤。

1421

被折叠的 条评论
为什么被折叠?



