linux命令之磁盘与文件系统管理命令(上)

时间:2022-03-11 18:31:12

1.fdisk:磁盘分区工具

该命令是linux下常用的磁盘分区工具,但是只能给小于2TB的磁盘划分分区。

常用参数为-l,显示所有磁盘分区的信息。

示例:

1)显示磁盘分区列表

[root@boxiaoyuan ~]# fdisk -l  # 查看当前系统的所有磁盘分区信息
Disk /dev/sda: 21.5 GB, 21474836480 bytes  # 磁盘/dev/sda的大小
255 heads, 63 sectors/track, 2610 cylinders  # 255个虚拟磁头,63个扇区/磁道,2610个柱面
Units = cylinders of 16065 * 512 = 8225280 bytes  # 一个柱面的大小为8225280bytes
Sector size (logical/physical): 512 bytes / 512 bytes  #每个扇区的字节数
I/O size (minimum/optimal): 512 bytes / 512 bytes  # 每次读写的字节数
Disk identifier: 0x0000db9b

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1        1045     8388608   82  Linux swap / Solaris
/dev/sda2   *        1045        2611    12581888   83  Linux

说明:Device表示分区;Boot表示启动分区,用*表示;Start表示开始的柱面;End表示结束的柱面;Blocks表示快数量;Id表示分区类型Id;System表示分区类型。

2)模拟分区实战

[root@boxiaoyuan ~]# 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 0x784b7b62.
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): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1 First cylinder (1-130, default 1): 
Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-130, default 130): +100M

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x784b7b62

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
e
Partition number (1-4): 2 First cylinder (15-130, default 15): 
Using default value 15 Last cylinder, +cylinders or +size{K,M,G} (15-130, default 130): 
Using default value 130 Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x784b7b62

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15         130      931770    5  Extended
 Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
p
Partition number (1-4): 3
No free sectors available

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (15-130, default 15): 
Using default value 15 Last cylinder, +cylinders or +size{K,M,G} (15-130, default 130): +400M

Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x784b7b62

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15         130      931770    5  Extended
/dev/sdb5              15          66      417658+  83  Linux

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (67-130, default 67): 
Using default value 67 Last cylinder, +cylinders or +size{K,M,G} (67-130, default 130): 
Using default value 130 Command (m for help): p

Disk /dev/sdb: 1073 MB, 1073741824 bytes
255 heads, 63 sectors/track, 130 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: 0x784b7b62

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1          14      112423+  83  Linux
/dev/sdb2              15         130      931770    5  Extended
/dev/sdb5              15          66      417658+  83  Linux
/dev/sdb6              67         130      514048+  83  Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@boxiaoyuan ~]# partprobe /dev/sdb  # 执行该命令通知内核分区表已更新,不需要重启系统就可以让分区生效

格式化磁盘

[root@boxiaoyuan ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
28112 inodes, 112420 blocks
5621 blocks (5.00%) reserved for the super user
第一个数据块=1
Maximum filesystem blocks=67371008
14 block groups
8192 blocks per group, 8192 fragments per group
2008 inodes per group
Superblock backups stored on blocks: 
        8193, 24577, 40961, 57345, 73729

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

This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

挂载磁盘

[root@boxiaoyuan ~]# mount /dev/sdb1 /mnt
[root@boxiaoyuan ~]# df -T
Filesystem     Type    1K-blocks    Used Available Use% Mounted on
/dev/sda2      ext4     12253360 8557940   3066328  74% /
tmpfs          tmpfs      953140      80    953060   1% /dev/shm
/dev/sr0       iso9660   3824484 3824484         0 100% /media/CentOS_6.8_Final
/dev/sdb1      ext4       104769    1550     97598   2% /mnt

 2.partprobe:更新内核的硬盘分区表信息

该命令用于在硬盘分区发生变化时,更新内核的硬盘分区表数据,可以在不重启系统时就更新分区表信息。

[root@boxiaoyuan ~]# partprobe /dev/sdb

3.parted:磁盘分区工具

该命令用于对大于2TB的磁盘进行分区,并且要将磁盘转换为GPT格式。

常用的参数选项为-l,显示所有磁盘分区的信息。

通过parted -h或者直接通过parted进入交互模式后通过-h查看帮助。

[root@boxiaoyuan ~]# parted
GNU Parted 2.1
使用 /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) h                                                                
  align-check TYPE N                       
  check NUMBER                            
  cp [FROM-DEVICE] FROM-NUMBER TO-NUMBER   
  help [COMMAND]                         
 mklabel,mktable LABEL-TYPE # 创建分区表         
  mkfs NUMBER FS-TYPE                   
 mkpart PART-TYPE [FS-TYPE] START END # 创建分区 mkpartfs PART-TYPE FS-TYPE START END # 创建带有文件系统的分区
  move NUMBER START END                   
  name NUMBER NAME                        
 print [devices|free|list,all|NUMBER] # 显示分区表信息
  quit                                    
  rescue START END                       
  resize NUMBER START END               
  rm NUMBER  # 删除编号NUMBER的分区                              
  select DEVICE                          
  set NUMBER FLAG STATE                   
  toggle [NUMBER [FLAG]]                  
  unit UNIT                              
  version                                 
