Linux中磁盘管理

Linux磁盘管理基本命令

df查看磁盘分区使用情况

  • -l : 仅显示本地磁盘(默认)
  • -a : 显示所有文件系统的磁盘使用情况,包含比如/proc/
  • -h : 以1024进制计算最适合的单位显示磁盘容量
  • -H : 以1000进制计算最适合的单位显示磁盘容量
  • -T : 显示磁盘分区类型
  • -t : 显示指定类型文件系统的磁盘分区
  • -x : 不现实指定类型文件系统的磁盘分区

du统计磁盘上的文件大小

  • -b : 以byte为单位统计文件
  • -k : 以KB为单位统计文件
  • -m : 以MB为单位统计文件
  • -h : 按照1024进制以最适合的单位统计文件
  • -H : 按照1000进制以最适合的单位统计文件
  • -s : 指定统计目标

Linux磁盘分区和格式化概述

  1. 主分区和扩展分区总数不能超过4个
  2. 扩展分区最多只能有一个
  3. 扩展分区不能直接存取数据

之前为你们学习了在安装过程中使用图形界面进行分区的技能,但是当系统装好之后,随着不断地使用,会导致硬盘空间小号殆尽,这时候怎么办呢?

答:在保留原硬盘的基础上,给服务器添加新的硬盘。

在VM虚拟机中添加硬盘

  1. 将虚拟机中的Linux系统关闭;
  2. 点击设置
  3. 添加新硬盘
  4. 重启linux系统
  5. 即可完成添加硬盘步骤

Linux中分区模式

分区模式分类:

  • Linux中MBR分区
  • Linux中GPT分区

分区模式之MBR:

  • 主分区不超过四个
  • 单个分区容量最大2TB

分区模式之GPT:

  • 主分区的个数"几乎"没有限制(在GPT的分区表中最多可以支持128个主分区)
  • 单个分区容量"几乎"没有限制(在GPT的分区模式中,每个分区的大小突破了MBR分区的2TB限制;GPT模式下每个分区的大小最多可以达到18EB)
  • GPT的主分区中,不适合安装X86架构的系统
  • GPT分区模式中没有主分区、扩展分区和逻辑分区的概念,就叫分区

1EB = 1024PB,1PB = 1024TB,1TB = 1024GB
18EB = 18 * 1024PB = 18 * 1024 * 1024TB = 18 * 1024 * 1024 * 1024GB

Linux中MBR分区

  • Linux中硬件设备都是一文件的形式存在于根目录下的dev目录下
  • 硬件设备都是由Linux系统自动是别的
  • 必须对硬盘进行分区、格式化、挂载后才能使用
  • 分区需要使用一个硬盘分区工具fdisk
  • fdisk工具只能给硬盘做MBR的分区

使用fdisk进行硬盘分区:

  1. fdisk /dev/sdb
  2. m : 查看命令的帮助信息
  3. n : 给硬盘中添加一个新分区
  4. p : 添加主分区
  5. 指定分区编号(默认是1)
  6. 指定扇区的起始位置
  7. 指定扇区的终止位置(例如:+3000M或+3G)
  8. p : 查看新建的分区
  9. n : 给硬盘中添加一个新分区
  10. e : 添加扩展分区
  11. 指定分区编号(默认是2)
  12. 指定扇区的起始位置(默认)
  13. 指定扇区的终止位置(默认)
  14. n : 给硬盘中添加一个新分区
  15. l : 添加逻辑分区
  16. 指定分区编号(默认5)
  17. 指定扇区的起始位置(默认)
  18. 指定扇区的终止位置(例如:+3000M或+3G)
  19. n : 给硬盘中添加第二块逻辑新分区
  20. l : 添加逻辑分区
  21. 指定分区编号(默认6)
  22. 指定扇区的起始位置(默认)
  23. 指定扇区的终止位置(例如:+3000M或+3G)
  24. d : 删除分区
  25. 输入要删除的分区编号
  26. 2 : 删除扩展分区(因为逻辑分区是在扩展分区内的,所以逻辑分区也会被删除)
  27. p : 查看新建的分区
  28. 再次从新分区后
  29. w : 将磁盘的分区方案写入磁盘分区表中
  30. fdisk -l 查看分区是否成功

例如:

