linux磁盘管理

1.MBR:共512字节

446字节bootloader主引导程序

64字节磁盘分区表

2字节魔数

2.主分区和逻辑分区

主分区最多是4个

扩展分区最多一个,另外扩展分区不能被直接使用而是制作成逻辑分区使用

逻辑分区序列号是从5开始的

逻辑分区可以有很多个,取决于扩展分区大小

 

3.fdisk管理分区

n:添加新的分区

p:查看分区信息

w:保存退出

q:不保存退出

d:删除分区

t:改变分区类型

 

4.新建分区的使用

1)在虚拟机上添加一块硬盘

2)fdisk创建新分区:+500M选择大小

[root@li ~]# 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 0xe140b235.

 

Command (m for help): n                          #新建分区

Partition type:

p primary (0 primary, 0 extended, 4 free)

e extended

Select (default p): p                            #选择主分区

Partition number (1-4, default 1):

First sector (2048-41943039, default 2048):    #分区磁盘起始位,默认值,回车即可

Using default value 2048

Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G    #磁盘大小选择,加号不能省略,回车即可

Partition 1 of type Linux and of size 5 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: 0xe140b235

 

Device Boot Start End Blocks Id System

/dev/sdb1 2048 10487807 5242880 83 Linux

 

Command (m for help): W    #w保存退出,q是不保存退出

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

Syncing disks.

3)格式化

[root@li ~]# mkfs.xfs /dev/sdb1     #格式化成centos7系统默认的文件系统类型XFS

meta-data=/dev/sdb1 isize=512 agcount=4, agsize=327680 blks

= sectsz=512 attr=2, projid32bit=1

= crc=1 finobt=0, sparse=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 log bsize=4096 blocks=2560, version=2

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

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

4)挂载使用

mount /dev/sdb1 /test

 

5)开机自启

echo "/dev/sdb1 /test xfs defaults 0 0" >> /etc/fstab

 

echo "mount/dev/sdb1/test" >> /etc/rc.local

chmod +x /etc/rc.d/rc.local

 

5.fdisk创建逻辑分区

1)fdisk添加扩展分区

[root@li ~]# 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     #选择扩展分区

Partition number (2-4, default 2):

First sector (10487808-41943039, default 10487808):

Using default value 10487808

Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +5G    #5G大小

Partition 2 of type Extended and of size 5 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: 0xe140b235

 

Device Boot Start End Blocks Id System

/dev/sdb1 2048 10487807 5242880 83 Linux

/dev/sdb2 10487808 20973567 5242880 5 Extended

 

Command (m for help): w   #保存退出

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

Syncing disks.

2)创建逻辑分区

[root@li ~]# 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    #创建逻辑分区

Adding logical partition 5

First sector (10489856-20973567, default 10489856):

Using default value 10489856

Last sector, +sectors or +size{K,M,G} (10489856-20973567, default 20973567): +10G   #逻辑分区大小不能超过扩展分区大小

Value out of range.

Last sector, +sectors or +size{K,M,G} (10489856-20973567, default 20973567): +2g

Unsupported suffix: ‘g’.

Supported: 10^N: KB (KiloByte), MB (MegaByte), GB (GigaByte)

2^N: K (KibiByte), M (MebiByte), G (GibiByte)

Last sector, +sectors or +size{K,M,G} (10489856-20973567, default 20973567): +2G

Partition 5 of type Linux and of size 2 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: 0xe140b235

 

Device Boot Start End Blocks Id System

/dev/sdb1 2048 10487807 5242880 83 Linux

/dev/sdb2 10487808 20973567 5242880 5 Extended

/dev/sdb5 10489856 14684159 2097152 83 Linux

 

Command (m for help): W

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

Syncing disks.

3)创建文件系统

同上

4)开机自动挂载

同上

 

6.LVM逻辑卷

功能/命令 物理卷管理 卷组管理 逻辑卷管理
扫描 pvscan vgscan lvscan
建立 pvcreate vgcreate lvcreate
显示 pvdisplay vgdisplay lvdisplay
删除 pvremove vgremove lvremove
扩展   vgextend lvextend
缩小   vgreduce lvreduce

