磁盘管理

1.分区工具

fdisk MBR分区表,只有4个主分区,适用于2TB下的分区

gdisk GPT分区表,128个分区,

parted 高级分区工具

```

```shell

[root@oldboyedu ~]# fdisk /dev/sdb

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 0xf35f2768.

Command (m for help): m

Command action

a toggle a bootable flag

b edit bsd disklabel

c toggle the dos compatibility flag

d delete a partition #删除分区

g create a new empty GPT partition table

G create an IRIX (SGI) partition table

l list known partition types #显示分区列表信息

m print this menu #菜单

n add a new partition #创建新的分区

o create a new empty DOS partition table

p print the partition table #打印分区表信息

q quit without saving changes #退出

s create a new empty Sun disklabel

t change a partition's system id

u change display/entry units

v verify the partition table

w write table to disk and exit #保存并退出

x extra functionality (experts only)

2. Fdisk

```shell

#1. 添加硬盘

#2. 创建分区

fdisk /dev/sdb

#3.格式化,做文件系统

mkfs.xfs /dev/sdb1

#4.创建一个挂载点

mkdir /data

#5.进行挂载

mount /dev/sdb1 /data

#6.永久挂载,开机自启动挂载

/etc/fstab

/dev/sdb1 /data xfs defaults 0 0

mount -a

reboot

```

3. Gdisk

```shell

#1.添加硬盘3TB

#2.gdisk分区,创建分区

下载安装 yum install -y gdisk

Command (? for help): m

b back up GPT data to a file

c change a partition's name

d delete a partition #删除分区

i show detailed information on a partition

l list known partition types #显示分区列表

n add a new partition #创建分区

o create a new empty GUID partition table (GPT)

p print the partition table #打印分区表信息

q quit without saving changes #退出

r recovery and transformation options (experts only)

s sort partitions

t change a partition's type code

v verify disk

w write table to disk and exit #保存退出

x extra functionality (experts only)

? print this menu #菜单

#3.格式化,创建文件系统

[root@oldboyedu ~]# mkfs.xfs /dev/sdc1

#4.创建挂载点

[root@oldboyedu ~]# mkdir /gpt

#5.挂载

[root@oldboyedu ~]# mount /dev/sdc1 /gpt

#6.永久挂载

[root@oldboyedu ~]# tail -1 /etc/fstab

/dev/sdc1 /gpt xfs defaults 0 0

4. 磁盘的挂载方式

mount 挂载磁盘

选项:

-t #指定文件系统

-a #重新加载/etc/fstab文件挂载列表

-o #指定挂载的参数

umount 卸载

选项:

-l #强制卸载

或者退出挂载目录进行卸载。

永久挂载:/etc/fstab 推荐使用UUID进行挂载

#查看UUID的信息

[root@oldboyedu ~]# blkid

/dev/sr0: UUID="2018-11-25-23-54-16-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos"

/dev/sdb1: UUID="86f77539-a04d-4448-aa45-e77644b67f71" TYPE="xfs"

/dev/sdc1: UUID="a0c64606-5655-4090-b6cd-464b201f3837" TYPE="xfs" PARTLABEL="Linux filesystem" PARTUUID="ba2761a8-1031-4bff-ad1a-cfa6c37cffab"

/dev/sda1: UUID="e7d2d29d-d679-4f0f-8acb-e86ffbe9b5f9" TYPE="xfs"

/dev/sda2: UUID="7b4436bc-2748-49ba-9ff8-a5923eaf2127" TYPE="swap"

/dev/sda3: UUID="8a332f48-6869-4866-9e8d-698aaaf744df" TYPE="xfs

#临时挂载

[root@oldboyedu ~]# mount UUID="a0c64606-5655-4090-b6cd-464b201f3837" /gpt

#永久挂载

[root@oldboyedu ~]# tail -1 /etc/fstab

UUID="a0c64606-5655-4090-b6cd-464b201f3837" /gpt xfs defaults 0 0

#/etc/fstab文件详解

UUID="a0c64606-5655-4090-b6cd-464b201f3837" /gpt xfs defaults 0 0

第一列:设备名,UUID,网络地址

第二列:挂载点

第三列:文件系统的类型 ext3 ext4 xfs swap

第四列:默认挂载的参数,defaults,

async/sync 是否同步,或者异步,默认的是async

user/nouser 是否让普通用户使用挂载命令,nouser

suid/nosuid suid

exec/noexec 是否有执行文件的权限,exec

auto/noauto 执行mount -a时,此文件系统是否被主动挂载,默认auto

rw/ro 读写,只读, rw

第五列:是否使用dump命令进行备份 0 不备份,1 每天备份 2 不定期的备份

第六列:通过什么方式让fsck检查磁盘, 0 不检查 , 1 检查,/ 2 其他的分区 ,检查的顺序


5. Swap介绍

**企业案例:java内存不足时,大量占用swap。**

```shell

