1. 程式人生 > >Linux:磁盤掛載

Linux:磁盤掛載

swa ext esxi alloc chang 設置 參考 ack eal

本來虛擬centos的服務器的磁盤分配的就不大,之前只分配了20G的樣子,由於最近有裝了不少軟件,比如nifi壓縮版就有1.2G的大小,一下子沒有磁盤資源了。今晚就折騰在這事上了。

[root@master 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)查看一下調整後的磁盤狀態

[root@master 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)磁盤分區

[root@master]# fdisk /dev/sda

分別鍵入以下參數:

p       查看已分區數量(我看到有兩個 /dev/sda1 和/dev/sda2)
n       新增加一個分區
p       分區類型,選擇主分區
        分區號選3(1和2已占用,見上)
回車     默認選擇(起始扇區)
回車     默認選擇(結束扇區)
t       修改分區類型
       選分區3
8e     修改為LVM(8e就是LVM)
w       寫分區表,完成後退出fdisk命令

實際操作:

[root@master 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.

#重新查看重新分區後的分區情況: [root@master 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)格式化分區

註意:格式化之前要重啟機器

[spark@master ~]$ mkfs.ext3 /dev/sda3
mke2fs 1.42.9 (28-Dec-2013)
mkfs.ext3: Permission denied while trying to determine filesystem size
[spark@master ~]$ su root
Password: 
[root@master 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 

[root@master spark]#

4. 添加新LVM到已有的LVM組,實現擴容

進入lvm管理

[root@master]# 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 

以下是具體實操操作記錄:

[root@master spark]# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created.
[root@master spark]# 
[root@master 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. 

[root@master 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

#磁盤資源不足清理磁盤
[root@master spark]# cd /opt/
[root@master 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          
[root@master 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
[root@master 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
[root@master 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

#磁盤清理後查看磁盤資源
[root@master 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
#重啟服務器
[root@master opt]# reboot

#重新登錄
Last login: Tue Aug  7 23:16:33 2018 from 192.168.0.103
[spark@master ~]$ su root
Password: 
[root@master 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.
[root@master 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
   
[root@master spark]# vgextend centos /dev/sda3
  Volume group "centos" successfully extended
[root@master 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
[root@master 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

以下是具體實操操作記錄:

[root@master 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
[root@master 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
[root@master 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:磁盤掛載