root@localhost:~# fdisk -l
Disk /dev/loop0: 87.9 MiB, 92119040 bytes, 179920 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/loop1: 86.6 MiB, 90759168 bytes, 177264 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/sda: 30 GiB, 32212254720 bytes, 62914560 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
Disklabel type: gpt
Disk identifier: B453A511-713D-490D-8F29-4F146C823C49

Device        Start      End  Sectors  Size Type
/dev/sda1      2048     4095     2048    1M BIOS boot
/dev/sda2      4096   413695   409600  200M Linux filesystem
/dev/sda3    413696  2510847  2097152    1G Linux swap
/dev/sda4   2510848 23482367 20971520   10G Linux filesystem
/dev/sda5  23482368 62912511 39430144 18.8G Linux filesystem


Disk /dev/sdb: 8 GiB, 8589934592 bytes, 16777216 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
root@localhost:~# ^C
root@localhost:~# 
root@localhost:~# fdisk -l
Disk /dev/loop0: 87.9 MiB, 92119040 bytes, 179920 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/loop1: 86.6 MiB, 90759168 bytes, 177264 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/sda: 30 GiB, 32212254720 bytes, 62914560 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
Disklabel type: gpt
Disk identifier: B453A511-713D-490D-8F29-4F146C823C49

Device        Start      End  Sectors  Size Type
/dev/sda1      2048     4095     2048    1M BIOS boot
/dev/sda2      4096   413695   409600  200M Linux filesystem
/dev/sda3    413696  2510847  2097152    1G Linux swap
/dev/sda4   2510848 23482367 20971520   10G Linux filesystem
/dev/sda5  23482368 62912511 39430144 18.8G Linux filesystem


Disk /dev/sdb: 8 GiB, 8589934592 bytes, 16777216 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
root@localhost:~# fdisk /dev/sdb 

Welcome to fdisk (util-linux 2.31.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xab6f1544.

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table


Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-16777215, default 2048):
Last sector, +sectors or +size{K,M,G,T,P} (2048-16777215, default 16777215): +3000M

Created a new partition 1 of type 'Linux' and of size 3 GiB.

Command (m for help): p
Disk /dev/sdb: 8 GiB, 8589934592 bytes, 16777216 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
Disklabel type: dos
Disk identifier: 0xab6f1544

Device     Boot Start     End Sectors Size Id Type
/dev/sdb1        2048 6146047 6144000   3G 83 Linux

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): e
Partition number (2-4, default 2): 2
First sector (6146048-16777215, default 6146048):
Last sector, +sectors or +size{K,M,G,T,P} (6146048-16777215, default 16777215):

Created a new partition 2 of type 'Extended' and of size 5.1 GiB.

Command (m for help): p
Disk /dev/sdb: 8 GiB, 8589934592 bytes, 16777216 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
Disklabel type: dos
Disk identifier: 0xab6f1544

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sdb1          2048  6146047  6144000    3G 83 Linux
/dev/sdb2       6146048 16777215 10631168  5.1G  5 Extended

Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (6148096-16777215, default 6148096):
Last sector, +sectors or +size{K,M,G,T,P} (6148096-16777215, default 16777215): +2000M

Created a new partition 5 of type 'Linux' and of size 2 GiB.

Command (m for help): p
Disk /dev/sdb: 8 GiB, 8589934592 bytes, 16777216 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
Disklabel type: dos
Disk identifier: 0xab6f1544

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sdb1          2048  6146047  6144000    3G 83 Linux
/dev/sdb2       6146048 16777215 10631168  5.1G  5 Extended
/dev/sdb5       6148096 10244095  4096000    2G 83 Linux

Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 6
First sector (10246144-16777215, default 10246144):
Last sector, +sectors or +size{K,M,G,T,P} (10246144-16777215, default 16777215):

Created a new partition 6 of type 'Linux' and of size 3.1 GiB.

Command (m for help): p
Disk /dev/sdb: 8 GiB, 8589934592 bytes, 16777216 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
Disklabel type: dos
Disk identifier: 0xab6f1544

Device     Boot    Start      End  Sectors  Size Id Type
/dev/sdb1           2048  6146047  6144000    3G 83 Linux
/dev/sdb2        6146048 16777215 10631168  5.1G  5 Extended
/dev/sdb5        6148096 10244095  4096000    2G 83 Linux
/dev/sdb6       10246144 16777215  6531072  3.1G 83 Linux

Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 7
No free sectors available.

Command (m for help): d
Partition number (1,2,5,6, default 6): 2