(parted) 

示例:

1)显示分区情况

[root@boxiaoyuan ~]# parted -l
Model: VMware, VMware Virtual S (scsi)  # 磁盘类型
Disk /dev/sda: 21.5GB  # 磁盘大小
Sector size (logical/physical): 512B/512B  # 扇区大小
Partition Table: msdos  # 分区表类型

Number  Start   End     Size    Type     File system     标志
 1      1049kB  8591MB  8590MB  primary  linux-swap(v1)
 2      8591MB  21.5GB  12.9GB  primary  ext4            启动

2)模拟分区实战

[root@boxiaoyuan ~]# parted /dev/sdb # parted直接接需要分区的设备
GNU Parted 2.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt # 为sdb磁盘创建GPT分区表,大于2TB的磁盘必须执行这一步 (parted) mkpart primary 0 500 # 创建主分区,500M                                          
警告: The resulting partition is not properly aligned for best performance.
忽略/Ignore/放弃/Cancel? Ignore # 忽略警告                                        
(parted) p # 显示分区表信息                                                   
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End    Size   File system  Name     标志
 1      17.4kB  500MB  500MB               primary

(parted) mkpart logical 501 1000 # 创建逻辑分区,大小为500MB (parted) p # 显示分区表信息                                                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 1074MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt

Number  Start   End     Size   File system  Name     标志
 1      17.4kB  500MB   500MB               primary
 2      501MB   1000MB  499MB               logical

(parted) quit # 退出                                                            
信息: You may need to update /etc/fstab.                                  

[root@boxiaoyuan ~]# ls /dev/sdb*
/dev/sdb  /dev/sdb1  /dev/sdb2
[root@bixiaoyuan ~]# mkfs.ext4 /dev/sdb1 # 格式化
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
122400 inodes, 488264 blocks
24413 blocks (5.00%) reserved for the super user
第一个数据块=1
Maximum filesystem blocks=67633152
60 block groups
8192 blocks per group, 8192 fragments per group
2040 inodes per group
Superblock backups stored on blocks: 
        8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409

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

This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@boxiaoyuan ~]# mount /dev/sdb1 /mnt  #挂载

4.mkfs:创建linux文件系统

该命令用于在指定的设备或者分区上创建格式化并创建文件系统。

常用的参数有-t:指定要创建的文件系统类型,也可以直接使用mkfs.ext4创建ext4文件系统。

5.df:报告文件系统磁盘空间的使用情况

该命令用于显示文件系统磁盘空间的使用情况。

参数 说明
-h(常用) 以容易理解的格式显示磁盘的使用情况
-i(常用) 显示文件系统的inode信息
-T(常用) 列出文件系统的类型

 

 

 

示例:

1)显示磁盘的使用情况

[root@boxiaoyuan ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda2       12253360 8557460   3066808  74% /
tmpfs             953140       0    953140   0% /dev/shm

2)-h选项

[root@boxiaoyuan ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        12G  8.2G  3.0G  74% /
tmpfs           931M   72K  931M   1% /dev/shm

3)列出文件系统的类型

[root@boxiaoyuan ~]# df -T
Filesystem     Type  1K-blocks    Used Available Use% Mounted on
/dev/sda2      ext4   12253360 8557556   3066712  74% /
tmpfs          tmpfs    953140      72    953068   1% /dev/shm

 

注:本文内容为《跟老男孩学linux运维 核心系统命令实践》的学习笔记。