Linux:磁盘挂载

Posted yy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux:磁盘挂载相关的知识,希望对你有一定的参考价值。

本来虚拟centos的服务器的磁盘分配的就不大,之前只分配了20G的样子,由于最近有装了不少软件,比如nifi压缩版就有1.2G的大小,一下子没有磁盘资源了。今晚就折腾在这事上了。

[[email protected] spark]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G   17G   20K 100% /
devtmpfs                 2.4G     0  2.4G   0% /dev
tmpfs                    2.4G  4.0K  2.4G   1% /dev/shm
tmpfs                    2.4G   12M  2.4G   1% /run
tmpfs                    2.4G     0  2.4G   0% /sys/fs/cgroup
/dev/sda1               1014M  142M  873M  14% /boot
tmpfs                    479M     0  479M   0% /run/user/1000

1. 查看挂载点信息

df -h 

显示结果:根目录可用磁盘只有20K。

2. 扩展VMWare硬盘空间

首先需要关闭操作系统,然后在设置中将磁盘调整到需要的容量。
如果选项是灰色的,说明虚拟机有快照,将其快照删除再操作。

备注:这里通过vmw,扩展master节点10G磁盘资源。

3. 对新增加的硬盘进行分区、格式化

(1)查看一下调整后的磁盘状态

[[email protected] spark]# fdisk -l

Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000aad3c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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/sda1,大小为10G。

(2)磁盘分区

[[email protected]]# fdisk /dev/sda

分别键入以下参数:

p       查看已分区数量(我看到有两个 /dev/sda1 和/dev/sda2)
n       新增加一个分区
p       分区类型,选择主分区
        分区号选3(1和2已占用,见上)
回车     默认选择(起始扇区)
回车     默认选择(结束扇区)
t       修改分区类型
       选分区3
8e     修改为LVM(8e就是LVM)
w       写分区表,完成后退出fdisk命令

实际操作:

[[email protected] spark]# fdisk /dev/sda
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.


Command (m for help): p

Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000aad3c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): p
Partition number (3,4, default 3): 3
First sector (41943040-62914559, default 41943040): #直接回车(不输入任何内容)
Using default value 41943040
Last sector, +sectors or +size{K,M,G} (41943040-62914559, default 62914559): #直接回车(不输入任何内容)
Using default value 62914559
Partition 3 of type Linux and of size 10 GiB is set

Command (m for help): t
Partition number (1-3, default 3): 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!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

#重新查看重新分区后的分区情况: [[email protected] spark]# fdisk
/dev/sda 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. Command (m for help): p Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000aad3c Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 41943039 19921920 8e Linux LVM /dev/sda3 41943040 62914559 10485760 8e Linux LVM Command (m for help):

(3)格式化分区

注意:格式化之前要重启机器

[[email protected] ~]$ mkfs.ext3 /dev/sda3
mke2fs 1.42.9 (28-Dec-2013)
mkfs.ext3: Permission denied while trying to determine filesystem size
[[email protected] ~]$ su root
Password: 
[[email protected] spark]# mkfs.ext3 /dev/sda3
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
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2684354560
80 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

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 

[[email protected] spark]#

4. 添加新LVM到已有的LVM组,实现扩容

进入lvm管理

实际上以下命令我在centos7下直接操作也行。

关于lvm的一些基础知识可以参考:《Linx 卷管理详解--VG LV PV

(1)初始化刚才的分区

pvcreate /dev/sda3

(2)查看卷和卷组

vgdisplay

要记下”VG Name”,这里为centos;再记下”Free PE/Size”的大小,例如这里为21513

(3)将初始化过的分区加入到虚拟卷组

vgextend centos /dev/sda3

刚才记下的”VG Name”就在这里使用。

但是执行到这一步时报错了:

Couldn‘t create temporary archive name.
Volume group "centos" metadata archive failed. Internal error:
Attempt to unlock unlocked VG #orphans.

查看了下,因为磁盘实在太小了,无法执行,删除一些数据就行。

