Linux学习之 fdisk分区

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zxllynu/article/details/84933622

1、添加新硬盘用于练习分区

操作注意:

1)先给Linux虚拟机断电

2)虚拟机设置选项,选择添加硬盘,完成后,重启,

3)查询当前配置

[root@localhost ~]# 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: 0x000e9453

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        2611    20458496   8e  Linux LVM

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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/mapper/VolGroup-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 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/mapper/VolGroup-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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

3、使用fdisk 命令分区

[root@localhost ~]# fdisk  /dev/sdb

注意fdisk交互指令说明   

fdisk交互指令说明
命令 说明
a 设置可引导标记
b 编辑bsd磁盘标签
c 设置DOS操作系统兼容标记
d 删除一个分区
l 显示已知的文件系统类型。82为Linux swap分区,83为Linux分区
m 显示帮助菜单
n 新建分区
o 建立空白DOS分区表
p 显示分区列表
q 不保存退出
s 新建空白SUN磁盘标签
t 改变一个分区的系统ID
u 改变显示记录单位
v 验证分区表
w 保存退出
x 附加功能(仅专家)
[root@localhost ~]# 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 0x4aac79af.
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): 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
   x   extra functionality (experts only)

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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: 0x4aac79af

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): 

如下,我们决定采取如下分区策略要求:

     a、一个主分区,2G空间;

    b、 一个扩展分区,剩余所有空间;

     c、在扩展分区中,分配第一个逻辑分区,分2G空间;

分区步骤如下所示:

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
   x   extra functionality (experts only)

#查看当前分区状态
Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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: 0x4aac79af

   Device Boot      Start         End      Blocks   Id  System

#查看当前分区号列表
Command (m for help): l

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

#创建一个分区类型
Command (m for help): n
#提示选择分区类型:主分区还是扩展分区
Command action
   e   extended
   p   primary partition (1-4)

#根据提示输入p,表示创建一个主分区
p

#该主分区的编号,建议按照顺序来
Partition number (1-4): 1

#表示把该硬盘分成1到1305块,从哪一个数据块开始,默认从1开始,此处建议从1开始
First cylinder (1-1305, default 1): 

#提示表示第一个主分区建立完成
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +2G   

#继续输命令,此处继续创建一个分区
Command (m for help): n    
Command action
   e   extended
   p   primary partition (1-4)

#根据提示,选择创建一个扩展分区
e

#因为主分区和扩展分区一共四个,已经创建了一个主分区,此处就依次序,选2
Partition number (1-4): 2

#表示目前扩展分区的可选空间从263开始,默认为263,选择默认(不建议断开设置)
First cylinder (263-1305, default 263): 

#提示表示一个扩展分区创建完成
Using default value 263
Last cylinder, +cylinders or +size{K,M,G} (263-1305, default 1305): 
Using default value 1305

#查询下当前分区状态
Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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: 0x4aac79af

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         262     2104483+  83  Linux
/dev/sdb2             263        1305     8377897+   5  Extended

#如上,符合我们的创建目标

#在此,在扩展分区中,试着创建一个逻辑分区(因为扩展分区内无法写人文件,但可以在#扩展分区中创建逻辑分区来实现分区写入数据的目的)
Command (m for help): n

#提示要创建一个逻辑分区还是主分区
Command action
   l   logical (5 or over)
   p   primary partition (1-4)

#选择l表示要创建一个逻辑分区
l

#如下,同上,选择默认
First cylinder (263-1305, default 263): 

#提示逻辑分区创建完成
Using default value 263
Last cylinder, +cylinders or +size{K,M,G} (263-1305, default 1305): +2G

#查询当前磁盘分区状况
Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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: 0x4aac79af

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         262     2104483+  83  Linux
/dev/sdb2             263        1305     8377897+   5  Extended
/dev/sdb5             263         524     2104483+  83  Linux

#注意,只有保存,更改才有效果
Command (m for help): w

#如下提示表示修改并保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# 

4、重新读取分区表信息

[root@localhost ~] # partprobe

