linux fdisk 分区、格式化、挂载!

文件系统的一个使用流程:

存储介质选择---》硬盘分区操作;创建分区来容纳文件系统;创建分区,这个分区可以是整个硬盘也可以是部分硬盘----》创建文件系统(我们在存储数据之前,你必须使用文件系统对其分区进行格式化)----》把它挂载到虚拟目录----》写入配置文件/etc/fstab

硬盘分区的划分标准:

硬盘分区由主分区、扩展分区和逻辑分区组成

在一块硬盘上,如果是MBR分区方式,那么它的主分区最多只能有4个,或者3个主分区和1个扩展分区;在扩展分区上我们可以创建多个逻辑分区

分区编号:主分区1-4 ,逻辑分区5.。。。。。

LINUX规定:逻辑分区必须建立在扩展分区之上,而不是建立在主分区上

分区作用:

  1. 主分区:主要是用来启动操作系统的,它主要放的是操作系统的启动或引导程序,/boot分区最好放在主分区上
  2. 扩展分区不能使用的,它只是做为逻辑分区的容器存在的;我们真正存放数据的是主分区和逻辑分区,大量数据都放在逻辑分区中
  3. 如果你用的是GPT的分区方式,那么它没有限制主分区个数
  4. 注意:从MBR转到GPT分区或者说从GPT转到MBR会导致数据全部丢失

因此我们在对硬盘分区时最好划分主分区连续,比如说:主分区一、主分区二、扩展分区。

此文以fdisk工具为例,对一个硬盘划分。

Command Reference

    *partprobe        #分区之后,让内核更新分区信息,否则系统需要restart后,内核才能识别新的分区

    */proc/partitions 文件夹(目录)也可以用来查看分区信息
fdisk用法:
 fdisk [选项] <磁盘>    更改分区表
 fdisk [选项] -l <磁盘> 列出分区表
 fdisk -s <分区>        给出分区大小(块数)

选项:
 -b <大小>             扇区大小(512、1024、2048或4096)
 -c[=<模式>]           兼容模式:“dos”或“nondos”(默认)
 -h                    打印此帮助文本
 -u[=<单位>]           显示单位:“cylinders”(柱面)或“sectors”(扇区,默认)
 -v                    打印程序版本
 -C <数字>             指定柱面数
 -H <数字>             指定磁头数
 -S <数字>             指定每个磁道的扇区数
 

1、fdisk -l 查看系统上的硬盘,找到需要分区的硬盘后比如说:/dev/sda.

可以看出sda磁盘仍然有很多空间未启动;接下来进行磁盘分区,fdisk /dev/sda

进入该设备。此时出现:

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 注:l是列出分区类型,以供我们设置相应分区的类型;
   m   print this menu 注:m 是列出帮助信息;
   n   add a new partition 注:添加一个分区;
   o   create a new empty DOS partition table 
   p   print the partition table 注:p列出分区表;
   q   quit without saving changes 注:不保存退出;
   s   create a new empty Sun disklabel    
   t   change a partition's system id 注:t 改变分区类型;
   u   change display/entry units 
   v   verify the partition table
   w   write table to disk and exit 注:把分区表写入硬盘并退出;
   x   extra functionality (experts only) 注:扩展应用,专家功能;

具体每个参数的含义,请仔细阅读。常用的就是:d l m p q t w

2、列出当前操作硬盘的分区情况,用p

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000ea964

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux

Command (m for help): n  注:添加分区
Partition type:
   p   primary (1 primary, 0 extended, 3 free)  注:1个主分区,3个空闲分区总共四个主分区
   e   extended
Select (default p): p    注:添加主分区,默认主分区;
Partition number (2-4, default 2): 2 注:添加主分区2,因为有系统已有主分区1;
irst sector (2099200-209715199, default 2099200):  注:直接回车,主分区2的结束位置;默认为2099200,默认就好;
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-209715199, default 209715199): +10G   注:指定分区大小,用+10G来指定大小为10G

Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)  注:2个主分区,2个空闲分区总共四个主分区
   e   extended
Select (default p): e    注:添加扩展分区;
Partition number (3,4, default 3):      注:直接回车,因为主分区已经分了两个了,这个也算主分区,从3开始;
First sector (23070720-209715199, default 23070720):     注:直接回车;
Using default value 23070720
Last sector, +sectors or +size{K,M,G} (23070720-209715199, default 209715199):   注:直接回车,把其余的所有空间都给扩展分区;

Using default value 209715199
Partition 3 of type Extended and of size 89 GiB is set

Command (m for help): p 注:再次打印分区查看分区结果

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000ea964

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    23070719    10485760   83  Linux
/dev/sda3        23070720   209715199    93322240    5  Extended

Command (m for help): n 注:在扩展分区添加逻辑分区
Partition type:
   p   primary (2 primary, 1 extended, 1 free)    注:显示已有2个主分区1个扩展分区1个空闲分区;
   l   logical (numbered from 5)                   注:增加一个逻辑分区;编号从5开始 ;因为主分区有4个;
