linux系统中的磁盘管理

计算机中存放信息的主要的存储设备就是硬盘,但是硬盘不能直接使用,必须对硬盘进行分割,分割成的一块一块的硬盘区域就是磁盘分区。在传统的磁盘管理中,将一个硬盘分为两大类分区:主分区和扩展分区。由于64位的主分区列表只能记载四个分区的数据,所以最多只能有四个主分区。为了不将存储空间浪费掉,将一个分区设置为扩展分区,再将扩展分区作为一个容器划分为4个逻辑分区。这样就可以高效的利用存储空间!!!

本地存储设备

fdisk -l                     真实存在的设备


 

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


 

blkid                        系统可使用的设备


 

df                             系统正在挂载的设备

 

设备的挂载和卸载

1.设备名称
均为/dev/xdx  格式 例如:/dev/hdo /dev/hd1 /dev/sda /dev/sdb

/dev/vda            虚拟磁盘
/dev/sro             光驱
/dev/mapper/*  虚拟设备

命名规则:

第一位“s“ #表示磁盘的类型,s=sata
第二位“d“ #disk磁盘
第三位“a“ #第一块磁盘
第四位“1“ #第一块分区

2.设备的挂载

当我把U盘插入电脑,这块U盘就是该系统可以识别的第二块设备,如下图的sdb:

当U盘插入电脑,系统会自动将U盘挂载

将U盘卸载:

挂载在指定目录:

将U盘卸载:

umount   /gq

卸载时如果有以下报错:

方法1:

方法2:

正常的U盘默认位读写挂载,可以用mount命令查看挂载信息

-o指定只读挂载设备:

因为是只读挂载,所以不能创建文件:

重新恢复为读写挂载:

或者使用以下命名,不需要卸载就可以重新挂载:

446=512-446=66=64+2
64==> 主分区表 mpt
446==> mbr
2==> 55aa

分区步骤

[root@localhost ~]# fdisk -l                  查看我的设备

Disk /dev/vda: 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: 0x00013f3e

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux

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


[root@localhost ~]# fdisk /dev/vdb               进行分区
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 0x34986982.

Command (m for help): n                             <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-20971519, default 2048):   默认
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +100M     分区大小
Partition 1 of type Linux and of size 100 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: 0x34986982

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux

Command (m for help): wq                退出并保存
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

保存后我们的分区表需要重新读取
partprobe

[root@localhost ~]# fdisk -l          

Disk /dev/vda: 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: 0x00013f3e

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux

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: 0x34986982

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux

[root@localhost ~]# mkfs.xfs /dev/vdb1     (给设备安装文件系统
meta-data=/dev/vdb1              isize=256    agcount=4, agsize=6400 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=25600, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0

[root@localhost ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
/dev/vdb1: UUID="b42ae526-1ff9-425e-999c-a1f180f4d97f" TYPE="xfs"   ==>  现在这块设备挂载后就可以使用了

[root@localhost mnt]# mount /dev/vdb1 /mnt  ==> 挂载
[root@localhost mnt]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3160008   7313892  31% /
devtmpfs          469344       0    469344   0% /dev
tmpfs             484932      84    484848   1% /dev/shm
tmpfs             484932   12764    472168   3% /run
tmpfs             484932       0    484932   0% /sys/fs/cgroup
/dev/vdb1          98988    5280     93708   6% /mnt

现在这块设备就可以使用了!!!

重复vdb1分区步骤,继续分区,最多有4块主分区,第4块分区我们设置为扩展分区
将剩余空间全部给它。

划分扩展分区


在扩展分区里,可以进行逻辑分区


保存后我们的分区表需要重新读取
partprobe

这样我的设备在安装好文件系统之后,全部就可以使用了!!!

永久挂载
 vim /etc/fstab

编辑内容:

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

给设备安装文件系统
ext3 rhel5及之前版本 最多支持32TB的文件和2T文件,实际2TB文件系统,16G文件
ext4 rhel6                   1EB         16TB
xfs  rhel7                   18EB        9EB        7G/S           4G/S

创建swap分区

[root@localhost ~]# fdisk  /dev/vdb
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): t   #修改vdb6的标签为swap
Partition number (1-5, default 5): 5
Hex code (type L to list all codes): 82  #swap的标签,或者L列出所有标签
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): wq
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@localhost ~]# partprobe

初始化文件系统为swap:

[root@localhost ~]# mkswap /dev/vdb5

将vdb5加入到swap分区中:

[root@localhost ~]# swapon -a /dev/vdb5
[root@localhost ~]# swapon -s	#查看swap分区

将其写入文件中,使swap中添加的vdb6永久有效:

[root@localhost ~]# vim /etc/fstab 

使文件中的策略立即生效:

要删除设置的swap分区,必须先关设备再删除设备:
 

[root@localhost ~]# swapoff /dev/vdb5   删除设备
[root@localhost ~]# vim /etc/fstab      删除文件中对/dev/vdb5这块设备的激活
[root@localhost ~]# fdisk  /dev/vdb
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): d                  删除
Partition number (1-5, default 5): 5     vdb5
Partition 5 is deleted

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: 0x03301795

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448    20971519    10177536    5  Extended

Command (m for help): wq                 退出并保存
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@localhost ~]# partprobe             重读分区表
[root@localhost ~]# blkid                 查看
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs"
/dev/vdb1: UUID="56e222b2-a3f7-4208-aedd-3e398e99aca0" TYPE="xfs"


/dev/vdb5这块设备已删除

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值