Partition 2 has been deleted.

Command (m for help): p
Disk /dev/sdb: 8 GiB, 8589934592 bytes, 16777216 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
Disklabel type: dos
Disk identifier: 0xab6f1544

Device     Boot Start     End Sectors Size Id Type
/dev/sdb1        2048 6146047 6144000   3G 83 Linux

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (6146048-16777215, default 6146048):
Last sector, +sectors or +size{K,M,G,T,P} (6146048-16777215, default 16777215): +1000M

Created a new partition 2 of type 'Linux' and of size 1000 MiB.

Command (m for help): p
Disk /dev/sdb: 8 GiB, 8589934592 bytes, 16777216 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
Disklabel type: dos
Disk identifier: 0xab6f1544

Device     Boot   Start     End Sectors  Size Id Type
/dev/sdb1          2048 6146047 6144000    3G 83 Linux
/dev/sdb2       6146048 8194047 2048000 1000M 83 Linux

Command (m for help): n
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)
Select (default p): e
Partition number (3,4, default 3): 3
First sector (8194048-16777215, default 8194048):
Last sector, +sectors or +size{K,M,G,T,P} (8194048-16777215, default 16777215):

Created a new partition 3 of type 'Extended' and of size 4.1 GiB.

Command (m for help): p
Disk /dev/sdb: 8 GiB, 8589934592 bytes, 16777216 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
Disklabel type: dos
Disk identifier: 0xab6f1544

Device     Boot   Start      End Sectors  Size Id Type
/dev/sdb1          2048  6146047 6144000    3G 83 Linux
/dev/sdb2       6146048  8194047 2048000 1000M 83 Linux
/dev/sdb3       8194048 16777215 8583168  4.1G  5 Extended

Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 5
First sector (8196096-16777215, default 8196096):
Last sector, +sectors or +size{K,M,G,T,P} (8196096-16777215, default 16777215): +1000M

Created a new partition 5 of type 'Linux' and of size 1000 MiB.

Command (m for help): p
Disk /dev/sdb: 8 GiB, 8589934592 bytes, 16777216 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
Disklabel type: dos
Disk identifier: 0xab6f1544

Device     Boot   Start      End Sectors  Size Id Type
/dev/sdb1          2048  6146047 6144000    3G 83 Linux
/dev/sdb2       6146048  8194047 2048000 1000M 83 Linux
/dev/sdb3       8194048 16777215 8583168  4.1G  5 Extended
/dev/sdb5       8196096 10244095 2048000 1000M 83 Linux

Command (m for help): n
All space for primary partitions is in use.
Adding logical partition 6
First sector (10246144-16777215, default 10246144):
Last sector, +sectors or +size{K,M,G,T,P} (10246144-16777215, default 16777215):

Created a new partition 6 of type 'Linux' and of size 3.1 GiB.

Command (m for help): p
Disk /dev/sdb: 8 GiB, 8589934592 bytes, 16777216 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
Disklabel type: dos
Disk identifier: 0xab6f1544

Device     Boot    Start      End Sectors  Size Id Type
/dev/sdb1           2048  6146047 6144000    3G 83 Linux
/dev/sdb2        6146048  8194047 2048000 1000M 83 Linux
/dev/sdb3        8194048 16777215 8583168  4.1G  5 Extended
/dev/sdb5        8196096 10244095 2048000 1000M 83 Linux
/dev/sdb6       10246144 16777215 6531072  3.1G 83 Linux

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

root@localhost:~# 

Linux中GPT分区

  • Linux中硬件设备都是一文件的形式存在于根目录下的dev目录下
  • 硬件设备都是由Linux系统自动是别的
  • 必须对硬盘进行分区、格式化、挂载后才能使用
  • GPT分区需要使用另一个硬盘分区工具parted
  • parted工具既可以给硬盘做MBR分区,又可以给硬盘做GPT分区
  • GPT分区模式中没有主分区、扩展分区和逻辑分区的概念,就叫分区

