Linux的硬盘分区

    关于硬盘分区,这里先只讨论硬盘分区的方法在linux环境下,其他知识以后也会陆续说到

    首先我们通过 df 命令查看一下硬盘的使用情况,在安装硬盘分区后做比较用

[root@bogon ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              16G  2.2G   13G  15% /
tmpfs                1012M  444K 1012M   1% /dev/shm
/dev/sda1             291M   29M  248M  11% /boot
/dev/sr1              2.8G  2.8G     0 100% /media/RHEL_6.0 i386 Disc 1
/dev/sr0               45M   45M     0 100% /media/CDROM

 

 我们再通过fdisk命令 查看一下硬盘的分区情况

[root@bogon ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0008a5e5

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          39      307200   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              39        2093    16501760   83  Linux
/dev/sda3            2093        2611     4161536   82  Linux swap / Solaris

Disk /dev/sdb: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

Disk /dev/sdb doesn't contain a valid partition table

 

 sda 表示已经完成分区并在使用,sdb表示尚未分区,也就是我们下边要进行的操作对象,dev表示硬盘

我们使用 fdisk 命令来对 的 sdb来进行分区

[root@bogon ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xe3b900aa.
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)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): 

 

 下面介绍一下 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
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit   /*保存退出,分区完成后一定要输入w,否则一切为空,回到解放前*/
   x   extra functionality (experts only)

 我们先创建一个新的分区

Command (m for help): n
Command action
   e   extended  /*代表扩展分区,但扩展分区不可以直接使用,需要分为逻辑分区后方可使用,逻辑分区从5开始*/
   p   primary partition (1-4)   /*代表主分区,硬盘只能有四个主分区,从1-4 扩展分区也算一个主分区,主分区可以直接使用*/

 先进行创建主分区

   p   primary partition (1-4)
p
Partition number (1-4): 1     /*表示盘符编码,1-4任意都可以,我们符合习惯从1开始*/
First cylinder (1-1958, default 1): 1  /*此处表示此分区的开始位置,我们假设a,a属于(1-1958) 可以直接回车,默认为1,我们同样从1开始,符合习惯*/
Last cylinder, +cylinders or +size{K,M,G} (1-1958, default 1958): +5G   /*此处表示此分区的结束位置,我们假设为b b属于 (a+1 - 1958),此硬盘大小则为 b-a  或者更加便捷直接输入我们想要的硬盘大小,前加上 "+" 即可*/

 到此处第一个主分区已经完成,我们可以通过 参数 p 来查看一下

Command (m for help): p

Disk /dev/sdb: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xe3b900aa

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         654     5253223+  83  Linux
开始位置 结束位置 分区大小,单位是byte,小数点向前移动,三位,可以获得大致大小

可以看到,第一个分区已经完成,下边我们来分一个扩展分区,同样 先通过参数 n 创建一个新的分区

Command (m for help): n
Command action
   e   extended 
   p   primary partition (1-4)

此时我们要进行的扩展分区,所以我们输入 参数  e 

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Partition number (1-4): 2
First cylinder (655-1958, default 655): 655   /*因为我们第一个分区占用了 (1- 654) ,所以此时默认从655开始 */
Last cylinder, +cylinders or +size{K,M,G} (655-1958, default 1958): +6G  

此时扩展分区已经分区完成,同样的我们通过 参数 p来查看一下

Command (m for help): p

Disk /dev/sdb: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xe3b900aa

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         654     5253223+  83  Linux
/dev/sdb2             655        1438     6297480    5  Extended /*表示这是一个逻辑分区*/

扩展分区是不能直接使用的,我们必须把扩展分区分成逻辑分区后才可以使用,同样的我们 采用参数 n 创建一个新的分区

Command (m for help): n
Command action
   l   logical (5 or over) /*此时可以发现,此处有Extended 变成了 logical  因为扩展分区只能存在一个,logical表示逻辑分区*/
   p   primary partition (1-4)

我们选择参数 l 分出逻辑分区

l
First cylinder (655-1438, default 655): 655
Last cylinder, +cylinders or +size{K,M,G} (655-1438, default 1438): +3G

此时逻辑分区已经完成 ,同样的我们使用参数 p 来查看一下

Command (m for help): p

Disk /dev/sdb: 16.1 GB, 16106127360 bytes
255 heads, 63 sectors/track, 1958 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xe3b900aa

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         654     5253223+  83  Linux
/dev/sdb2             655        1438     6297480    5  Extended
/dev/sdb5             655        1047     3156741   83  Linux /*逻辑分区从5开始,1-4只能是主分区*/

这是分区已经完成,最后一步,参数 w 保存退出

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

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

硬盘完成分区之后,并不能直接使用,需要进行格式化以及挂载才能使用

格式化 我们采用 mkfs命令,简单介绍一下mkfs命令

    mkfs命令用于在设备上(通常为硬盘)创建Linux文件系统。mkfs本身并不执行建立文件系统的工作,而是去调用相关的程序来执行
    mkfs (选项) (参数)
    
     mkfs:指定建立文件系统时的参数;
      -t<文件系统类型>:指定要建立何种文件系统;
      -v:显示版本信息与详细的使用方法;
      -V:显示简要的使用方法;
      -c:在制做档案系统前,检查该partition是否有坏轨

    

下面我们来进行格式化

[root@bogon ~]# mkfs -t ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
328656 inodes, 1313305 blocks
65665 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1346371584
41 block groups
32768 blocks per group, 32768 fragments per group
8016 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736

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

This filesystem will be automatically checked every 27 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@bogon ~]# mkfs -t ext4 /dev/sdb2
mke2fs 1.41.12 (17-May-2010)
mkfs.ext4: inode_size (128) * inodes_count (0) too big for a
    filesystem with 0 blocks, specify higher inode_ratio (-i)
    or lower inode count (-N).