(4)扩展已有卷的容量

lvextend -l +9.5G /dev/mapper/centos-root

lvextend指令用于在线扩展逻辑卷的空间大小,而不中断应用程序对逻辑卷的访问。其后有两个选项

选项说明
-L 指定逻辑卷的大小,单位为“kKmMgGtT”字节,也就是Size
-l 指定逻辑卷的大小,单位为PE数

其余两个参数:+9.5G是磁盘扩增的大小(也可以是通过vgdisplay查看的free的大小【尽量不完全使用,小个几十到几百M】),后边的目录参数/dev/mapper/centos-root可以通过df命令查看

(5)查看卷容量

pvdisplay

(6)退出

quit 

以下是具体实操操作记录:

[[email protected] spark]# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created.
[[email protected] spark]# 
[[email protected] spark]# vgextend centos /dev/sda3
Couldn‘t create temporary archive name.
Volume group "centos" metadata archive failed. Internal error:
Attempt to unlock unlocked VG #orphans. 

[[email protected] spark]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G   17G   20K 100% /
devtmpfs                 2.4G     0  2.4G   0% /dev
tmpfs                    2.4G  4.0K  2.4G   1% /dev/shm
tmpfs                    2.4G   12M  2.4G   1% /run
tmpfs                    2.4G     0  2.4G   0% /sys/fs/cgroup
/dev/sda1               1014M  142M  873M  14% /boot
tmpfs                    479M     0  479M   0% /run/user/1000

#磁盘资源不足清理磁盘
[[email protected] spark]# cd /opt/
[[email protected] opt]# ls
apache-maven-3.5.4                   elasticsearch-head-master      hdfs-over-ftp-master.zip    kibana-6.2.2-linux-x86_64.tar.gz  node-v8.9.1.tar.gz             zookeeper-3.4.12
apache-maven-3.5.4-bin.tar.gz        elasticsearch-head-master.zip  jdk1.8.0_171                nifi-1.7.1                        scala-2.11.0                   zookeeper-3.4.12.tar.gz
elasticsearch-6.2.2                  hadoop-2.9.0                   jdk-8u171-linux-x64.tar.gz  nifi-1.7.1-bin.zip                scala-2.11.0.tgz
elasticsearch-6.2.2.zip              hadoop-2.9.0.tar.gz            kafka_2.11-1.1.0            node-8.9.1                        spark-2.2.1-bin-hadoop2.7
elasticsearch-analysis-ik-6.2.2.zip  hdfs-over-ftp-master           kibana-6.2.2-linux-x86_64   node-v8.9.1                       spark-2.2.1-bin-hadoop2.7.tgz          
[[email protected] opt]# rm *.gz
rm: remove regular file ‘apache-maven-3.5.4-bin.tar.gz’? y
rm: remove regular file ‘hadoop-2.9.0.tar.gz’? y
rm: remove regular file ‘jdk-8u171-linux-x64.tar.gz’? y
rm: remove regular file ‘kibana-6.2.2-linux-x86_64.tar.gz’? y
rm: remove regular file ‘node-v8.9.1.tar.gz’? y
rm: remove regular file ‘zookeeper-3.4.12.tar.gz’? y
[[email protected] opt]# rm *.zip
rm: remove regular file ‘elasticsearch-6.2.2.zip’? y
rm: remove regular file ‘elasticsearch-analysis-ik-6.2.2.zip’? y
rm: remove regular file ‘elasticsearch-head-master.zip’? y
rm: remove regular file ‘hdfs-over-ftp-master.zip’? y
rm: remove regular file ‘nifi-1.7.1-bin.zip’? y
[[email protected] opt]# rm *.tgz
rm: remove regular file ‘scala-2.11.0.tgz’? y
rm: remove regular file ‘spark-2.2.1-bin-hadoop2.7.tgz’? y