#1.添加一个1G分区

#2.格式化为swap

[root@oldboyedu ~]# mkswap /dev/sdb5

[root@oldboyedu ~]# free -m

total used free shared buff/cache available

Mem: 1980 107 1529 9 343 1694

Swap: 2047 0 2047

[root@oldboyedu ~]# swapon /dev/sdb5

[root@oldboyedu ~]# free -m

total used free shared buff/cache available

Mem: 1980 109 1527 9 343 1692

Swap: 3071 0 3071

[root@oldboyedu ~]# swapon -s

Filename Type Size Used Priority

/dev/sda2 partition 2097148 0 -2

/dev/sdb5 partition 1048572 0 -3

[root@oldboyedu ~]# swapoff /dev/sdb5

[root@oldboyedu ~]# free -m

total used free shared buff/cache available

Mem: 1980 107 1529 9 343 1693

Swap: 2047 0 2047

[root@oldboyedu ~]# swapoff -a

[root@oldboyedu ~]# free -m

total used free shared buff/cache available

Mem: 1980 106 1531 9 342 1694

Swap: 0 0 0

[root@oldboyedu ~]# swapon -a

[root@oldboyedu ~]# free -m

total used free shared buff/cache available

Mem: 1980 107 1530 9 342 1694

Swap: 2047 0 2047

#生成一个大文件

[root@oldboyedu ~]# dd /root/swap_file bs=10M count=100

#格式化该文件为swap文件

[root@oldboyedu ~]# mkswap -f /root/swap_file

Setting up swapspace version 1, size = 1023996 KiB

no label, UUID=6930c391-2924-464b-a126-be1f85b953b6

[root@oldboyedu ~]# file /root/swap_file

/root/swap_file: Linux/i386 swap file (new style), version 1 (4K pages), size 255999 pages, no label, UUID=6930c391-2924-464b-a126-be1f85b953b6

[root@oldboyedu ~]# free -m

total used free shared buff/cache available

Mem: 1980 107 499 9 1374 1679

Swap: 2047 0 2047

#设置权限位600

[root@oldboyedu ~]# chmod 600 /root/swap_file

#添加swap大小

[root@oldboyedu ~]# swapon -f /root/swap_file

[root@oldboyedu ~]# free -m

total used free shared buff/cache available

Mem: 1980 108 497 9 1374 1678

Swap: 3047 0 3047


6. RAID概述


#1. 得到了更大的容量

#2. 得到的数据安全,冗余。

#3. 得到更好的性能。

RAID级别

RAID0 1 5 10

特点

RAID0:(条带卷) N*单块容量 最少两块,(支持一块) 读写最快 没有容错机制 只要求速度,不要求安全。

RAID1:(镜像卷) 一半 只能两块 读一般,写特慢 50% 只要求安全,不要求速度

RAID5:(校验卷) 浪费一块 至少三块, 读写稍快 只能坏一块 对安全和速度要求都不高。

