linux磁盘基础知识_linux(图片源自网络)

一 机械硬盘

 

    柱面cylinder:每个盘片面的同轨道数据为一个柱面,即每个磁盘的每个面同一轨道号的数据称为柱面

    扇区sector:以圆心为角度扩散的区域为扇形,即为扇区

    磁头head:磁臂上读取磁盘的读写头为磁头

二 磁盘表示

    IDE机械硬盘 hd <a...z><1...n>:

    <a...z>为第几块磁盘的顺序

    <1...n>每个磁盘的分区顺序

     SATA,SCSI,SAS,U盘等硬盘 sd<a...z><1...n>:

    <a...z>为第几块磁盘的顺序

    <1...n>每个磁盘的分区顺序

    例:

    有一块IDE硬盘分两个区,一块SATA硬盘分3个区

    在/dev下显示如:

    /dev/hda 

    /dev/hda1

    /dev/hda2

    /dev/sdb

    /dev/sdb1

    /dev/sdb2

    /dev/sdb3

    同样适用于DVD,cdrom,floppy等

三 系统分区

    主要分MBR或者GPT

    MBR(master boot record)传统的X86架构硬件BIOS引导的分区机制,只支持32bit或64bit系统,分区数量有限.

    MBR结构占用硬盘前512字节,按顺序分为:

           1.引导代码占446字节

           2.分区表占64个字节,分为4条16字节

           3.标记是否可引导2字节

    分区限制:

                  主分区最大4个,(扩展分区也属于主分区只能存在一个)因为只有4条分区字节

                  扩展分区最大1个,会占用一个主分区数

                  逻辑分区无限制

    GPT(GUID partition table)兼容更多架构只支持UEFI引导的分区机制,只支持64bit系统,默认支持2TB以上硬盘,

                                         由intel发布的替代BIOS的技术,很多新主板可以切换BIOS为UEFI模式,

     全局唯一标示分区表结构如下:

                  1.表头8字节

                  2.版本4字节

                  3.分区表大小4字节

                  4.分区表头校验4字节

                  5.保留信息4字节

                  6.分区表项参数(含备份表)64字节

                  7.分区串行校验4字节

                  8.保留

    分区表项格式如下:

                  1.分区类型16字节

                  2.分区全局标识(GUID)16字节

                  3.起始LBA 8字节

                  4.结束LBA 8字节

                  5.属性标签 8字节

                  6.分区名 72字节

     分区限制:

                   GPT支持分区数量,仅受操作系统限制

                   GPT有两个分区表,一个主要分区表,一个备份分区表

四 分区工具的使用

    fdisk

    fdisk -l 查看当前硬盘及分区信息

[root@localhost ~]# fdisk -l
Disk /dev/sda: 21.4 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          13      104391   83  Linux
/dev/sda2              14        2610    20860402+  8e  Linux LVM
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdb doesn't contain a valid partition table
[root@localhost ~]#

 fdisk /dev/sdb 进入这块磁盘分区交互界面