#磁盘清理后查看磁盘资源
[[email protected] opt]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G   15G  2.1G  88% /
devtmpfs                 2.4G     0  2.4G   0% /dev
tmpfs                    2.4G  4.0K  2.4G   1% /dev/shm
tmpfs                    2.4G   12M  2.4G   1% /run
tmpfs                    2.4G     0  2.4G   0% /sys/fs/cgroup
/dev/sda1               1014M  142M  873M  14% /boot
tmpfs                    479M     0  479M   0% /run/user/1000
#重启服务器
[[email protected] opt]# reboot

#重新登录
Last login: Tue Aug  7 23:16:33 2018 from 192.168.0.103
[[email protected] ~]$ su root
Password: 
[[email protected] spark]# pvcreate /dev/sda3
WARNING: ext3 signature detected on /dev/sda3 at offset 1080. Wipe it? [y/n]: y
  Wiping ext3 signature on /dev/sda3.
  Physical volume "/dev/sda3" successfully created.
[[email protected] spark]# vgdisplay
  --- Volume group ---
  VG Name               centos
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               <19.00 GiB
  PE Size               4.00 MiB
  Total PE              4863
  Alloc PE / Size       4863 / <19.00 GiB
  Free  PE / Size       0 / 0   
  VG UUID               cKJ05H-yI4f-5qfY-5SO5-ybTW-2zDx-M5uyg0
   
[[email protected] spark]# vgextend centos /dev/sda3
  Volume group "centos" successfully extended
[[email protected] spark]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G   15G  2.1G  88% /
devtmpfs                 2.4G     0  2.4G   0% /dev
tmpfs                    2.4G     0  2.4G   0% /dev/shm
tmpfs                    2.4G   12M  2.4G   1% /run
tmpfs                    2.4G     0  2.4G   0% /sys/fs/cgroup
/dev/sda1               1014M  142M  873M  14% /boot
tmpfs                    479M     0  479M   0% /run/user/1000
[[email protected] spark]# fdisk -l

Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 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: 0x000aad3c

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
/dev/sda3        41943040    62914559    10485760   8e  Linux LVM

Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 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 /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 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

5. 文件系统扩容

以上只是卷扩容了,还要对文件系统实现真正扩容
CentOS 7 下面 由于使用的是 XFS,所以要用

xfs_growfs /dev/mapper/centos-root

CentOS 6 下面 要用

resize2fs /dev/mapper/centos-root

以下是具体实操操作记录:

[[email protected] spark]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G   15G  2.1G  88% /
devtmpfs                 2.4G     0  2.4G   0% /dev
tmpfs                    2.4G     0  2.4G   0% /dev/shm
tmpfs                    2.4G   12M  2.4G   1% /run
tmpfs                    2.4G     0  2.4G   0% /sys/fs/cgroup
/dev/sda1               1014M  142M  873M  14% /boot
tmpfs                    479M     0  479M   0% /run/user/1000
[[email protected] spark]# xfs_growfs /dev/mapper/centos-root
meta-data=/dev/mapper/centos-root isize=512    agcount=4, agsize=1113856 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=1        finobt=0 spinodes=0
data     =                       bsize=4096   blocks=4455424, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=1
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 4455424 to 6945792
[[email protected] spark]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   27G   15G   12G  57% /
devtmpfs                 2.4G     0  2.4G   0% /dev
tmpfs                    2.4G     0  2.4G   0% /dev/shm
tmpfs                    2.4G   12M  2.4G   1% /run
tmpfs                    2.4G     0  2.4G   0% /sys/fs/cgroup
/dev/sda1               1014M  142M  873M  14% /boot
tmpfs                    479M     0  479M   0% /run/user/1000

6. 查看新的磁盘空间

df -h

 

 

参考《Esxi中CentOS7 扩展磁盘容量

以上是关于Linux:磁盘挂载的主要内容,如果未能解决你的问题,请参考以下文章

Shell ❀ Linux系统磁盘快速挂载执行脚本

Linux磁盘挂载和扩容(1)

如何查看Linux挂载磁盘

linux磁盘挂载

linux挂载磁盘怎么开机

linux下挂载磁盘的命令是啥?