[root@localhost ~]# partprobe
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda (设备或资源忙).  As a result, it may not reflect all of your changes until after reboot.
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
Error: 无法让分区超出磁盘!
[root@localhost ~]# 

注意:如果上方保存退出时,正常,表示一切成功,如果出现错误,因为分区表被占用(比如分区完成,保存了两次),提示要重启系统,这时,可以通过该命令,代替重启系统。如上虽然有警告,但属于正常提示,分区正常完成,只是光盘等不存在。

5、格式化分区

[root@localhost ~ ]# mkfs -t ext4  /dev/sdb1

示例:

在完成上方后,通过fdisk -l  命令查询下当前分区格式化

[root@localhost ~]# 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: 0x000e9453

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        2611    20458496   8e  Linux LVM

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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: 0x4aac79af

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         262     2104483+  83  Linux
/dev/sdb2             263        1305     8377897+   5  Extended
/dev/sdb5             263         524     2104483+  83  Linux

Disk /dev/mapper/VolGroup-lv_root: 18.8 GB, 18798870528 bytes
255 heads, 63 sectors/track, 2285 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/mapper/VolGroup-lv_swap: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 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

[root@localhost ~]# 

注意如上,sdb2 是扩展分区,不能格式化,也不能写入数据!!!

现在格式化sdb1  和sdb5

[root@localhost ~]# mkfs -t ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131648 inodes, 526120 blocks
26306 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=541065216
17 block groups
32768 blocks per group, 32768 fragments per group
7744 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

正在写入inode表: 完成                            
Creating journal (16384 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 32 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]# mkfs -t ext4 /dev/sdb5
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
131648 inodes, 526120 blocks
26306 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=541065216
17 block groups
32768 blocks per group, 32768 fragments per group
7744 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

正在写入inode表: 完成                            
Creating journal (16384 blocks): 完成
Writing superblocks and filesystem accounting information: 完成

This filesystem will be automatically checked every 34 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]# 

如上,没有报错,表示格式化完成

6、建立挂载点并挂载

[root@localhost ~]#mkdir /disk1

[root@localhost ~]#mount  /dev/sdb1   /disk1/

[root@localhost ~]# mkdir /disk1
[root@localhost ~]# mkdir /disk5
[root@localhost ~]# mount /dev/sdb1 /disk1
[root@localhost ~]# mount /dev/sdb5 /disk5
[root@localhost ~]# mount
/dev/mapper/VolGroup-lv_root on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
gvfs-fuse-daemon on /root/.gvfs type fuse.gvfs-fuse-daemon (rw,nosuid,nodev)
/dev/sr0 on /media/Ubuntu 14.04.5 LTS amd64 type iso9660 (ro,nosuid,nodev,uhelper=udisks,uid=0,gid=0,iocharset=utf8,mode=0400,dmode=0500)
/dev/sdb1 on /disk1 type ext4 (rw)
/dev/sdb5 on /disk5 type ext4 (rw)
[root@localhost ~]# df
Filesystem           1K-blocks    Used Available Use% Mounted on
/dev/mapper/VolGroup-lv_root
                      17938864 8059304   8961648  48% /
tmpfs                   953092      76    953016   1% /dev/shm
/dev/sda1               487652   41364    420688   9% /boot
/dev/sr0               1078176 1078176         0 100% /media/Ubuntu 14.04.5 LTS amd64
/dev/sdb1              2005848    3096   1897528   1% /disk1
/dev/sdb5              2005848    3096   1897528   1% /disk5

如上,挂载完成并通过检查

但是,注意如上方法,只能临时生效,每次重启系统需要重新挂载,那如何长久生效呢?

二、fdisk分区——自动挂载与fstab文件修复

1、自动挂载(一般比如硬盘、boot  mnt分区等等):

 实质就是在/etc/fstab文件中完成自动挂载配置:

》第一字段:分区设备文件名或UUID(硬盘通用唯一识别码)

》第二字段:挂载点

》第三字段:文件系统名称

》第四字段:挂载参数

》第五字段:指定分区是否被dump备份,0代表不备份,1代表每天备份,2代表不定期备份,备份就是分区自动备份内容到lost-found 文件;