Select (default p): l 注:添加逻辑分区;
Adding logical partition 5  逻辑分区编号为自增,不能选择
First sector (23072768-209715199, default 23072768): 
Using default value 23072768
Last sector, +sectors or +size{K,M,G} (23072768-209715199, default 209715199): 
Using default value 209715199
Partition 5 of type Linux and of size 89 GiB is set

Command (m for help): p 列出分区表;

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000ea964

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    23070719    10485760   83  Linux
/dev/sda3        23070720   209715199    93322240    5  Extended
/dev/sda5        23072768   209715199    93321216   83  Linux
 

然后我们根据前面所说通过t指令来改变分区类型;

Command (m for help): t  注:通过t来指定分区类型;
Partition number (1-3,5, default 5):  注:要改变哪个分区类型呢?默认为5,其实也就是sda5,直接回车
Hex code (type L to list all codes): L 注:在这里输入L,就可以查看分区类型的id了;

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      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            
1e  Hidden W95 FAT1 80  Old Minix  

Hex code (type L to list all codes): c 注:如果我想让这个分区是W95 FAT32 (LBA)类型的,通过L查看得知 c是表示的是,所以输入了c;

WARNING: If you have created or modified any DOS 6.xpartitions, please see the fdisk manual page for additionalinformation.

Changed type of partition 'Linux' to 'W95 FAT32 (LBA)' 注:系统信息,改变成功;是否是改变了,请用p查看

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000ea964

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    23070719    10485760   83  Linux
/dev/sda3        23070720   209715199    93322240    5  Extended
/dev/sda5        23072768   209715199    93321216    c  W95 FAT32 (LBA)
分区类型已改变

3、通过fdisk的d指令来删除一个分区

Command (m for help): p    注:列出分区情况;

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000ea964

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    23070719    10485760   83  Linux
/dev/sda3        23070720   209715199    93322240    5  Extended
/dev/sda5        23072768   209715199    93321216    c  W95 FAT32 (LBA)

Command (m for help): d     注:执行删除分区指定;
Partition number (1-3,5, default 5):     注:我想删除 sda5 ,直接回车就可以因为默认为5分区 ;

Command (m for help): p 注:再查看一下硬盘分区情况,看是否删除了?

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000ea964

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    23070719    10485760   83  Linux
/dev/sda3        23070720   209715199    93322240    5  Extended
 

Command (m for help):
警告:删除分区时要小心,请看好分区的序号,如果您删除了扩展分区,扩展分区之下的逻辑分区都会删除;所以操作时一定要小心;如果知道自己操作错了,请不要惊慌,用q不保存退出;切记切记!!!!在分区操作错了之时,千万不要输入w保存退出!!!

 

4、通过fdisk的n指令再增加一个分区

Command (m for help): p

ommand (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000ea964

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    23070719    10485760   83  Linux
/dev/sda3        23070720   209715199    93322240    5  Extended
 

Command (m for help): n   
Command action
   p   primary (2 primary, 1 extended, 1 free)              注:显示已有1个主分区2个扩展分区1个空闲分区;
   l   logical (numbered from 5)       注:增加一个逻辑分区;编号从5开始 ;见上文Linux分区划分标准;
Select (default p): p
No free sectors available     注:失败中,为什么失败?
注:我试图增加一个主分区,看来是失败了,为什么失败?因为我们看到主分区+扩展分区把整个磁盘都用光了,看扩展分区的End的值,再看一下 p输出信息中仅剩1扇区; 所以我们只能增加逻辑分区了; 
Command (m for help): n
Partition type:
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)
Select (default p): l   注:在这里输入l,就进入划分逻辑分区阶段了;
Adding logical partition 5  注:逻辑分区自动编号,从扩展分区的标号开始
First sector (23072768-209715199, default 23072768):   注:这个就是分区的Start 值;这里最好直接按回车,如果您输入了一个非默认的数字,会造成空间浪费;
Using default value 23072768
Last sector, +sectors or +size{K,M,G} (23072768-209715199, default 209715199):+50G 注:这个是定义分区大小的,+50G 就是大小为50G ;当然您也可以根据p提示的单位扇区的大小来算,然后来指定 End的数值。根据Units = sectors of 1 * 512 = 512 bytes可以知道1扇区=512字节;但是用+50G这个办法来添加更直观一点。如果您想添加一个10G左右大小的分区,请输入 +10G或+10000M或+10000000K;


5、fdisk 的退出,用q或者 w

其中 q是 不保存退出,w是保存退出

Command (m for help): w
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.

警告:重新读取分区表失败,错误16:内核仍然使用旧表。新表将用于下一次重新启动或运行PARTROPE(8)或KTAXX(8)之后

fdisk -l

Disk /dev/sda: 107.4 GB, 107374182400 bytes, 209715200 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: 0x000ea964

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    23070719    10485760   83  Linux
/dev/sda3        23070720   209715199    93322240    5  Extended
/dev/sda5        23072768   127930367    52428800   83  Linux
/dev/sda6       127932416   209715199    40891392   83  Linux