物理卷 - ->卷组 - ->逻辑卷

1)创建物理卷

pvcreate /dev/sd{b,c}

 

2)创建卷组

vgcreate vg_li /dev/sd{b,c}

 

3)创建逻辑卷

lvcreate –n lv_li –L 500M vg_li

 

4)格式化

mkfs.xfs /dev/vg_li/lv_li

 

5)挂载使用

mount /dev/vg_li/lv_li /test

 

注意:卸载命令umount

后面跟挂载点或者磁盘都可以

 

7.LVM扩容

lvextend –L +300M /dev/vg_li/lv_li

lvextend –L 800M /dev/vg_li/lv_li

 

xfs_growfs /dev/vg_li/lv_li

 

8.交换分区SWAP

1)创建分区

[root@li ~]# 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): p

Partition number (3,4, default 3):

First sector (20973568-41943039, default 20973568):

Using default value 20973568

Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): +2G

Partition 3 of type Linux and of size 2 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: 0xe140b235

 

Device Boot Start End Blocks Id System

/dev/sdb1 2048 10487807 5242880 83 Linux

/dev/sdb2 10487808 20973567 5242880 5 Extended

/dev/sdb3 20973568 25167871 2097152 83 Linux

/dev/sdb5 10489856 14684159 2097152 83 Linux

/dev/sdb6 14686208 18880511 2097152 83 Linux

/dev/sdb7 18882560 20973567 1045504 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.

2)识别新创建的分区

[root@li ~]# ls /dev/sd*

/dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdb1 /dev/sdb2 /dev/sdb5 /dev/sdb6 /dev/sdb7

[root@li ~]# partprobe     #加载新建分区

Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.

[root@li ~]# ls /dev/sd*

/dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdb1 /dev/sdb2 /dev/sdb3 /dev/sdb5 /dev/sdb6 /dev/sdb7

3)格式化为swap类型

[root@li ~]# mkswap /dev/sdb3

Setting up swapspace version 1, size = 2097148 KiB

no label, UUID=b184f890-2ec4-417d-9aef-1ac87a78464b

4)启动swap

[root@li ~]# free -h

total used free shared buff/cache available

Mem: 974M 89M 751M 7.6M 133M 733M

Swap: 2.0G 0B 2.0G

[root@li ~]# swapon /dev/sdb3

[root@li ~]# free -h

total used free shared buff/cache available

Mem: 974M 90M 750M 7.6M 133M 732M

Swap: 4.0G 0B 4.0G

5)开机自启

echo “/dev/sdb3 swap swap defaults 0 0”  >> /etc/fstab

echo “swapon /dev/sdb3” >> /etc/rc.local

9.创建交换分区SWAP–本地文件

 

1)创建目录

[root@li ~]# mkdir /swap

2)创建文件

[root@li ~]# dd if=/dev/zero of=/swap/swap bs=2M count=2014

2014+0 records in

2014+0 records out

4223664128 bytes (4.2 GB) copied, 29.3039 s, 144 MB/s

3)格式化

[root@li ~]# mkswap /swap/swap

Setting up swapspace version 1, size = 4124668 KiB

no label, UUID=211cb23a-49b5-46fc-b568-f167d4b6d06a

 

4)写入文件

[root@li ~]# echo “/swap/swap swap swap defaults 0 0” >> /etc/fstab

[root@li ~]# mount -a

[root@li ~]# free -h

total used free shared buff/cache available

Mem: 974M 85M 69M 7.6M 819M 712M

Swap: 4.0G 0B 4.0G

[root@li ~]# swapon -a

swapon: /swap/swap: insecure permissions 0644, 0600 suggested.

[root@li ~]# free -h

total used free shared buff/cache available

Mem: 974M 88M 66M 7.6M 819M 709M

Swap: 7.9G 0B 7.9G

5)修改权限

[root@li ~]# chmod 0600 /swap/swap
原文地址:https://www.cnblogs.com/Agnostida-Trilobita/p/11142004.html