[root@localhost ~]# fdisk/dev/sdb
bash: fdisk/dev/sdb: No such file or directory
[root@localhost ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
Command (m for help):
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
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): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
Command (m for help): n
Command action
e   extended
#创建一个扩展分区
p   primary partition (1-4)
#创建一个主分区,分区号1-4
p
Partition number (1-4): 1
First cylinder (1-652, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-652, default 652): +2.5G
Command (m for help): P
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1           3       24066   83  Linux
Command (m for help): n
Command action
e   extended
p   primary partition (1-4)
e
Partition number (1-4): 2
First cylinder (4-652, default 4):
Using default value 4
Last cylinder or +size or +sizeM or +sizeK (4-652, default 652):
Using default value 652
Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1           3       24066   83  Linux
/dev/sdb2               4         652     5213092+   5  Extended
Command (m for help): n
Command action
l   logical (5 or over)
#创建一个逻辑分区,分区号从5开始
p   primary partition (1-4)
l
First cylinder (4-652, default 4):
Using default value 4
Last cylinder or +size or +sizeM or +sizeK (4-652, default 652): +5G
Command (m for help): P
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1           3       24066   83  Linux
/dev/sdb2               4         652     5213092+   5  Extended
/dev/sdb5               4         612     4891761   83  Linux
Command (m for help): n
Command action
l   logical (5 or over)
p   primary partition (1-4)
l
First cylinder (613-652, default 613):
Using default value 613
Last cylinder or +size or +sizeM or +sizeK (613-652, default 652):
Using default value 652
Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1           3       24066   83  Linux
/dev/sdb2               4         652     5213092+   5  Extended
/dev/sdb5               4         612     4891761   83  Linux
/dev/sdb6             613         652      321268+  83  Linux
Command (m for help): t
Partition number (1-6): 1
Hex code (type L to list codes): L
0  Empty           1e  Hidden W95 FAT1 80  Old Minix       bf  Solaris
1  FAT12           24  NEC DOS         81  Minix / old Lin c1  DRDOS/sec (FAT-
2  XENIX root      39  Plan 9          82  Linux swap / So c4  DRDOS/sec (FAT-
3  XENIX usr       3c  PartitionMagic  83  Linux           c6  DRDOS/sec (FAT-
4  FAT16 <32M      40  Venix 80286     84  OS/2 hidden C:  c7  Syrinx
5  Extended        41  PPC PReP Boot   85  Linux extended  da  Non-FS data
6  FAT16           42  SFS             86  NTFS volume set db  CP/M / CTOS / .
7  HPFS/NTFS       4d  QNX4.x          87  NTFS volume set de  Dell Utility
8  AIX             4e  QNX4.x 2nd part 88  Linux plaintext df  BootIt
9  AIX bootable    4f  QNX4.x 3rd part 8e  Linux LVM       e1  DOS access
a  OS/2 Boot Manag 50  OnTrack DM      93  Amoeba          e3  DOS R/O
b  W95 FAT32       51  OnTrack DM6 Aux 94  Amoeba BBT      e4  SpeedStor
c  W95 FAT32 (LBA) 52  CP/M            9f  BSD/OS          eb  BeOS fs
e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a0  IBM Thinkpad hi ee  EFI GPT
f  W95 Ext'd (LBA) 54  OnTrackDM6      a5  FreeBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a6  OpenBSD         f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a7  NeXTSTEP        f1  SpeedStor
12  Compaq diagnost 5c  Priam Edisk     a8  Darwin UFS      f4  SpeedStor
14  Hidden FAT16 <3 61  SpeedStor       a9  NetBSD          f2  DOS secondary
16  Hidden FAT16    63  GNU HURD or Sys ab  Darwin boot     fb  VMware VMFS
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT
Hex code (type L to list codes): 83
#分区后其实默认就是83
Command (m for help): p
Disk /dev/sdb: 5368 MB, 5368709120 bytes
255 heads, 63 sectors/track, 652 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1           3       24066   83  Linux
/dev/sdb2               4         652     5213092+   5  Extended
/dev/sdb5               4         612     4891761   83  Linux
/dev/sdb6             613         652      321268+  83  Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]#

partprobe 使内核更新磁盘信息后就能通过fdisk -l看到磁盘信息了

 

四 创建文件系统

    俗称格式化,没有被格式化的分区为raw

 

    mke2fs -T ext3 /dev/sdb2 创建文件系统 格式为ext3 分区为/dev/sdb1

    -b "blocksize" 指定文件系统块大小(每次读写磁盘的最小大小,一般默认4096)

    -c 创建时检查磁盘坏块

    -L "label" 指定分区卷标

    -j 建立文件系统日志 (ext3和ext4默认带)

[root@localhost mnt]# mke2fs -T ext3 /dev/sdb5
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
306432 inodes, 612470 blocks
30623 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=629145600
19 block groups
32768 blocks per group, 32768 fragments per group
16128 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Writing inode tables: done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost mnt]#

  mkfs.ext3 /dev/sdb1 直接创建sdb1文件系统为ext3

[root@localhost ~]# mkfs.ext3 /dev/sdb1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
245280 inodes, 489974 blocks
24498 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=503316480
15 block groups
32768 blocks per group, 32768 fragments per group
16352 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 29 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]# mke2fs -t ext3 /dev/sdb1
mke2fs 1.39 (29-May-2006)
mke2fs: invalid blocks count - /dev/sdb1

dumpe2fs /dev/sdb1 查看此分区详细信息

[root@localhost ~]# dumpe2fs /dev/sdb1
dumpe2fs 1.39 (29-May-2006)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          27edd72e-c733-40de-8fa2-a280f0985180
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal resize_inode dir_index filetype sparse_super large_file
....................................................

fsck /dev/sdb1 修复文件系统(修复前必须卸载磁盘)

fsck -y /dev/sdb1 不提示直接修复文件系统 

mkdir /mnt/sdb1

mount /dev/sdb1 /mnt/sdb1 挂载磁盘到上面创建的目录(磁盘 挂载点)

 umount /dev/sdb1 卸载分区(通过磁盘)

umount /mnt/sdb1 卸载分区(通过挂载点)

 

 可以通过修改配置文件启动时自动挂载

修改/etc/fstab

在最后添加要挂载的信息

/dev/sdb1<tab>/mnt<tab>ext4<tab>defaults<tab>0 0

目标磁盘          挂载点       文件系统    参数1           参数2