使用parted进行硬盘分区:

  1. parted : 进入parted分区工具
  2. help : 查看分区帮助信息
  3. select /dev/sdc : 切换分区的目标硬盘
  4. mklable : 给目标硬盘指定分区表的类型
    1. mklable msdos : 给目标硬盘指定分区表的类型为MBR
    2. mklable gpt : 给目标硬盘指定分区表的类型为GPT
  5. print : 查看当前硬盘的分区详情
  6. print all : 查看所有硬盘的分区详情
  7. 分区时可以选择使用交互模式或者命令模式
  8. 先选择交互模式进行分区
  9. mkpart : 给硬盘中添加一个新分区
  10. 填写分区名称:games
  11. 指定分区文件系统:默认ext2
  12. 指分区起始位置:从1开始(单位为M)
  13. 指分区结束位置:从2000(单位为M)
  14. print : 查看当前硬盘的分区详情
  15. 先选择命令模式进行分区
  16. mkpart test 2000 3000 : 使用命令模式进行分区,分出一个名为test的分区(单位为M)
  17. print : 查看当前硬盘的分区详情
  18. unit GB : 默认分区时指定分区起始和结束位置是以MB为单位的,通过这个命令可以设置单位
  19. quit : 退出parted分区工具

例如:

root@localhost:~# parted
GNU Parted 3.2
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  resizepart NUMBER END                    resize partition NUMBER
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted
(parted) select /dev/sd
sda   sda1  sda2  sda3  sda4  sda5  sdb   sdb1  sdb2  sdb3  sdb5  sdb6  sdc
(parted) select /dev/sdc
Using /dev/sdc
(parted) mklabel gpt
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start  End  Size  File system  Name  Flags

(parted) print all
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sda: 32.2GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system     Name  Flags
 1      1049kB  2097kB  1049kB                        bios_grub
 2      2097kB  212MB   210MB   ext4
 3      212MB   1286MB  1074MB  linux-swap(v1)
 4      1286MB  12.0GB  10.7GB  ext4
 5      12.0GB  32.2GB  20.2GB  ext4


Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start   End     Size    Type      File system  Flags
 1      1049kB  3147MB  3146MB  primary
 2      3147MB  4195MB  1049MB  primary
 3      4195MB  8590MB  4395MB  extended
 5      4196MB  5245MB  1049MB  logical
 6      5246MB  8590MB  3344MB  logical


Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start  End  Size  File system  Name  Flags


Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
Model: NECVMWar VMware SATA CD01 (scsi)
Disk /dev/sr0: 845MB
Sector size (logical/physical): 2048B/2048B
Partition Table: mac
Disk Flags:

Number  Start  End    Size    File system  Name   Flags
 1      2048B  6143B  4096B                Apple
 2      807MB  809MB  2392kB               EFI


(parted) mkpart
Partition name?  []? games
File system type?  [ext2]?
Start? 1
End? 1024
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name   Flags
 1      1049kB  1024MB  1023MB  ext2         games

(parted) mkpart test 1024 3000
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name   Flags
 1      1049kB  1024MB  1023MB  ext2         games
 2      1024MB  3000MB  1976MB               test

(parted) mkpart test 3000 4100
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name   Flags
 1      1049kB  1024MB  1023MB  ext2         games
 2      1024MB  3000MB  1976MB               test
 3      3000MB  4100MB  1100MB               test

(parted) mkpart test 3000 5100
Warning: You requested a partition from 3000MB to 5100MB (sectors 5859375..9960937).
The closest location we can manage is 4100MB to 5100MB (sectors 8007680..9960937).
Is this still acceptable to you?
Yes/No? yes
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name   Flags
 1      1049kB  1024MB  1023MB  ext2         games
 2      1024MB  3000MB  1976MB               test
 3      3000MB  4100MB  1100MB               test
 4      4100MB  5100MB  1000MB               test

(parted) mkpart aaa 3000 5100
Warning: You requested a partition from 3000MB to 5100MB (sectors 5859375..9960937).
The closest location we can manage is 5100MB to 5100MB (sectors 9960938..9960938).
Is this still acceptable to you?
Yes/No? no
(parted) mkpart aaa 3000
End?
End?
End? 9999
Error: The location 9999 is outside of the device /dev/sdc.
(parted) 8000
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  resizepart NUMBER END                    resize partition NUMBER
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name   Flags
 1      1049kB  1024MB  1023MB  ext2         games
 2      1024MB  3000MB  1976MB               test
 3      3000MB  4100MB  1100MB               test
 4      4100MB  5100MB  1000MB               test