Disk /dev/mapper/centos-root: 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 /dev/mapper/centos-swap: 4294 MB, 4294967296 bytes, 8388608 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

mount /dev/sda5 /mnt
mount: special device /dev/sda5 does not exist  磁盘不存在!

同步磁盘

reboot  或者  partprobe 
partprobe 
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
mount /dev/sda5 /mnt/
mount: /dev/sda5 写保护,将以只读方式挂载
mount: 未知的文件系统类型“(null)”

所以还需要对新分区进行格式化
 

6、对分区进行格式化,以及加载;
先提示一下;用 mkfs.bfs mkfs.ext2 mkfs.jfs mkfs.msdos mkfs.vfatmkfs.cramfs mkfs.ext3 mkfs.minix mkfs.reiserfs mkfs.xfs 等命令来格式化分区,比如我想格式化 sda5为ext3文件系统,则输入;

[root@moko15 ~]# mkfs -t ext3 -c /dev/sda5   具体参数含义,请man mkfs。
如果我想加载 sda5到目前系统来存取文件,应该有mount 命令,但首先您得建一个挂载目录;比如 /mnt/sda6 ; 
[root@moko15 ~]# mkdir /sda5
[root@moko15 ~]# mount /dev/sda5 /sda5[root@moko15 ~]#  df -Th
文件系统                类型      容量  已用  可用 已用% 挂载点
/dev/mapper/centos-root xfs        10G  4.1G  6.0G   41% /
devtmpfs                devtmpfs  975M     0  975M    0% /dev
tmpfs                   tmpfs     992M     0  992M    0% /dev/shm
tmpfs                   tmpfs     992M   10M  982M    2% /run
tmpfs                   tmpfs     992M     0  992M    0% /sys/fs/cgroup
/dev/sda1               xfs      1014M  201M  814M   20% /boot
tmpfs                   tmpfs     199M     0  199M    0% /run/user/0
/dev/sda5               ext3       40G   49M   38G    1% /sda5
 

  • 44
    点赞
  • 216
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会依次回答你的问题。 1. Linux分区格式化为ext3格式化: 首先,我们需要使用fdisk命令来对磁盘进行分区。具体命令如下: ``` sudo fdisk /dev/sda ``` 其中,`/dev/sda`是你要进行分区的磁盘。接下来,按照提示进行分区操作。 然后,我们需要使用mkfs命令将分区格式化为ext3格式。具体命令如下: ``` sudo mkfs.ext3 /dev/sda1 ``` 其中,`/dev/sda1`是你要格式化分区。如果你要格式化的是整个磁盘,那么应该是`/dev/sda`。 2. Linux fdisk 分区格式化挂载: 这个问题其实涉及到三个子问题,我会依次回答。 a. fdisk 分区: 我们可以使用fdisk命令对磁盘进行分区。具体命令如下: ``` sudo fdisk /dev/sda ``` 其中,`/dev/sda`是你要进行分区的磁盘。接下来,按照提示进行分区操作。 b. 格式化: 我们可以使用mkfs命令将分区格式化为指定的格式。比如,要将分区格式化为ext4格式,具体命令如下: ``` sudo mkfs.ext4 /dev/sda1 ``` 其中,`/dev/sda1`是你要格式化分区。如果你要格式化的是整个磁盘,那么应该是`/dev/sda`。 c. 自动挂载: 我们可以通过编辑`/etc/fstab`文件,来实现在系统启动时自动挂载指定分区。具体操作如下: 1) 找到要挂载分区的UUID,可以使用以下命令查看: ``` sudo blkid ``` 2) 打开`/etc/fstab`文件,添加以下内容: ``` UUID=<分区UUID> <挂载点> <文件系统类型> defaults 0 0 ``` 其中,`<分区UUID>`是你要挂载分区的UUID,`<挂载点>`是你要将分区挂载到的目录,`<文件系统类型>`是分区文件系统类型,比如ext4。 3) 保存文件并退出。 现在,你可以使用以下命令来挂载分区: ``` sudo mount -a ``` 这样,系统就会自动挂载指定分区了。 3. ext3格式化,ext4格式化挂载: 这个问题也涉及到三个子问题,我会依次回答。 a. ext3格式化: 要将分区格式化为ext3格式,可以使用以下命令: ``` sudo mkfs.ext3 /dev/sda1 ``` 其中,`/dev/sda1`是你要格式化分区。如果你要格式化的是整个磁盘,那么应该是`/dev/sda`。 b. ext4格式化: 要将分区格式化为ext4格式,可以使用以下命令: ``` sudo mkfs.ext4 /dev/sda1 ``` 其中,`/dev/sda1`是你要格式化分区。如果你要格式化的是整个磁盘,那么应该是`/dev/sda`。 c. 挂载: 要挂载已经格式化好的分区,可以使用以下命令: ``` sudo mount /dev/sda1 /mnt ``` 其中,`/dev/sda1`是你要挂载分区,`/mnt`是你要将分区挂载到的目录。如果你要挂载的是整个磁盘,那么应该是`/dev/sda`。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值