Linux系统的磁盘管理

linux系统中的磁盘管理

1.本地存储设备的识别

fdisk  -l                          ##真实存在的设备


cat /proc/partition        ##系统识别的设备


blkid                              ##系统可使用的设备


df                                   ##系统正在挂载的设备

2.设备的挂载和卸载

a.设备名称
/dev/xdx                  ##/dev/hd0系统下hd的第0个分区 /dev/hd1/dev/sda /dev/sda /dev/sdb /dev/sda1 /dev/ada2 /dev/sdb1
/dev/sr0                  ##光驱
/dev/mapper/*       ##虚拟设备


b.设备的挂载和卸载

mount       设备        挂载点

mount    /dev/sdb1    /mnt                     ##挂载sdb1到mnt

umount   /mnt  |        /dev/sdb1             ##卸载

mount -o ro 设备                                   ##只读挂载
mount                                                     ##查看挂载信息
mount -o remount,rw  /dev/sdb1  |  /mnt           ##重新读写挂载

c.解决设备正忙问题
[root@foundation1 ~]# umount /mnt
umount: /mnt: target is busy.                ##有程序在使用此设备
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
解决方法一:
  fuser   -kvm   /mnt
  umount   /mnt

注意:fuser -vmk 表示显示、扫描并停止进程
方法二:
[root@foundation1 ~]# lsof    /mnt/
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
COMMAND PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
bash    599 root  cwd    DIR   8,16    20480    1 /mnt
[root@foundation1 ~]# kill -9 599
[root@foundation1 ~]# umount /mnt

3.磁盘分区

    MBR 分区表:硬盘0磁道1扇区的512个字节中记录的信息如下

    512=466+64+2

  1.MBR 主引导分区       446字节

  2.MPT 主分区表            64字节

  3.硬盘有效标示 "55aa"   2字节

  4.一个分区占用16字节,故有4个分区

    其中3个主分区,1个扩展分区,扩展分区中成为逻辑分区

(所谓扩展分区,严格地讲它不是一个实际意义的分区,它仅仅是一个指向下一个分区的指针,这种指针结构将形成一个单向链表。这样在主引导扇区中除了主分区 外,仅需要存储一个被称为扩展分区的分区数据,通过这个扩展分区的数据可以找到下一个分区(实际上也就是下一个逻辑磁盘)的起始位置,以此起始位置类推可 以找到所有的分区。无论系统中建立多少个逻辑磁盘,在主引导扇区中通过一个扩展分区的参数就可以逐个找到每一个逻辑磁盘【出自百度】)

  5.注意:MBR分区最大只能分2TB,可识别的分区总数为16,可用的为14

 

建立分区步骤

1.fdisk -l
2.fdisk /dev/vdb


m

   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    修改分区 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): p            主分区
Partition number (1-4, default 1):      id 用默认
First sector (2048-20971519, default 2048):     分区起始,一般用默认
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +500M     分区大小
Partition 1 of type Linux and of size 500 MiB is set

Command (m for help): P        显示信息

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: 0x79575e24

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048     1026047      512000   83  Linux

建立完成三个主分区

三个主分区建立完后,再建立时,系统会默认建立扩展分区,即从p成为e,剩下的空间全都留给了扩展分区e。

删除:

此处为第七分区的删除。

default    默认
分区:只能三个主分区,扩展分区:容器(延伸为逻辑分区)
           退出
wq         退出保存

注意:每次改变分区,退出fdisk /dev/vdb  注意partprobe同步分区

cat /proc/parttions         ##查看系统识别的磁盘分区信息

 

4.给设备安装文件系统

ext3    rhel5及之前的版本  最多支持32TB的文件系统和2t文件,实际2tb文件系统,16G文件

ext4    rhel16                        1EB                     16TB

xfs       rhel7                          18EB                    9EB                             7G/s                       4G/s

1EB=1024TB

mkfs.xfs         /dev/vdb1       ## 格式化/dev/vdb1

blkid               ##查看是否为xfs属性


mount /dev/vdb1  /mnt          挂载到/mnt/

df              ##查看已挂载的设备

永久挂载

vim /etc/fstab
设备           挂载点     文件系统      挂载参数        是否备份    是否检测
/dev/vdb1     /mnt            xfs              defaults                0                0

内容为;

/etc/rc.local 脚本命令     /etc/rc/fstab 检测策略(读取此文件更早)

mount -a  读取文件,启动挂载

注意:要先格式化变成文件系统才能挂载
如果/etc/rc/fstab文件编辑错误,再启动是无法正常启动的,根据提示输入超级用户密码,得到一个shell,重新编辑此文件,再reboot就可以了。

 

6.swap分区管理

1)swap分区建立
激活

新建swap分区    fdisk   /dev/vdb

先建立P分区

修改分区类型t     ##  L(小写) 查看所有分区类型的id

划分分区并设定分区标签位 82

退出保存时会报错

再同步分区即可。

mkswap  /dev/vdb6               ##格式化swap分区
swapon -a /dev/vdb6            ##挂载
swapon -s                              ##查看系统的swap分区信息

注意:以上设置swap分区是临时的,永久设置须修改配置文件
vim /etc/fstab

/dev/vdb6           swap          swap                       defaults                        0                0  
  设备               挂载点    文件系统类型      挂载参数,默认            不备份        不检测  

修改后  swapon  -a         ##检查报错

2)删除swap 分区

vim /etc/fastb

删除刚才所作的操作。

swapoff  /dev/vdb6


swapon -s

 

7.配额

配额是针对分区的

建立一个分区7

格式化分区7并同步分区

此文件中配额单位为k

注意:xfs 文件系统默认配额功能开启,ext4之前的文件系统需要quotaon  -n   /dev/vdb 命令激活

blocks         ##文件已占用分区大小                       

 soft             ##文件超出配额大小,报警

hard             ##文件不能超出配额大小                 
inodes         ##文件个数

soft              ##文件/目录个数,超额报警               
hard             ## 文件/目录个数不能超额

 

mount -o usrquota /dev/vdb7 /public
chmod 777 /public
edquota -u student

注意:先挂设备,再改权限

vim /etc/fstab
/dev/vdb7    /public      xfs      defaults,userquota    0     0

设置配额为20M

切换到student用户测试配额限制:

设置为10M时,实际也为10M

当超过设置的限额20M时,就无法再变大了。

 

8.磁盘加密

建立分区    fdisk   /dev/vdb

同步分区

cryptsetup luckFormat /dev/vdb1          ##加密此分区,并设置密码

cryptsetup open /dev/vdb8         打开锁,并给一个任意的名字,需要输入密码,再查看可以看到已加密。


mkfs.xfs /dev/mapper/westos          格式化磁盘文件


mount /dev/mapper/westos     /mnt/
touch /mnt/file{1..10}
umount /mnt/

挂载到mnt 建立文件,卸载后再删除是无法删除的。

卸载后再关闭,关闭后是无法再挂载的,也不能查看文件。

cryptsetup close westos

加密磁盘开机自动挂载
vim /etc/fstab
/dev/mapper/westos      /mnt    xfs     defaults     0     0

vim /etc/crypttab
输入:westos  /dev/vdb1        /root/westoskey

 

修改文件,使磁盘识别密码(westos为磁盘文件名)

vim /root/westoskey
输入锁密码:westos2018             之前设定的锁密码

  修改文件权限,设置root只读

     chmod  600  /root/westoskey

      取消磁盘加密:

       卸载  umount /mnt

      关闭磁盘文件  cryptsetup close test

       格式化磁盘    ##磁盘已加密,须强制格式化

        mkfs.xfs    /dev/vdb1    -f

 

    修改配置文件,还原设置

     vim    /etc/crypttab

     vim     /etc/fstab

    密码文件删除(可做可不做,建议删除)

     rm   -fr    /root/diskpass
 

9.建立gpt分区(gpt可大于2t,msdos最大2t)

 1.GUID 磁碟分割表

  GUID Partition Table:全局唯一标识磁盘分区表

  特性对比:

    MBR:3个主分区,1个扩展分区(下含逻辑分区),最多识别16个

    GPT:128个分区,管理硬盘大小18EB

    ext3:最多支持32TB

    ext4:最多支持1EB

    xfs:容量8EB-1byte

2.修改前准备(例/dev/vdb)

   将要分区的硬盘停止(卸载所有设备)

   将该磁盘格式化

3.修改MBR分区改为GPT分区

[root@server ~]# parted /dev/vdb
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel            
New disk label type? gpt   ##gpt类型                       
Warning: The existing disk label on /dev/vdb will be destroyed and all data on
this disk will be lost. Do you want to continue?
Yes/No? yes                                                               
(parted) quit      ##退出                                                
Information: You may need to update /etc/fstab.


##把类型从dos改为gpt
新建一个分区查看一下
[root@server ~]# fdisk /dev/vdb
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
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 number (1-128, default 1):
First sector (34-20971486, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-20971486, default 20971486): +100M
Created partition 1


Command (m for help): p

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: gpt     ##类型改变成功


#         Start          End    Size  Type            Name
 1         2048       206847    100M  Linux filesyste

改成lvm可在fdisk /dev/vdb中
Command (m for help): t     
Partition type (type L to list all types): l ##查看所有
Partition type (type L to list all types): 15  ##建立为lvm
Command (m for help): p  ##显示

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 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: gpt


#         Start          End    Size  Type            Name
 1         2048       206847    100M  Linux LVM      

Command (m for help): wq
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@server ~]# partprobe
 

从gpt再改回msdos
parted /dev/vdb
 mklabel
yes
 quit  


整个过程:

[root@server ~]# parted /dev/vdb
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel                                                          
New disk label type? msdos                                                
Warning: The existing disk label on /dev/vdb will be destroyed and all data on this
disk will be lost. Do you want to continue?
Yes/No? yes
(parted) quit                                                             
Information: You may need to update /etc/fstab.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值