Linux 分区

一、存储单位

bit     电脑记忆体中最小的单位,在二进位电脑系统中,每一bit 可以代表0 或 1 的数位讯号。 

Byte   一个Byte由8 bits 所组成,可代表一个字元(A~Z)、数字(0~9)、或符号(,.?!%&+-*/),是记忆体储存资料的基本单位,至於每个中文字则须要两Bytes。当记忆体容量过大时,位元组这个单位就不够用,因此就有千位元组的单位KB出现,以下乃个记忆体计算单位之间的相关性:

1 Byte = 8 Bits

1 KB = 1024 Bytes

1 MB = 1024 KB

1 GB = 1024 MB

二、物理结构

磁盘由盘片,机械手臂,磁头,主轴马达组成,而数据的写入主要是在盘片上面,盘片上又细分为扇区与柱面两种单位,扇区每个为512bytes那么大  ,

其中,磁盘的第一个扇区特别重要,因为磁盘的第一个扇区记录了两个重要的信息:

1、主引导分区(BMR):可以安装引导加载程序的地方,有446bytes。

2、分区表:记录整块硬盘分区的状态,有66bytes。

图1

磁道:图1中硬盘被一圈圈分成18等分的同心圆,这些同心圆就是磁道,但打开硬盘,用户不能看到这些,它实际上是被磁头磁化的同心圆.这些磁道是有间隔的,因为磁化单元太近会产生干扰。

扇区sector(扇区),每个扇区储存512字节;操作系统会一次性连续读取多个扇区,即一次性读取多个扇区称为一个block(块)

柱面:假如一个硬盘只有图1中的3个磁盘片,每一片中的磁道数是相等的.从外圈开始,这些磁道被分成了0磁道、1磁道、2磁道...具有相同磁道编号的同心圆组成面就称作柱面.为了便于理解,柱面可以看作没有底的铁 桶.从图1可以看出,柱面数就是磁盘上的磁道数.柱面是硬盘分区的最小单位.因此,一个硬盘的容量=柱面*磁头*扇区*512。

Windows(簇):扇区是硬盘数据存储的最小单位,但操作系统无法对数目众多的扇区进行寻址,所以操作系统就将相邻的扇区组合在一起,形成一个簇,然后再对簇进行管理.每个簇可以包括2、4、8、16、32、64个扇区

Linux (块) 文件存取的最小单位:block(块),由多个扇区组成;block的大小常见的有1KB、2KB、4KB,在linux中常见设置为4KB,即连续8个扇区组成一个block;/boot分区的block一般为1KB

[root@fileshare1 home]# stat /boot/ | grep "IO Block"
  Size: 4096          Blocks: 8          IO Block: 4096   directory
[root@fileshare1 home]# xfs_info /dev/sda1
meta-data=/dev/sda1              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

ext3与ext4格式化文件系统:

       dumpe2fs /dev/sda1 | grep "Block size"

       tune2fs -l /dev/sda1 | grep "Block size"

 xfs格式化文件系统:

       xfs_info /dev/sda1

每个block只能存放一个文件,如果文件的大小比block大,会申请更多的block;如果文件的大小比block小,仍会占用一个block,剩余的空间会浪费

  1. 例:有1万个文件,大小为10B,block为4KB
  2. 理论上占用空间大小:10000 * 10B=97.656MB
  3. 实际上占用空间大小:10000 * 4KB = 40GB

三、分区

[root@demo1 ~]# fdisk -l

Disk /dev/sda: 231.9 GB, 231928233984 bytes, 452984832 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: 0x000d2f4d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   450641919   224271360   8e  Linux LVM

Disk /dev/mapper/centos-root: 225.5 GB, 225485783040 bytes, 440401920 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 /dev/mapper/centos-swap: 4160 MB, 4160749568 bytes, 8126464 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

 磁盘容量的计算

磁盘的结构一般包括磁道、盘面、扇区、碰头等

一个磁道的大小=512 字节*扇区数

一个盘面的大小=磁道的大小*磁道数