》第六字段:指定分区是否被fsck检测,0代表不检测,其他数字代表检测的优先级,那么当然1的优先级比2高,系统会先检测根目录,个人创建的分区,

一般应该为2

注意:该文件一旦出错,系统可能无法启动,则需要fsck自动修复

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


#
# /etc/fstab
# Created by anaconda on Sun Nov 25 22:51:34 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
#
/dev/mapper/VolGroup-lv_root /                       ext4    defaults        1 1
UUID=ae089b3b-ead9-401d-b17f-80d4575f207f /boot                   ext4    defaults        1 2
/dev/mapper/VolGroup-lv_swap 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" 15L, 779C                                                1,0-

1)如何查看分区的UUID?

使用如下命令查看:

[root@localhost ~]# dumpe2fs  -h  设备文件名            (-h表示只查看超级块儿信息) 

示例:如下,里面会有UUID选项和值

[root@localhost ~]# dumpe2fs -h /dev/sdb1
dumpe2fs 1.41.12 (17-May-2010)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          042f7bd6-d557-4137-b881-02b0b0cb14d6
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
Filesystem flags:         signed_directory_hash 
Default mount options:    (none)
Filesystem state:         clean
Errors behavior:          Continue
Filesystem OS type:       Linux
Inode count:              131648
Block count:              526120
Reserved block count:     26306
Free blocks:              500688
Free inodes:              131637
First block:              0
Block size:               4096
Fragment size:            4096
Reserved GDT blocks:      128
Blocks per group:         32768
Fragments per group:      32768
Inodes per group:         7744
Inode blocks per group:   484
Flex block group size:    16
Filesystem created:       Mon Dec 10 08:02:59 2018
Last mount time:          Mon Dec 10 08:08:40 2018
Last write time:          Mon Dec 10 08:08:40 2018
Mount count:              1
Maximum mount count:      32
Last checked:             Mon Dec 10 08:02:59 2018
Check interval:           15552000 (6 months)
Next check after:         Sat Jun  8 08:02:59 2019
Lifetime writes:          97 MB
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
First inode:              11
Inode size:	          256
Required extra isize:     28
Desired extra isize:      28
Journal inode:            8
Default directory hash:   half_md4
Directory Hash Seed:      9f7752db-881a-4005-9985-1cf59fd4c88b
Journal backup:           inode blocks
Journal features:         (none)
日志大小:             64M
Journal length:           16384
Journal sequence:         0x00000001
Journal start:            0

[root@localhost ~]# 

2、分区自动挂载

[root@localhost ~] #vi /etc/fstab

省略部分输出

/dev/sdb1               /disk1                  ext4    defaults        1 2

写这个文件时特别注意,千万别写错

所以,不要先重启,应该先测试是否有问题,方法:

mount -a

如果回车无错误提示,表示修改正确

[root@localhost ~]# mount -a
[root@localhost ~]# 
如上,表示正确

故意出错修改示例:

  1 2
/dev/mapper/VolGroup-lv_swap 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
/dev/sdb                /disk1                  ext4    defaults        1 2
/dev/sdb5               /disk5                  ext4    defaults        1 2
~                                                                          

如上,故意将disk1对应的设备文件名写错,然后保存

之后重启虚拟机,可以通过命令 reboot,查看虚拟机

提示输入root密码,根据提示进入,发现无法修改,但有提示错误在哪一行

3、/etc/fstab 文件修复

根据如上提示,我们可以通过如下方式进行

重新挂载根分区,赋予读写权限

[root@localhost ~] mount -o remount.rw /

然后再vim  /etc/fstab 命令打开,修改正确,然后保存退出

再reboot命令验证,应该是正确的。

但特别注意:该修复不是万能的,如果连根分区都错了,无法挂载根分区,无法出现

如上错误提示信息,就真的崩溃了,所以,修改该文件特别要小心!!!!!!!!!!!!!!!!!

猜你喜欢

转载自blog.csdn.net/zxllynu/article/details/84933622