(parted) mkpart aaa 3000 8100
Warning: You requested a partition from 3000MB to 8100MB (sectors 5859375..15820312).
The closest location we can manage is 5100MB to 8100MB (sectors 9960938..15820312).
Is this still acceptable to you?
Yes/No? yes
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? Cancel
(parted) mkpart aaa 5100 8888
Error: The location 8888 is outside of the device /dev/sdc.
(parted) mkpart aaa 5100 8000
(parted) pring
  align-check TYPE N                        check partition N for TYPE(min|opt) alignment
  help [COMMAND]                           print general help, or help on COMMAND
  mklabel,mktable LABEL-TYPE               create a new disklabel (partition table)
  mkpart PART-TYPE [FS-TYPE] START END     make a partition
  name NUMBER NAME                         name partition NUMBER as NAME
  print [devices|free|list,all|NUMBER]     display the partition table, available devices, free space, all found partitions, or a particular partition
  quit                                     exit program
  rescue START END                         rescue a lost partition near START and END
  resizepart NUMBER END                    resize partition NUMBER
  rm NUMBER                                delete partition NUMBER
  select DEVICE                            choose the device to edit
  disk_set FLAG STATE                      change the FLAG on selected device
  disk_toggle [FLAG]                       toggle the state of FLAG on selected device
  set NUMBER FLAG STATE                    change the FLAG on partition NUMBER
  toggle [NUMBER [FLAG]]                   toggle the state of FLAG on partition NUMBER
  unit UNIT                                set the default unit to UNIT
  version                                  display the version number and copyright information of GNU Parted
(parted) print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 8590MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:

Number  Start   End     Size    File system  Name   Flags
 1      1049kB  1024MB  1023MB  ext2         games
 2      1024MB  3000MB  1976MB               test
 3      3000MB  4100MB  1100MB               test
 4      4100MB  5100MB  1000MB               test
 5      5100MB  8000MB  2899MB               aaa

(parted) quit
Information: You may need to update /etc/fstab.

root@localhost:~#

Linux中分区的格式化

分区的格式化工具mkfs

mkfs.ext3 /dev/sdb1

  • 将/dev/sdb1分区格式化成ext3文件类型

mkfs -t ext4 /dev/sdb2

  • 将/dev/sdb2分区格式化成ext4文件类型

Linux中挂载分区

分区默认的挂载目录/mnt目录

挂载命令mount

mount /dev/sdb1 /mnt/test

卸载命令umount

umount /mnt/test

使用mount和umount挂载只是暂时性的,系统重启后需要重新手动挂载

编辑/etc/fstab文件使挂载永久生效

在/etc/fstab最后重新起一行,添加

/dev/sdb1 /mnt/test ext4 default 0 0
  • /dev/sdb1代表设备名称
  • /mnt/test代表挂载点
  • ext4代表文件系统的类型

Linux中创建swap分区

如何为硬盘添加swap交换分区?

  1. 建立一个普通的linux分区
  2. 修改分区类型的16进制编码
  3. 格式化交换分区
  4. 启用交换分区

1.建立一个普通的linux分区

2.修改分区类型的16进制编码

  1. fdisk /dev/sdb : 进入fdisk分区工具
  2. p : 查看分区详情
  3. t : 修改分区类型编号
  4. 5 : 分区编号(/dev/sdb5分区)
  5. L : 查看编码列表
  6. 82 : linux中swap交换分区的编码
  7. w : 将修改后磁盘的分区方案写入磁盘分区表中并保存
  8. mkswap /dev/sdb5 : 格式化swap交换分区
  9. swapon /dev/sdb5 : 启用swap交换分区
  10. 编辑/etc/fstab是系统启动时自动启动swap交换分区,新增一行
/dev/sdb5 swap swap defaults 0 0
  1. free : 查看swap交换分区加载状况
  2. swapoff /dev/sdb5 : 停止swap交换分区

Linux中创建swap文件

  1. 创建要作为swap分区的文件:增加1GB大小的交换分区,则命令写法如下,其中的count等于想要的块的数量(bs*count=文件大小)。
dd if=/dev/zero of=/root/.swapfile bs=1M count=1024
  1. 格式化为交换分区文件:
mkswap /root/.swapfile #建立swap的文件系统
  1. 启用交换分区文件:
swapon /root/.swapfile #启用swap文件
  1. 使系统开机时自启用,在文件/etc/fstab中添加一行:
/root/.swapfile swap swap defaults 0 0
  1. free : 查看swap交换文件加载状况
  2. swapoff /root/.swapfile : 停止swap交换文件
10-06 16:33