Linux磁盘分区及其常⽤命令
⼀、磁盘分区命名⽅式
在Linux中,每⼀个硬件设备都映射到⼀个系统的⽂件,包括硬盘、光驱等IDE或SCSI设备。Linux把各种IDE设备分配了⼀个由hd前缀组成的⽂件。⽽各种SCSI设备,则被分配了⼀个由sd前缀组成的⽂件,编号⽅法为拉丁字母表顺序。例如,第⼀个IDE设备(如IDE硬盘或IDE光驱),Linux定义为hda;第⼆个IDE设备定义为hdb;下⾯依次类推。SCSI设备就是sda、sdb、sdc等。(USB磁盘通常会被识别诶SCSI设备,因此其设备名可能是sda)。
在Linux中规定,每⼀个磁盘设备最多能有4个主分区(其中包括扩展分区)。任何⼀个扩展分区都要占⽤⼀个主分区号码。在⼀个硬盘中,主分区和扩展分区⼀共最多是4个。编号顺序为阿拉伯数字顺序。
需要注意的是,主分区按1234编号,扩展分区中的逻辑分区,编号直接从5开始,⽆论是否有2号或3号主分区。对于第⼀个IDE硬盘的第⼀主分区,则编号为hda1,⽽第⼆个IDE硬盘的第⼀个逻辑分区编号应为hdb5。
常见的Linux磁盘命名的规则维hdXY(或sdXY),其中,X为⼩写拉丁字母,Y为阿拉伯数字。个别系统可能命名有差异。
⼆、常⽤磁盘管理命令。
1、挂载磁盘分区——mount
要使⽤磁盘分区,就需要挂载该分区。挂载时需要指定需要挂载的设备和挂载⽬录(该⽬录也成为挂载d点)
常⽤的命令格式如下
mount -t type device dir
选项 -t 的参数type为⽂件系统格式(ext4,vfat,ntfs等;)
device为设备名称(如:"/dev/hda1" "/dev/sdb1")
dir为挂载⽬录,成功挂载后,就可以通过访问该⽬录以访问该分区内的⽂件(如:"/mnt/windows_c" "/mnt/cdrom")只要是未被使⽤的空⽬录都可⽤于挂载分区
-V:显⽰程序版本;
-l:显⽰已加载的⽂件系统列表;
-h:显⽰帮助信息并退出;
-v:冗长模式,输出指令执⾏的详细信息;
-n:加载没有写⼊⽂件“/etc/mtab”中的⽂件系统;
-r:将⽂件系统加载为只读模式;
-a:加载⽂件“/etc/fstab”中描述的所有⽂件系统。
2、卸载磁盘分区——umount
要移除磁盘,⾸先需要卸载该分区。常⽤命令格式如下
umount [device|dir]
卸载时只需要⼀个参数,可以是设备名称,也可以是挂载点
-a:卸除/etc/mtab中记录的所有⽂件系统;
-h:显⽰帮助;
-
n:卸除时不要将信息存⼊/etc/mtab⽂件中;
-r:若⽆法成功卸除,则尝试以只读的⽅式重新挂⼊⽂件系统;
-t<⽂件系统类型>:仅卸除选项中所指定的⽂件系统;
-v:执⾏时显⽰详细的信息;
-V:显⽰版本信息。
3、查看磁盘分区信息
1)、查看磁盘的挂载情况——mount                查看磁盘的挂载情况⽅法:还接输⼊不带参数的mount命令;
2)、查看磁盘的分区情况——fdisk                查看磁盘的分区情况使⽤命令“fdisk -l”;
3)、查看磁盘的使⽤情况——df                        查看磁盘的使⽤情况,直接使⽤df;
三,磁盘分区
1, linux磁盘分区主要分为基本分区(primary partion)和扩充分区(extension partion)两种,基本分区和扩充分区的数⽬之和不能⼤于四个。且基本分区可以马上被使⽤但不能再分区。扩充分区必须再进⾏分区后才能使⽤,也就是说它必须还要进⾏⼆次分区。在 Linux 中,每⼀个硬件设备都映射到⼀个系统的⽂件,对于硬盘、光驱等 IDE 或 SCSI 设备也不例外。Linux把各种 IDE 设备分配了⼀个由 hd 前缀组成的⽂件;⽽对于各种 SCSI 设备,则分配了⼀个由 sd 前缀组成的⽂件。
2,磁盘分区⼯具fdisk命令
⾸先选择要进⾏操作的磁盘:
[root@localhost ~]# fdisk /dev/sdb
输⼊m列出可以执⾏的命令:
(m for ): m
Command action
a  toggle a bootable flag
b  edit bsd disklabel
c  toggle the dos compatibility flag
d  delet
e 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
u  change display/entry units
v  verify the partition table
table to disk and
x  extra functionality (experts only)
输⼊p列出磁盘⽬前的分区情况:
Command (m for help): p
Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start        End      Blocks  Id  System
/dev/sdb1              1          1        8001  8e  Linux LVM
/
dev/sdb2              2          26      200812+  83  Linux
输⼊d然后选择分区,删除现有分区:
Command (m for help): d
Partition number (1-4): 1
Command (m for help): d
Selected partition 2
查看分区情况,确认分区已经删除:
Command (m for help): print
Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start        End      Blocks  Id  System
Command (m for help):
输⼊n建⽴新的磁盘分区,⾸先建⽴两个主磁盘分区:
Command (m for help): n
Command action
e  extended
p  primary partition (1-4)
p    //建⽴主分区
Partition number (1-4): 1  //分区号
First cylinder (1-391, default 1):  //分区起始位置
Using default value 1
cylinder or +size or +sizeM or +sizeK (1-391, default 391): 100  //分区结束位置,单位为扇区Command (m for help): n  //再建⽴⼀个分区
Command action
e  extended
p  primary partition (1-4)
p
Partition number (1-4): 2  //分区号为2
First cylinder (101-391, default 101):
Using default value 101
Last cylinder or +size or +sizeM or +sizeK (101-391, default 391): +200M  //分区结束位置,单位为M 确认分区建⽴成功:
Command (m for help): p
Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start        End      Blocks  Id  System
/dev/sdb1              1        100      803218+  83  Linux
/dev/sdb2            101        125      200812+  83  Linux
再建⽴⼀个逻辑分区:
Command (m for help): n
Command action
e  extended
p  primary partition (1-4)
e  //选择扩展分区
Partition number (1-4): 3
First cylinder (126-391, default 126):
Using default value 126
Last cylinder or +size or +sizeM or +sizeK (126-391, default 391):
Using default value 391
确认扩展分区建⽴成功:
Command (m for help): p
Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start        End      Blocks  Id  System
/dev/sdb1              1        100      803218+  83  Linux
/dev/sdb2            101        125      200812+  83  Linux
/dev/sdb3            126        391    2136645    5  Extended
在扩展分区上建⽴两个逻辑分区:
Command (m for help): n
Command action
l  logical (5 or over)
p  primary partition (1-4)
l //选择逻辑分区
First cylinder (126-391, default 126):
Using default value 126
Last cylinder or +size or +sizeM or +sizeK (126-391, default 391): +400M
Command (m for help): n
Command action
l  logical (5 or over)
p  primary partition (1-4)
l
linux磁盘管理First cylinder (176-391, default 176):
Using default value 176
Last cylinder or +size or +sizeM or +sizeK (176-391, default 391):
Using default value 391
确认逻辑分区建⽴成功:
Command (m for help): p
Disk /dev/sdb: 3221 MB, 3221225472 bytes
255 heads, 63 sectors/track, 391 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot      Start        End      Blocks  Id  System
/dev/sdb1              1        100      803218+  83  Linux
/dev/sdb2            101        125      200812+  83  Linux
/dev/sdb3            126        391    2136645    5  Extended
/dev/sdb5            126        175      401593+  83  Linux
/dev/sdb6            176        391    1734988+  83  Linux
Command (m for help):
从上⾯的结果我们可以看到,在硬盘sdb我们建⽴了2个主分区(sdb1,sdb2),1个扩展分区(sdb3),2个逻辑分区(sdb5,sdb6)
注意:主分区和扩展分区的磁盘号位1-4,也就是说最多有4个主分区或者扩展分区,逻辑分区开始的磁盘号为5,因此在这个实验中试没有sdb4的。
最后对分区操作进⾏保存:
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re- partition table.
Syncing disks.
建⽴好分区之后我们还需要对分区进⾏格式化才能在系统中使⽤磁盘。
四,磁盘分区的格式化
1,格式化:指将分区格式化成不同的⽂件系统。那什么是⽂件系统呢?⽂件系统:指操作系统⽤于明确存储设备或分区上的⽂件的⽅法和数据结构:即在存储设备上组织⽂件的⽅法。
2,格式化磁盘的主要命令是mkfs,其常⽤的命令格式为:mkfs -t  type device [block_size]
选项 t 的参数type为⽂件系统格式(如ext4,vfat,ntfs等),参数device为设备名称(如“/dev/hda1”),参数block_size为block⼤⼩,为可选项;
格式化交换分区的命令略有不同,不是mkfs,⽽是mkswap。