磁盘管理
MBR
512字节
三部分组成
主引导程序:bootloader 446
分区表:64字节
魔数:55AA
主分区:最多是4个。编号默认是从1开始。
扩展分区:最多是一个,可以是0个。
逻辑分区:可以很多个,逻辑分区是建立在扩展分区之上的
扩展分区不能使用的,它只是做为逻辑分区的容器存在的,先创建一个扩展分区,在拓展分区之上创建逻辑分区;
我们真正存放数据的是主分区和逻辑分区,大量数据都放在逻辑分区中。
文件系统
centos6系统默认的是ext4
centos7系统默认的是xfs
Ext4:Ext3的改进版本,作为RHEL 6系统中的默认文件管理系统,
它支持的存储容量高达1EB(1EB=1,073,741,824GB),且能够有无限多的子目录。
另外,Ext4文件系统能够批量分配block块,从而极大地提高了读写效率。
XFS:是一种高性能的日志文件系统,而且是RHEL 7中默认的文件管理系统,
它的优势在发生意外宕机后尤其明显,即可以快速地恢复可能被破坏的文件,
而且强大的日志功能只用花费极低的计算和存储性能。
并且它最大可支持的存储容量为18EB,这几乎满足了所有需求。
fdisk分区管理工具
n 添加新分区
p 打印分区信息
w 保存分区信息并退出
q 不保存退出
例子1:创建主分区
第一步:创建主分区
[root@ken1 ~]# 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 0x1eff4d75.
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)
Command (m for help): n #创建新的分区
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): #创建主分区,回车默认是主分区
Using default response p
Partition number (1-4, default 1): #回车默认编号为1
First sector (2048-41943039, default 2048): #默认分区起始值
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +30G #分区大小不能超过磁盘总大小
Value out of range.
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +1G #想要分区的大小
Partition 1 of type Linux and of size 1 GiB is set
Command (m for help): P #打印分区信息
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x1eff4d75
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
Command (m for help): W #保存当前分区信息
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
第二步:查看分区
[root@ken1 ~]# ls /dev/sdb*
/dev/sdb /dev/sdb1
第三步:进行格式化就是安装文件系统的过程
[root@ken1 ~]# mkfs.xfs /dev/sdb1
第四步:挂载使用
[root@ken1 ~]# mount /dev/sdb1 /ken
[root@ken1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.3G 16G 8% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 488M 0 488M 0% /dev/shm
tmpfs 488M 7.7M 480M 2% /run
tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda1 1014M 142M 873M 14% /boot
/dev/sr0 4.2G 4.2G 0 100% /mnt
tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb1 1014M 33M 982M 4% /ken
[root@ken1 ~]# mkdir /ken #创建挂载点
[root@ken1 ~]# mount /dev/sdb1 /ken #把磁盘分区挂载到linux系统一个目录下
[root@ken1 ~]# df -h #查看挂载信息
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.3G 16G 8% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 488M 0 488M 0% /dev/shm
tmpfs 488M 7.7M 480M 2% /run
tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda1 1014M 142M 873M 14% /boot
/dev/sr0 4.2G 4.2G 0 100% /mnt
tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb1 1014M 33M 982M 4% /ken
第五步:验证数据写入位置 #数据写入磁盘中
[root@ken1 ~]# touch /ken/{1..100}.txt
[root@ken1 ~]# ls /ken
100.txt 17.txt 24.txt 31.txt 39.txt 46.txt 53.txt 60.txt 68.txt 75.txt 82.txt 8.txt 97.txt
10.txt 18.txt 25.txt 32.txt 3.txt 47.txt 54.txt 61.txt 69.txt 76.txt 83.txt 90.txt 98.txt
11.txt 19.txt 26.txt 33.txt 40.txt 48.txt 55.txt 62.txt 6.txt 77.txt 84.txt 91.txt 99.txt
12.txt 1.txt 27.txt 34.txt 41.txt 49.txt 56.txt 63.txt 70.txt 78.txt 85.txt 92.txt 9.txt
13.txt 20.txt 28.txt 35.txt 42.txt 4.txt 57.txt 64.txt 71.txt 79.txt 86.txt 93.txt
14.txt 21.txt 29.txt 36.txt 43.txt 50.txt 58.txt 65.txt 72.txt 7.txt 87.txt 94.txt
15.txt 22.txt 2.txt 37.txt 44.txt 51.txt 59.txt 66.txt 73.txt 80.txt 88.txt 95.txt
16.txt 23.txt 30.txt 38.txt 45.txt 52.txt 5.txt 67.txt 74.txt 81.txt 89.txt 96.txt
[root@ken1 ~]# umount /ken
[root@ken1 ~]# ls /ken
[root@ken1 ~]# mkdir /ken1
[root@ken1 ~]# mount /dev/sdb1 /ken1
[root@ken1 ~]# ls /ken1
100.txt 17.txt 24.txt 31.txt 39.txt 46.txt 53.txt 60.txt 68.txt 75.txt 82.txt 8.txt 97.txt
10.txt 18.txt 25.txt 32.txt 3.txt 47.txt 54.txt 61.txt 69.txt 76.txt 83.txt 90.txt 98.txt
11.txt 19.txt 26.txt 33.txt 40.txt 48.txt 55.txt 62.txt 6.txt 77.txt 84.txt 91.txt 99.txt
12.txt 1.txt 27.txt 34.txt 41.txt 49.txt 56.txt 63.txt 70.txt 78.txt 85.txt 92.txt 9.txt
13.txt 20.txt 28.txt 35.txt 42.txt 4.txt 57.txt 64.txt 71.txt 79.txt 86.txt 93.txt
14.txt 21.txt 29.txt 36.txt 43.txt 50.txt 58.txt 65.txt 72.txt 7.txt 87.txt 94.txt
15.txt 22.txt 2.txt 37.txt 44.txt 51.txt 59.txt 66.txt 73.txt 80.txt 88.txt 95.txt
16.txt 23.txt 30.txt 38.txt 45.txt 52.txt 5.txt 67.txt 74.txt 81.txt 89.txt 96.txt
实现开机自动挂载
方法一:
[root@ken1 ~]# vim /etc/fstab
/dev/sdb1 /ken xfs defaults 0 0
写到该文件中系统容易起不来,解决办法
bios方法:
看视频!
方法二:
[root@ken1 ~]# vim /etc/rc.local
mount /dev/sdb1 /ken
[root@ken1 ~]# chmod +x /etc/rc.d/rc.local #一定要加执行权限,否则文件中的命令无法执行
例子2:创建扩展分区
第一步:创建扩展分区
[root@ken1 ~]# 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.
Command (m for help): n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e #创建拓展分区选择e
Partition number (2-4, default 2): 3 #分区编号可以在范围内自由选择
First sector (2099200-41943039, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-41943039, default 41943039): +3G
Partition 3 of type Extended and of size 3 GiB is set
Command (m for help): P
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x1eff4d75
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb3 2099200 8390655 3145728 5 Extended
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@ken1 ~]# ls /dev/sdb*
/dev/sdb /dev/sdb1
发现没有新创建的磁盘
第三步:同步分区表
[root@ken1 ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@ken1 ~]# ls /dev/sdb*
/dev/sdb /dev/sdb1 /dev/sdb3
例子3:创建逻辑分区
第一步:挂载逻辑分区
[root@ken1 ~]# 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.
Command (m for help): n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l #选择l,逻辑分区编号是从5开始的,即使主分区的编号没用完
Adding logical partition 5
First sector (2101248-8390655, default 2101248):
Using default value 2101248
Last sector, +sectors or +size{K,M,G} (2101248-8390655, default 8390655): +4G
Value out of range.
Last sector, +sectors or +size{K,M,G} (2101248-8390655, default 8390655): +1G
Partition 5 of type Linux and of size 1 GiB is set
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x1eff4d75
Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb3 2099200 8390655 3145728 5 Extended
/dev/sdb5 2101248 4198399 1048576 83 Linux
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@ken1 ~]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
[root@ken1 ~]# ls /dev/sdb*
/dev/sdb /dev/sdb1 /dev/sdb3 /dev/sdb5
第二步:格式化
[root@ken1 ~]# mkfs.xfs /dev/sdb5
第三步:挂载
[root@ken1 ~]# mount /dev/sdb5 /ken1
[root@ken1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.3G 16G 8% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 488M 0 488M 0% /dev/shm
tmpfs 488M 7.7M 480M 2% /run
tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda1 1014M 142M 873M 14% /boot
/dev/sr0 4.2G 4.2G 0 100% /mnt
/dev/sdb1 1014M 33M 982M 4% /ken
tmpfs 98M 0 98M 0% /run/user/0
/dev/sdb5 1014M 33M 982M 4% /ken1
第四步:开机自启
echo "mount /dev/sdb5 /ken" >> /etc/rc.local
swap交换分区
swap交换分区就是把磁盘中一块当中内存使用
工作中建议不用!
如果工作中发现交换分区用的太多,需要考虑加大内存!
工作中建议交换分区大小设置为2倍左右
16G 32G
查看交换分区:
[root@ken1 ~]# free -h
total used free shared buff/cache available
Mem: 974M 87M 751M 7.6M 134M 732M
Swap: 2.0G 0B 2.0G
例子1:添加2G交换分区
第一步:创建分区
略
第二步:格式化为swap类型
[root@ken1 ~]# mkswap /dev/sdb2
第三步:启动swap交换分区
[root@ken1 ~]# swapon /dev/sdb2
[root@ken1 ~]# free -h
total used free shared buff/cache available
Mem: 974M 88M 749M 7.6M 135M 730M
Swap: 4.0G 0B 4.0G
第四步:swap交换分区开机自启
vim /etc/fstab
/dev/sdb2 swap swap defaults 0 0
LVM逻辑卷
物理卷PV(physical volume) :物理卷就是LVM的基本存储逻辑块,但和基本的物理存储介质
(如分区、磁盘等)比较,却包含有与LVM相关的管理参数,创建物理卷它可以用硬盘分区,也可以用硬盘本身;
卷组VG(Volume Group) :一个LVM卷组由一个或多个物理卷组成??
逻辑卷LV(logical volume) :LV建立在VG之上,可以在LV之上建立文件系统
例子1:创建逻辑卷
第一步:创建物理卷
[root@ken1 ~]# pvcreate /dev/sd{b,c,d}
Physical volume "/dev/sdb" successfully created.
Physical volume "/dev/sdc" successfully created.
Physical volume "/dev/sdd" successfully created
第二步:创建卷组
[root@ken1 ~]# vgcreate ken /dev/sd{b,c,d}
Volume group "ken" successfully created
第三步:查看卷组信息
[root@ken1 ~]# vgdisplay ken
第四步:创建逻辑卷
[root@ken1 ~]# lvcreate -L 5G -n lv_ken ken # -L指定创建磁盘的大小,-l指定PE数量,PE大小4M
第五步:查看逻辑卷详细
[root@ken1 ~]# lvdisplay /dev/ken/lv_ken
第六步:格式化
[root@ken1 ~]# mkfs.xfs /dev/ken/lv_ken
第七步:挂载
[root@ken1 ~]# mount /dev/ken/lv_ken /ken2
[root@ken1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.3G 16G 8% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 488M 0 488M 0% /dev/shm
tmpfs 488M 7.7M 480M 2% /run
tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda1 1014M 142M 873M 14% /boot
/dev/sr0 4.2G 4.2G 0 100% /mnt
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/ken-lv_ken 5.0G 33M 5.0G 1% /ken2
LVM扩容
ext4扩容不能在线扩容,必须卸载才能扩容,支持扩容和缩容
xfs支持在线动态扩容,不支持缩容
例子:xfs格式扩容
第一步:扩容
[root@ken1 ~]# lvextend -L +5G /dev/ken/lv_ken
第二步:查看磁盘大小
[root@ken1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.3G 16G 8% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 488M 0 488M 0% /dev/shm
tmpfs 488M 7.7M 480M 2% /run
tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda1 1014M 142M 873M 14% /boot
/dev/sr0 4.2G 4.2G 0 100% /mnt
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/ken-lv_ken 5.0G 33M 5.0G 1% /ken2
/dev/mapper/ken-lv_ken1 5.0G 33M 5.0G 1% /ken3
第三步:执行xfs_growfs
[root@ken1 ~]# xfs_growfs /dev/ken/lv_ken
meta-data=/dev/mapper/ken-lv_ken isize=512 agcount=4, agsize=327680 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0 spinodes=0
data = bsize=4096 blocks=1310720, 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 1310720 to 2621440
第四步:再次查看磁盘大小
[root@ken1 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/centos-root 17G 1.3G 16G 8% /
devtmpfs 476M 0 476M 0% /dev
tmpfs 488M 0 488M 0% /dev/shm
tmpfs 488M 7.7M 480M 2% /run
tmpfs 488M 0 488M 0% /sys/fs/cgroup
/dev/sda1 1014M 142M 873M 14% /boot
/dev/sr0 4.2G 4.2G 0 100% /mnt
tmpfs 98M 0 98M 0% /run/user/0
/dev/mapper/ken-lv_ken 10G 33M 10G 1% /ken2
/dev/mapper/ken-lv_ken1 5.0G 33M 5.0G 1% /ken3
LVM逻辑卷删除
第一步:卸载光盘
[root@ken1 ~]# umount /ken2
第二步:删除逻辑卷
[root@ken1 ~]# lvremove /dev/ken/lv_ken
Do you really want to remove active logical volume ken/lv_ken? [y/n]: y
Logical volume "lv_ken" successfully removed
第三步:删除卷组
[root@ken1 ~]# vgremove ken
Volume group "ken" successfully removed
第四步:删除物理卷
[root@ken1 ~]# pvremove /dev/sd{b,c,d}
Labels on physical volume "/dev/sdb" successfully wiped.
Labels on physical volume "/dev/sdc" successfully wiped.
Labels on physical volume "/dev/sdc" successfully wiped.
Labels on physical volume "/dev/sdd" successfully wiped.
作业:扩容根磁盘在原有基础上加10G
[root@localhost ~]#pvcreate /dev/sdb
[root@localhost ~]# vgextend /dev/centos /dev/sdb
Volume group "centos" successfully extended
[root@localhost ~]# lvextend -L +10G /dev/centos/root
Size of logical volume centos/root changed from <17.00 GiB (4351 extents) to <27.00 GiB (6911 extents).
Logical volume centos/root successfully resized.
[root@localhost ~]# xfs_growfs /dev/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 7076864
[root@localhost ~]# df -h
文件系统 容量 已用 可用 已用% 挂载点
/dev/mapper/centos-root 27G 1.2G 26G 5% /
devtmpfs 475M 0 475M 0% /dev
tmpfs 487M 0 487M 0% /dev/shm
tmpfs 487M 7.7M 479M 2% /run
tmpfs 487M 0 487M 0% /sys/fs/cgroup
/dev/sda1 1014M 133M 882M 14% /boot