RAID10: RAID1+RAID0 (镜像条带卷) 一般 至少4块 读写很快 50% 对安全和速度都有要求

总结:

冗余从好到坏:RAID1---RAID10---RAID5---RAID0

性能从好到差:RAID0---RAID10---RAID5---RAID1

价格从低到高:RAID0---RAID5---RAID1---RAID10

“`

7 LVM逻辑卷

“`

物理卷(PV)

卷组(VG)

逻辑卷(LV)

基本单元(PE)大小4MB

```

### 11. LVM实战

```shell

环境准备

[root@oldboyedu ~]# lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT

sda 8:0 0 50G 0 disk

├─sda1 8:1 0 200M 0 part /boot

├─sda2 8:2 0 2G 0 part [SWAP]

└─sda3 8:3 0 47.8G 0 part /

sdb 8:16 0 20G 0 disk

sdc 8:32 0 20G 0 disk

#下载软件包

[root@oldboyedu ~]# yum install -y lvm2

#创建物理卷

[root@oldboyedu ~]# pvcreate /dev/sdb

Physical volume "/dev/sdb" successfully created.

#创建卷组并添加物理卷

[root@oldboyedu ~]# vgcreate lvm_test /dev/sdb

Volume group "lvm_test" successfully created

#创建逻辑卷

[root@oldboyedu ~]# lvcreate -L 1G -n lvm1 lvm_test

Logical volume "lvm1" created.

#检查

[root@oldboyedu ~]# vgs

VG #PV #LV #SN Attr VSize VFree

lvm_test 1 1 0 wz--n- <20.00g <19.00g

[root@oldboyedu ~]# lvs

LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert

lvm1 lvm_test -wi-a----- 1.00g

[root@oldboyedu ~]# pvs

PV VG Fmt Attr PSize PFree

/dev/sdb lvm_test lvm2 a-- <20.00g <19.00g

#根据卷组大小比例进行分配大小

[root@oldboyedu ~]# lvcreate -l 20%FREE -n lvm2 lvm_test

Logical volume "lvm2" created.

#格式化逻辑卷

[root@oldboyedu ~]# mkfs.xfs /dev/lvm_test/lvm1

meta-data=/dev/lvm_test/lvm1 isize=512 agcount=4, agsize=65536 blks

= sectsz=512 attr=2, projid32bit=1

= crc=1 finobt=0, sparse=0

data = bsize=4096 blocks=262144, imaxpct=25

= sunit=0 swidth=0 blks

naming =version 2 bsize=4096 ascii-ci=0 ftype=1

log =internal log bsize=4096 blocks=2560, version=2

= sectsz=512 sunit=0 blks, lazy-count=1

realtime =none extsz=4096 blocks=0, rtextents=0

#创建挂载点,并进行挂载

[root@oldboyedu ~]# mkdir /lvm1

[root@oldboyedu ~]#

[root@oldboyedu ~]#

[root@oldboyedu ~]# mount /dev/lvm_test/lvm1 /lvm1

[root@oldboyedu ~]# ll /lvm1

total 0