一个磁盘的大小=盘面大小*磁头数

因此,一个磁盘的容量=512 字节*扇区数*磁道数*磁头数

磁盘分区

所有磁盘的分区信息都是存储在分区表中,Linux系统仅支持4个分区表信息(主分区+扩展分区),一个分区表的大小在64bytes

Linux一般分为三个分区分别是:boot分区、swap分区、/根分区

Linux的分区编号:主分区1-4,逻辑分区从5开始计算

实际生产环境分区要求

1、 最少要有/和swap两个分区

2、 swap(虚拟内存)=1.5*物理内存大小,一般大小于或等于16G的物理内存的服务器,swap分区一般都直接设置为16G大小

3、 建议设置/boot分区,Linux引导分区,如内核文件等,一般所有文件一共才几十M的大小,因此这个分区就设置为100-200M即可

例子:增加116G的硬盘

[root@demo1 ~]# fdisk -l

Disk /dev/sda: 231.9 GB, 231928233984 bytes, 452984832 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: 0x000d2f4d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   450641919   224271360   8e  Linux LVM

Disk /dev/sdb: 124.6 GB, 124554051584 bytes, 243269632 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 /dev/mapper/centos-root: 225.5 GB, 225485783040 bytes, 440401920 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 /dev/mapper/centos-swap: 4160 MB, 4160749568 bytes, 8126464 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

fdisk  /dev/sdb

  • n - 创建分区
  • p - 打印分区表
  • d - 删除一个分区
  • q - 不保存更改退出
  • w - 保存更改并退出
[root@demo1 ~]# lsblk 
NAME            MAJ:MIN RM   SIZE RO TYPE MOUNTPOINT
fd0               2:0    1     4K  0 disk 
sda               8:0    0   216G  0 disk 
├─sda1            8:1    0     1G  0 part /boot
└─sda2            8:2    0 213.9G  0 part 
  ├─centos-root 253:0    0   210G  0 lvm  /
  └─centos-swap 253:1    0   3.9G  0 lvm  [SWAP]
sdb               8:16   0   116G  0 disk 
sr0              11:0    1  1024M  0 rom

增加分区


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

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): p
Partition number (1-4, default 1): 
First sector (2048-243269631, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-243269631, default 243269631): 
Using default value 243269631
Partition 1 of type Linux and of size 116 GiB is set

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

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

检查

[root@demo1 ~]# fdisk -l

Disk /dev/sda: 231.9 GB, 231928233984 bytes, 452984832 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: 0x000d2f4d

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200   450641919   224271360   8e  Linux LVM

Disk /dev/sdb: 124.6 GB, 124554051584 bytes, 243269632 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: 0xfe6c5dcc

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048   243269631   121633792   83  Linux

Disk /dev/mapper/centos-root: 225.5 GB, 225485783040 bytes, 440401920 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 /dev/mapper/centos-swap: 4160 MB, 4160749568 bytes, 8126464 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@demo1 ~]# mkfs.
mkfs.btrfs   mkfs.cramfs  mkfs.ext2    mkfs.ext3    mkfs.ext4    mkfs.minix   mkfs.xfs     

[root@demo1 ~]# mkfs.ext4 /dev/sdb
mke2fs 1.42.9 (28-Dec-2013)
/dev/sdb is entire device, not just one partition!
Proceed anyway? (y,n) y
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
7602176 inodes, 30408704 blocks
1520435 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2178940928
928 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
	4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done   

挂载分区

[root@demo1 ~]# mkdir /data
[root@demo1 ~]# mount /dev/sdb /data


在 /etc/fstab 文件中添加条目以便永久启动时自动挂载。


#
# /etc/fstab
# Created by anaconda on Wed Dec 15 14:07:05 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=4faa4adf-161e-4a55-95ef-7eb757fb826b /boot                   xfs     defaults        0 0
/dev/mapper/centos-swap swap                    swap    defaults        0 0
/dev/sdb  /data                        ext4    defaults        0 0

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值