[root@bogon ~]# mkfs -t ext4 /dev/sdb5
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
197600 inodes, 789185 blocks
39459 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=809500672
25 block groups
32768 blocks per group, 32768 fragments per group
7904 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912

Writing inode tables: done                            
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 25 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@bogon ~]# mkfs -t ext4 /dev/sdb2

格式化完成之后我们来进行硬盘的挂载

[root@bogon ~]# mkdir /tmp1
[root@bogon ~]# mkdir /tmp2
[root@bogon ~]# mount /dev/sdb1 /tmp1
[root@bogon ~]# mount /dev/sdb5 /tmp2

此时挂载完毕,我们来通过 df 命令来查看一下我们是否成功

[root@bogon ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              16G  2.2G   13G  15% /
tmpfs                1012M  444K 1012M   1% /dev/shm
/dev/sda1             291M   29M  248M  11% /boot
/dev/sr1              2.8G  2.8G     0 100% /media/RHEL_6.0 i386 Disc 1
/dev/sr0               45M   45M     0 100% /media/CDROM
/dev/sdb1             5.0G  139M  4.6G   3% /tmp1
/dev/sdb5             3.0G   69M  2.8G   3% /tmp2

可以看到,我们已经完成了硬盘的分区以及格式化和挂载,但是由于linux的特殊性,若我们不把此分区写入配置文件,我们下次重启是,此分区是消失的

下面我们通过vim 将其写入配置文件

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


/dev/sdb1 /part1 ext4 defaults 0 0
/dev/sdb5 /part1 ext4 defaults 0 0
# # /etc/fstab # Created by anaconda on Wed Mar 7 04:42:44 2018 # # 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 # UUID=93de9377-f280-4fad-959e-bfc88b5ffeff / ext4 defaults 1 1 UUID=d303ecd7-a97e-4dbe-a543-01b8db0362cc /boot ext4 defaults 1 2 UUID=5cb80a30-6c26-47d2-b82d-cef4de688119 swap swap defaults 0 0 tmpfs /dev/shm tmpfs defaults 0 0 devpts /dev/pts devpts gid=5,mode=620 0 0 sysfs /sys sysfs defaults 0 0 proc /proc proc defaults 0 0 ~ ~ ~ ~ ~ ~ ~ "/etc/fstab" 17L, 842C 3,1 All

 此时代表完成,输入wq保存退出即可

到此关于硬盘分区的操作到此结束,其他方面自己学习之后会再进行分享,谢谢大家

文中如有错误不足,希望大家多多包涵指正

posted @ 2018-05-07 15:35  前鹿  阅读(425)  评论(0编辑  收藏  举报