[root@oldboyedu ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/sda3 48G 4.1G 44G 9% /

devtmpfs 980M 0 980M 0% /dev

tmpfs 991M 0 991M 0% /dev/shm

tmpfs 991M 9.6M 981M 1% /run

tmpfs 991M 0 991M 0% /sys/fs/cgroup

/dev/sda1 197M 105M 93M 54% /boot

tmpfs 199M 0 199M 0% /run/user/0

/dev/mapper/lvm_test-lvm1 1014M 33M 982M 4% /lvm1


8.卷组的管理


#扩大卷组的大小

#扩大卷组的大小

[root@oldboyedu ~]# pvcreate /dev/sdc

Physical volume "/dev/sdc" successfully created.

[root@oldboyedu ~]# vgextend lvm_test /dev/sdc

Volume group "lvm_test" successfully extended

[root@oldboyedu ~]# vgs

VG #PV #LV #SN Attr VSize VFree

lvm_test 2 2 0 wz--n- 39.99g <35.20g

[root@oldboyedu ~]# pvs

PV VG Fmt Attr PSize PFree

/dev/sdb lvm_test lvm2 a-- <20.00g <15.20g

/dev/sdc lvm_test lvm2 a-- <20.00g <20.00g

#缩减卷组的大小

[root@oldboyedu ~]# pvs

PV VG Fmt Attr PSize PFree

/dev/sdb lvm_test lvm2 a-- <20.00g <15.20g

/dev/sdc lvm_test lvm2 a-- <20.00g <20.00g

[root@oldboyedu ~]# pvmove /dev/sdb /dev/sdc

/dev/sdb: Moved: 1.14%

/dev/sdb: Moved: 20.85%

/dev/sdb: Moved: 100.00%

[root@oldboyedu ~]# pvs

PV VG Fmt Attr PSize PFree

/dev/sdb lvm_test lvm2 a-- <20.00g <20.00g

/dev/sdc lvm_test lvm2 a-- <20.00g <15.20g

[root@oldboyedu ~]# vgreduce lvm_test /dev/sdb

Removed "/dev/sdb" from volume group "lvm_test"

[root@oldboyedu ~]# vgs

VG #PV #LV #SN Attr VSize VFree

lvm_test 1 2 0 wz--n- <20.00g <15.20g

**逻辑卷的管理**

#扩容

[root@oldboyedu ~]# lvs

LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert

lvm1 lvm_test -wi-ao---- 1.00g

lvm2 lvm_test -wi-a----- <3.80g

[root@oldboyedu ~]# lvextend -L +1G /dev/lvm_test/lvm1

Size of logical volume lvm_test/lvm1 changed from 1.00 GiB (256 extents) to 2.00 GiB (512 extents).

Logical volume lvm_test/lvm1 successfully resized.

[root@oldboyedu ~]# lvs

LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert

lvm1 lvm_test -wi-ao---- 2.00g

lvm2 lvm_test -wi-a----- <3.80g

[root@oldboyedu ~]# xfs_growfs /dev/lvm_test/lvm1

meta-data=/dev/mapper/lvm_test-lvm1 isize=512 agcount=4, agsize=65536 blks

= sectsz=512 attr=2, projid32bit=1

= crc=1 finobt=0 spinodes=0

data = bsize=4096 blocks=262144, 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 262144 to 524288

[root@oldboyedu ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/sda3 48G 4.1G 44G 9% /

devtmpfs 980M 0 980M 0% /dev

tmpfs 991M 0 991M 0% /dev/shm

tmpfs 991M 9.6M 981M 1% /run

tmpfs 991M 0 991M 0% /sys/fs/cgroup

/dev/sda1 197M 105M 93M 54% /boot

tmpfs 199M 0 199M 0% /run/user/0

/dev/mapper/lvm_test-lvm1 2.0G 33M 2.0G 2% /lvm1

[root@oldboyedu ~]# dd /lvm1/test.txt bs=100M count=18

18+0 records in

18+0 records out

1887436800 bytes (1.9 GB) copied, 3.96192 s, 476 MB/s

[root@oldboyedu ~]# ll -h /lvm1/

total 1.8G

-rw-r--r-- 1 root root 1.8G Aug 3 17:26 test.txt

#删除逻辑卷

[root@oldboyedu ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/sda3 48G 4.1G 44G 9% /

devtmpfs 980M 0 980M 0% /dev

tmpfs 991M 0 991M 0% /dev/shm

tmpfs 991M 9.6M 981M 1% /run

tmpfs 991M 0 991M 0% /sys/fs/cgroup

/dev/sda1 197M 105M 93M 54% /boot

tmpfs 199M 0 199M 0% /run/user/0

/dev/mapper/lvm_test-lvm1 4.9G 1.8G 3.1G 38% /lvm1

[root@oldboyedu ~]# umount /lvm1/

[root@oldboyedu ~]#

[root@oldboyedu ~]# lvremove /dev/lvm_test/lvm1

Do you really want to remove active logical volume lvm_test/lvm1? [y/n]: y

Logical volume "lvm1" successfully removed

“`

磁盘故障

“`shell

环境准备

[root@oldboyedu ~]# lsblk

NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT

sda 8:0 0 50G 0 disk

├─sda1 8:1 0 200M 0 part /boot

├─sda2 8:2 0 2G 0 part [SWAP]

└─sda3 8:3 0 47.8G 0 part /

sdb 8:16 0 20G 0 disk

├─sdb1 8:17 0 1G 0 part

└─sdb2 8:18 0 200M 0 part

sr0 11:0 1 4.3G 0 rom

[root@oldboyedu ~]# mkfs.xfs /dev/sdb1

[root@oldboyedu ~]# mount /dev/sdb1 /block

#出现报错

[root@oldboyedu ~]# cp test.txt /block/

cp: error writing ‘/block/test.txt’: No space left on device

cp: failed to extend ‘/block/test.txt’: No space left on device

[root@oldboyedu ~]#

#解决思路

[root@oldboyedu ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/sda3 48G 4.1G 44G 9% /

devtmpfs 980M 0 980M 0% /dev

tmpfs 991M 0 991M 0% /dev/shm

tmpfs 991M 9.6M 981M 1% /run

tmpfs 991M 0 991M 0% /sys/fs/cgroup

/dev/sda1 197M 105M 93M 54% /boot

tmpfs 199M 0 199M 0% /run/user/0

/dev/sdb1 1014M 1014M 32K 100% /block

#查找大文件

[root@oldboyedu ~]# du -sh /* |grep G

#经过领导的同意再去处理

rm -f

#inode号满了

[root@oldboyedu ~]# touch /inode/123

touch: cannot touch ‘/inode/123’: No space left on device

#排查思路

[root@oldboyedu ~]# df -h

Filesystem Size Used Avail Use% Mounted on

/dev/sda3 48G 4.1G 44G 9% /

devtmpfs 980M 0 980M 0% /dev

tmpfs 991M 0 991M 0% /dev/shm

tmpfs 991M 9.6M 981M 1% /run

tmpfs 991M 0 991M 0% /sys/fs/cgroup

/dev/sda1 197M 105M 93M 54% /boot

tmpfs 199M 0 199M 0% /run/user/0

/dev/sdb1 1014M 1014M 32K 100% /block

/dev/sdb2 197M 64M 133M 33% /inode

[root@oldboyedu ~]# df -i

Filesystem Inodes IUsed IFree IUse% Mounted on

/dev/sda3 25062912 73014 24989898 1% /

devtmpfs 250789 416 250373 1% /dev

tmpfs 253514 1 253513 1% /dev/shm

tmpfs 253514 766 252748 1% /run

tmpfs 253514 16 253498 1% /sys/fs/cgroup

/dev/sda1 102400 325 102075 1% /boot

tmpfs 253514 1 253513 1% /run/user/0

/dev/sdb1 128 5 123 4% /block

/dev/sdb2 102592 102592 0 100% /inode

#查找大于1MB的目录

[root@oldboyedu ~]# find / -type d -size +1M |xargs ls -lhd

drwxr-xr-x 2 root root 2.4M Aug 3 18:00 /inode/test/data

#检查

[root@oldboyedu ~]# ll /inode/test/data |wc -l

102401

#经过领导的同意,是否可以删除

[root@oldboyedu ~]# find /inode/test/data -type f -name "*.txt" -delete

[root@oldboyedu ~]# find /inode/test/data -type f -name "*.log" -delete

你可能感兴趣的:(磁盘管理)