相关概念
文件系统、挂载点、分区表、PV(physical volume)、VG(volume group)、LV(logical volume)
相关命令
fdisk、df、du、pvcreate、pvdisplay、vgdisplay、vgextend、lvdisplay、lvextend 、growfs
LVM(逻辑分区管理)中的几个概念
PV(physical volume):物理卷在逻辑卷管理系统最底层,可为整个物理硬盘或实际物理硬盘上的分区。
VG(volume group):卷组建立在物理卷上,一卷组中至少要包括一物理卷,卷组建立后可动态的添加卷到卷组中,一个逻辑卷管理系统工程中可有多个卷组。
LV(logical volume):逻辑卷建立在卷组基础上,卷组中未分配空间可用于建立新的逻辑卷,逻辑卷建立后可以动态扩展和缩小空间。
PE(physical extent):物理区域是物理卷中可用于分配的最小存储单元,物理区域大小在建立卷组时指定,一旦确定不能更改,同一卷组所有物理卷的物理区域大小需一致,新的pv加入到vg后,pe的大小自动更改为vg中定义的pe大小。
LE(logical extent):逻辑区域是逻辑卷中可用于分配的最小存储单元,逻辑区域的大小取决于逻辑卷所在卷组中的物理区域的大小。
卷组描述区域:卷组描述区域存在于每个物理卷中,用于描述物理卷本身、物理卷所属卷组、卷组中逻辑卷、逻辑卷中物理区域的分配等所有信息,它是在使用pvcreate建立物理卷时建立的。
![cb9fdc151112af52d1a1529fde0dfc24.png](https://i-blog.csdnimg.cn/blog_migrate/cbe788255ace1d0ef54331b486a01289.jpeg)
磁盘扩容
#1.对磁盘扩容,如下图,以VMWare为例子
![f586f68ff4a2294972efaefd88e1b5dc.png](https://i-blog.csdnimg.cn/blog_migrate/3f3d9f710550120bab9eb31ffef8cd89.jpeg)
#2.操作分区表
[root@slave2 ~]# fdisk /dev/sda欢迎使用 fdisk (util-linux 2.23.2)。更改将停留在内存中,直到您决定将更改写入磁盘。使用写入命令前请三思。命令(输入 m 获取帮助):m命令操作a toggle a bootable flagb edit bsd disklabelc toggle the dos compatibility flagd delete a partitiong create a new empty GPT partition tableG create an IRIX (SGI) partition tablel list known partition typesm print this menun add a new partitiono create a new empty DOS partition tablep print the partition tableq quit without saving changess create a new empty Sun disklabelt change a partitions system idu change display/entry unitsv verify the partition tablew write table to disk and exitx extra functionality (experts only)命令(输入 m 获取帮助):nPartition type:p primary (3 primary, 0 extended, 1 free)e extendedSelect (default e): p已选择分区 4起始 扇区 (52428800-62914559,默认为 52428800):将使用默认值 52428800Last 扇区, +扇区 or +size{K,M,G} (52428800-62914559,默认为 62914559):将使用默认值 62914559分区 4 已设置为 Linux 类型,大小设为 5 GiB命令(输入 m 获取帮助):t分区号 (1-4,默认 4):Hex 代码(输入 L 列出所有代码):8e已将分区"Linux"的类型更改为"Linux LVM"命令(输入 m 获取帮助):p磁盘 /dev/sda:32.2 GB, 32212254720 字节,62914560 个扇区Units = 扇区 of 1 * 512 = 512 bytes扇区大小(逻辑/物理):512 字节 / 512 字节I/O 大小(最小/最佳):512 字节 / 512 字节磁盘标签类型:dos磁盘标识符:0x000be0b1设备 Boot Start End Blocks Id System/dev/sda1 * 2048 2099199 1048576 83 Linux/dev/sda2 2099200 41943039 19921920 8e Linux LVM/dev/sda3 41943040 52428799 5242880 8e Linux LVM/dev/sda4 52428800 62914559 5242880 8e Linux LVM命令(输入 m 获取帮助):wThe partition table has been altered!Calling ioctl() to re-read partition table.WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.The kernel still uses the old table. The new table will be used atthe next reboot or after you run partprobe(8) or kpartx(8)正在同步磁盘。[root@slave2 ~]#
#3.重启虚拟机 reboot
查看当前分区类型,本文是xfs
[root@slave2 ~]# df -T /dev/sda1文件系统 类型 1K-块 已用 可用 已用% 挂载点/dev/sda1 xfs 1038336 225916 812420 22% /boot
查看所有分区类型
[root@slave2 ~]# df -T文件系统 类型 1K-块 已用 可用 已用% 挂载点/dev/mapper/centos-root xfs 23050240 21529044 1521196 94% /devtmpfs devtmpfs 1914816 0 1914816 0% /devtmpfs tmpfs 1931768 44096 1887672 3% /dev/shmtmpfs tmpfs 1931768 12756 1919012 1% /runtmpfs tmpfs 1931768 0 1931768 0% /sys/fs/cgroup/dev/sda1 xfs 1038336 225916 812420 22% /boottmpfs tmpfs 386356 4 386352 1% /run/user/42tmpfs tmpfs 386356 20 386336 1% /run/user/0[root@slave2 ~]# mkfs.xfs /dev/sda3mkfs.xfs: cannot open /dev/sda3: 设备或资源忙
#5.在新磁盘上创建xfs文件系统
[root@slave2 ~]# mkfs.xfs /dev/sda4meta-data=/dev/sda4 isize=512 agcount=4, agsize=327680 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0, sparse=0data = bsize=4096 blocks=1310720, imaxpct=25= sunit=0 swidth=0 blksnaming =version 2 bsize=4096 ascii-ci=0 ftype=1log =internal log bsize=4096 blocks=2560, version=2= sectsz=512 sunit=0 blks, lazy-count=1realtime =none extsz=4096 blocks=0, rtextents=0
#6.创建PV
[root@slave2 ~]# pvcreate /dev/sda4WARNING: xfs signature detected on /dev/sda4 at offset 0. Wipe it? [y/n]: yWiping xfs signature on /dev/sda4.Physical volume "/dev/sda4" successfully created.
#查看PV详情
[root@slave2 ~]# pvdisplay--- Physical volume ---PV Name /dev/sda2VG Name centosPV Size <19.00 GiB / not usable 3.00 MiBAllocatable yes (but full)PE Size 4.00 MiBTotal PE 4863Free PE 0Allocated PE 4863PV UUID tmP2ve-P0UY-2i43-04Ks-ARRJ-TfYB-AXy02A--- Physical volume ---PV Name /dev/sda3VG Name centosPV Size 5.00 GiB / not usable 4.00 MiBAllocatable yes (but full)PE Size 4.00 MiBTotal PE 1279Free PE 0Allocated PE 1279PV UUID 1xmsfP-UeGt-tylL-EbEc-PQcO-vh7W-jzjNtS"/dev/sda4" is a new physical volume of "5.00 GiB"--- NEW Physical volume ---PV Name /dev/sda4VG NamePV Size 5.00 GiBAllocatable NOPE Size 0Total PE 0Free PE 0Allocated PE 0PV UUID TesT6d-qtMO-YNxS-wQlV-qwLV-y2sk-sMkz0s
#7.查看VG详情,并将PV加入VG
#查看VG详情[root@slave2 ~]# vgdisplay--- Volume group ---VG Name centosSystem IDFormat lvm2Metadata Areas 2Metadata Sequence No 5VG Access read/writeVG Status resizableMAX LV 0Cur LV 2Open LV 2Max PV 0Cur PV 2Act PV 2VG Size 23.99 GiBPE Size 4.00 MiBTotal PE 6142Alloc PE / Size 6142 / 23.99 GiBFree PE / Size 0 / 0VG UUID VnYp52-KG41-5vLr-Y6Sk-dsE8-ytp4-jy2Jc8##此处的centos为VG Name,/dev/sda4为新的PV Name[root@slave2 ~]# vgextend centos /dev/sda4Volume group "centos" successfully extended
#8.查看LV详情
[root@slave2 ~]# lvdisplay--- Logical volume ---LV Path /dev/centos/swapLV Name swapVG Name centosLV UUID d6pDY4-aJSd-DxbL-mKWb-FqHQ-s2BO-173tvGLV Write Access read/writeLV Creation host, time localhost, 2018-08-07 05:24:42 +0800LV Status available# open 2LV Size 2.00 GiBCurrent LE 512Segments 1Allocation inheritRead ahead sectors auto- currently set to 8192Block device 253:1--- Logical volume ---LV Path /dev/centos/rootLV Name rootVG Name centosLV UUID 2XfVeV-Oduz-1Eml-qC1Q-FRe6-RTtb-05QFOfLV Write Access read/writeLV Creation host, time localhost, 2018-08-07 05:24:42 +0800LV Status available# open 1LV Size 21.99 GiBCurrent LE 5630Segments 2Allocation inheritRead ahead sectors auto- currently set to 8192Block device 253:0
#9.扩展逻辑卷
[root@slave2 ~]# lvextend /dev/centos/root /dev/sda3WARNING: No free extents on physical volume "/dev/sda3".No specified PVs have space available.[root@slave2 ~]# lvextend /dev/centos/root /dev/sda4Size of logical volume centos/root changed from 21.99 GiB (5630 extents) to <26.99 GiB (6909 extents).Logical volume centos/root successfully resized.
#10.调整文件系统大小,
本文中是xfs文件系统,使用xfs_growfs命令调整
[root@slave2 ~]# xfs_growfs /dev/centos/rootmeta-data=/dev/mapper/centos-root isize=512 agcount=6, agsize=1113856 blks= sectsz=512 attr=2, projid32bit=1= crc=1 finobt=0 spinodes=0data = bsize=4096 blocks=5765120, imaxpct=25= sunit=0 swidth=0 blksnaming =version 2 bsize=4096 ascii-ci=0 ftype=1log =internal bsize=4096 blocks=2560, version=2= sectsz=512 sunit=0 blks, lazy-count=1realtime =none extsz=4096 blocks=0, rtextents=0data blocks changed from 5765120 to 7074816
#11.查看是否成功
#已经扩容成功,查看当前文件系统大小,可以看见已新增5GB空间
[root@slave2 ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 27G 21G 6.5G 77% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 44M 1.9G 3% /dev/shm
tmpfs 1.9G 13M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/sda1 1014M 221M 794M 22% /boot
tmpfs 378M 4.0K 378M 1% /run/user/42
tmpfs 378M 20K 378M 1% /run/user/0
[root@slave2 ~]#
- fdisk用法
[root@master ~]# fdisk --helpfdisk:无效选项 -- -用法: fdisk [选项] 更改分区表 fdisk [选项] -l 列出分区表 fdisk -s 给出分区大小(块数)选项: -b 扇区大小(512、1024、2048或4096) -c[=] 兼容模式:“dos”或“nondos”(默认) -h 打印此帮助文本 -u[=] 显示单位:“cylinders”(柱面)或“sectors”(扇区,默认) -v 打印程序版本 -C 指定柱面数 -H 指定磁头数 -S 指定每个磁道的扇区数[root@master ~]#
例子1
fdisk -l 查看硬盘及分区信息
[root@wanwang1078 myhdd]# fdisk -lDisk /dev/sdb: 120.3 GB, 120259084288 bytes, 234881024 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 4096 bytesI/O size (minimum/optimal): 4096 bytes / 4096 bytesDisk label type: dosDisk identifier: 0xd68cfa6fDevice Boot Start End Blocks Id System/dev/sdb1 128 234878975 117439424 83 LinuxDisk /dev/sdc: 214.7 GB, 214748364800 bytes, 419430400 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 4096 bytesI/O size (minimum/optimal): 4096 bytes / 4096 bytesDisk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectorsUnits = sectors of 1 * 512 = 512 bytesSector size (logical/physical): 512 bytes / 4096 bytesI/O size (minimum/optimal): 4096 bytes / 4096 bytesDisk label type: dosDisk identifier: 0x000b9d1dDevice Boot Start End Blocks Id System/dev/sda1 * 2048 1026047 512000 83 Linux/dev/sda2 1026048 62914559 30944256 83 Linux[root@wanwang1078 myhdd]#Disk /dev/sdb: 120.3 GB, 120259084288 bytes, 234881024 sectorsDisk /dev/sdc: 214.7 GB, 214748364800 bytes, 419430400 sectorsDisk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors
例子2
[root@master ~]# fdisk -lDisk /dev/sdb: 429.5 GB, 429496729600 bytes255 heads, 63 sectors/track, 52216 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 4096 bytesI/O size (minimum/optimal): 4096 bytes / 4096 bytesDisk identifier: 0x5a12f31aDevice Boot Start End Blocks Id System/dev/sdb1 1 52217 419428352 83 LinuxDisk /dev/sda: 32.2 GB, 32212254720 bytes255 heads, 63 sectors/track, 3916 cylindersUnits = cylinders of 16065 * 512 = 8225280 bytesSector size (logical/physical): 512 bytes / 512 bytesI/O size (minimum/optimal): 512 bytes / 512 bytesDisk identifier: 0x000598c7Device Boot Start End Blocks Id System/dev/sda1 * 1 3917 31456256 83 Linux[root@master ~]#
- df用法
[root@master ~]# df --help用法:df [选项]... [文件]...Show information about the file system on which each FILE resides,or all file systems by default.Mandatory arguments to long options are mandatory for short options too. -a, --all include dummy file systems -B, --block-size=SIZE scale sizes by SIZE before printing them; e.g., '-BM' prints sizes in units of 1,048,576 bytes; see SIZE format below --direct show statistics for a file instead of mount point --total produce a grand total -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G) -H, --si likewise, but use powers of 1000 not 1024 -i, --inodes显示inode 信息而非块使用量 -k即--block-size=1K -l, --local只显示本机的文件系统 --no-sync取得使用量数据前不进行同步动作(默认) --output[=FIELD_LIST] use the output format defined by FIELD_LIST, or print all fields if FIELD_LIST is omitted. -P, --portability use the POSIX output format --sync invoke sync before getting usage info -t, --type=TYPE limit listing to file systems of type TYPE -T, --print-type print file system type -x, --exclude-type=TYPE limit listing to file systems not of type TYPE -v (ignored) --help显示此帮助信息并退出 --version显示版本信息并退出所显示的数值是来自 --block-size、DF_BLOCK_SIZE、BLOCK_SIZE 及 BLOCKSIZE 环境变量中第一个可用的 SIZE 单位。否则,默认单位是 1024 字节(或是 512,若设定 POSIXLY_CORRECT 的话)。SIZE is an integer and optional unit (example: 10M is 10*1024*1024). Unitsare K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).FIELD_LIST is a comma-separated list of columns to be included. Validfield names are: 'source', 'fstype', 'itotal', 'iused', 'iavail', 'ipcent','size', 'used', 'avail', 'pcent', 'file' and 'target' (see info page).GNU coreutils online help: 请向 报告df 的翻译错误要获取完整文档,请运行:info coreutils 'df invocation'#查看磁盘使用情况[root@master ~]# df -lhFilesystem Size Used Avail Use% Mounted on/dev/sda1 30G 17G 12G 58% /tmpfs 14G 292K 14G 1% /dev/shm/dev/sdb1 394G 6.2G 368G 2% /mnt/resource[root@master ~]#
- du用法
[root@master ~]# du --help用法:du [选项]... [文件]...或:du [选项]... --files0-from=FSummarize disk usage of each FILE, recursively for directories.Mandatory arguments to long options are mandatory for short options too. -0, --null end each output line with 0 byte rather than newline -a, --all write counts for all files, not just directories --apparent-size print apparent sizes, rather than disk usage; although the apparent size is usually smaller, it may be larger due to holes in ('sparse') files, internal fragmentation, indirect blocks, and the like -B, --block-size=SIZE scale sizes by SIZE before printing them; e.g., '-BM' prints sizes in units of 1,048,576 bytes; see SIZE format below -b, --bytes equivalent to '--apparent-size --block-size=1' -c, --total produce a grand total -D, --dereference-args dereference only symlinks that are listed on the command line -d, --max-depth=N print the total for a directory (or file, with --all) only if it is N or fewer levels below the command line argument; --max-depth=0 is the same as --summarize --files0-from=F summarize disk usage of the NUL-terminated file names specified in file F; if F is -, then read names from standard input -H equivalent to --dereference-args (-D) -h, --human-readable print sizes in human readable format (e.g., 1K 234M 2G) --inodes list inode usage information instead of block usage -k like --block-size=1K -L, --dereference dereference all symbolic links -l, --count-links count sizes many times if hard linked -m like --block-size=1M -P, --no-dereference don't follow any symbolic links (this is the default) -S, --separate-dirs for directories do not include size of subdirectories --si like -h, but use powers of 1000 not 1024 -s, --summarize display only a total for each argument -t, --threshold=SIZE exclude entries smaller than SIZE if positive, or entries greater than SIZE if negative --time show time of the last modification of any file in the directory, or any of its subdirectories --time=WORD show time as WORD instead of modification time: atime, access, use, ctime or status --time-style=STYLE show times using STYLE, which can be: full-iso, long-iso, iso, or +FORMAT; FORMAT is interpreted like in 'date' -X, --exclude-from=FILE exclude files that match any pattern in FILE --exclude=PATTERN exclude files that match PATTERN -x, --one-file-system skip directories on different file systems --help显示此帮助信息并退出 --version显示版本信息并退出所显示的数值是来自 --block-size、DU_BLOCK_SIZE、BLOCK_SIZE 及 BLOCKSIZE 环境变量中第一个可用的 SIZE 单位。否则,默认单位是 1024 字节(或是 512,若设定 POSIXLY_CORRECT 的话)。SIZE is an integer and optional unit (example: 10M is 10*1024*1024). Unitsare K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).GNU coreutils online help: 请向 报告du 的翻译错误要获取完整文档,请运行:info coreutils 'du invocation'[root@master ~]#
递归地总结每个文件的磁盘使用情况。
[root@master ~]# cd /home[root@master home]# du -h --max-depth=1439M ./tomcat28M ./zxp3.9G ./zookeeper3.3G ./bigdata1.6G ./jstorm20K ./wanwangcal19.3G .[root@master home]#