Linux-分区-fdisk-gdisk

4.文件系统

本章同步视频:https://edu.51cto.com/sd/e4874

4.5 文件系统管理

4.6 分区、格式化与挂载

如果我们想要在系统里面新增一颗磁碟时,应该有哪些动作需要做的呢:

n  对磁碟进行分割,以建立可用的 partition ;

n  对该 partition 进行格式化 (format),以建立系统可用的 filesystem;

n  若想要仔细一点,则可对刚刚建立好的 filesystem 进行检验;

n  在 Linux 系统上,需要建立挂载点 (亦即是目录),并将他挂载上来;

4.6.1 查看分区状况

1.lsblk - list block devices

[root@localhost tmp]# lsblk

NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda             8:0    0   20G  0 disk

├─sda1          8:1    0  500M  0 part /boot

└─sda2          8:2    0 13.7G  0 part

  ├─rhel-root 253:0    0  9.8G  0 lvm  /

  ├─rhel-swap 253:1    0    2G  0 lvm  [SWAP]

  └─rhel-home 253:2    0    2G  0 lvm  /home

sr0            11:0    1 1024M  0 rom 

[root@localhost tmp]# lsblk -d

NAME MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda    8:0    0   20G  0 disk

sr0   11:0    1 1024M  0 rom 

#只显示设备名,不显示分区

[root@localhost tmp]# lsblk -p

NAME                      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

/dev/sda                    8:0    0   20G  0 disk

├─/dev/sda1                 8:1    0  500M  0 part /boot

└─/dev/sda2                 8:2    0 13.7G  0 part

  ├─/dev/mapper/rhel-root 253:0    0  9.8G  0 lvm  /

  ├─/dev/mapper/rhel-swap 253:1    0    2G  0 lvm  [SWAP]

  └─/dev/mapper/rhel-home 253:2    0    2G  0 lvm  /home

/dev/sr0                   11:0    1 1024M  0 rom 

#显示完整设备名

2.blkid - locate/print block device attributes

[root@localhost tmp]# blkid

/dev/sda1: UUID="decc164a-f28b-4b7f-ae37-9fc38a347d94" TYPE="xfs"

/dev/sda2: UUID="Ijm3mK-onGH-ht6u-tGqw-qYYS-QrmH-dxbqkr" TYPE="LVM2_member"

/dev/mapper/rhel-root: UUID="f02db577-90a3-4ae2-a434-b43c4ab34d7e" TYPE="xfs"

/dev/mapper/rhel-swap: UUID="145d4801-311d-4efe-b188-7620c7a9ada9" TYPE="swap"

/dev/mapper/rhel-home: UUID="6ef1eb27-38b9-46bd-b6d4-abc3557b8f8c" TYPE="xfs"

#查看设备的UUID

3.GNU Parted - a partition manipulation program

[root@localhost tmp]# parted /dev/sda print

Model: VMware, VMware Virtual S (scsi)

Disk /dev/sda: 21.5GB

Sector size (logical/physical): 512B/512B

Partition Table: msdos

Disk Flags:

 

Number  Start   End     Size    Type     File system  Flags

 1      1049kB  525MB   524MB   primary  xfs          boot

 2      525MB   15.2GB  14.7GB  primary               lvm

#查看硬盘分区情况

4.fdisk - manipulate disk partition table

[root@localhost tmp]# fdisk -l /dev/sda

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000c007a

   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *        2048     1026047      512000   83  Linux

/dev/sda2         1026048    29714431    14344192   8e  Linux LVM

#查看硬盘分区情况

4.6.2 分区1-fdisk

1.确保有剩余磁盘空间

[root@localhost ~]# lsblk

NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda             8:0    0   20G  0 disk

├─sda1          8:1    0  500M  0 part /boot

└─sda2          8:2    0 13.7G  0 part

  ├─rhel-root 253:0    0  9.8G  0 lvm  /

  ├─rhel-swap 253:1    0    2G  0 lvm  [SWAP]

  └─rhel-home 253:2    0    2G  0 lvm  /home

sr0            11:0    1 1024M  0 rom 

#剩余空间=20G-500M-13.7G,有剩余空间。

2.fdisk使用

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

Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.

Be careful before using the write command.

Command (m for help): m     #输入m,查看可使用的命令

Command action

   a   toggle a bootable flag

   b   edit bsd disklabel

   c   toggle the dos compatibility flag

   d   delete a partition    #删除分区

   g   create a new empty GPT partition table

   G   create an IRIX (SGI) partition table

   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    #修改分区id

   u   change display/entry units

   v   verify the partition table

   w   write table to disk and exit     #保存退出

   x   extra functionality (experts only)

3.新建一个主分区

Command (m for help): n     #新建一个分区

Partition type:

   p   primary (2 primary, 0 extended, 2 free)

   e   extended

Select (default p): p     #创建一个主分区

Partition number (3,4, default 3):

#指定分区号,直接回车使用默认号码3

First sector (29714432-41943039, default 29714432):

#指定分区的起始扇区,直接回车使用默认值

Using default value 29714432

Last sector, +sectors or +size{K,M,G} (29714432-41943039, default 41943039): +200M

#指定分区结束扇区,也可以直接输入分区大小

Partition 3 of type Linux and of size 200 MiB is set

#3号主分区建立

4.新建一个扩建分区

Command (m for help): n   #新建一个分区

Partition type:

   p   primary (3 primary, 0 extended, 1 free)

   e   extended

Select (default e): e    #选择分区类型为扩展分区,直接回车采用默认值e

Selected partition 4     #分区号为4(只剩下4了,没的选了)

First sector (30124032-41943039, default 30124032):

#设置起始扇区,直接回车采用默认值

Using default value 30124032

Last sector, +sectors or +size{K,M,G} (30124032-41943039, default 41943039): +4G

#设置结束扇区,也可以直接指定容量

Partition 4 of type Extended and of size 4 GiB is set

#扩展分区被建立。

#注:主分区可以直接被格式化后使用,而扩展分区不能,需要再在扩展分区中划分逻辑分区,然后格式化逻辑分区才能使用。

5.新建一个逻辑分区

Command (m for help): n    #新建一个分区

All primary partitions are in use  #没有主分区可选

Adding logical partition 5      #只能直接创建逻辑分区,分区号是5

First sector (30126080-38512639, default 30126080):

#设置起始扇区,直接回车采用默认值

Using default value 30126080

Last sector, +sectors or +size{K,M,G} (30126080-38512639, default 38512639): +500M

#设置结束扇区,也可以直接指定容量

Partition 5 of type Linux and of size 500 MiB is set

#逻辑分区被建立。

6.列出分区表

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

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000c007a

   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *        2048     1026047      512000   83  Linux

/dev/sda2         1026048    29714431    14344192   8e  Linux LVM

/dev/sda3        29714432    30124031      204800   83  Linux

/dev/sda4        30124032    38512639     4194304    5  Extended

/dev/sda5        30126080    31150079      512000   83  Linux

7.保存

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.

#废话一堆

8.更新内核信息

[root@localhost ~]# partprobe /dev/sda

9.查看新的分区表

[root@localhost ~]# lsblk

NAME          MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT

sda             8:0    0   20G  0 disk

├─sda1          8:1    0  500M  0 part /boot

├─sda2          8:2    0 13.7G  0 part

│ ├─rhel-root 253:0    0  9.8G  0 lvm  /

│ ├─rhel-swap 253:1    0    2G  0 lvm  [SWAP]

│ └─rhel-home 253:2    0    2G  0 lvm  /home

├─sda3          8:3    0  200M  0 part

├─sda4          8:4    0    1K  0 part

└─sda5          8:5    0  500M  0 part

sr0            11:0    1 1024M  0 rom 

10.删除分区

Command (m for help): d    #删除分区

Partition number (1-5, default 5): 5    #指定要删除的分区号

Partition 5 is deleted

#注:删除分区后也要进行保存(7)和更新内核信息(8)的操作

4.6.3 分区2-gdisk

1.利用gdisk分区

[root@localhost ~]# gdisk /dev/sda

GPT fdisk (gdisk) version 0.8.6

Partition table scan:

  MBR: MBR only

  BSD: not present

  APM: not present

  GPT: not present

***************************************************************

Found invalid GPT and valid MBR; converting MBR to GPT format.

THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing 'q' if

you don't want to convert your MBR partitions to GPT format!

***************************************************************

Command (? for help):  #查看可用命令

b back up GPT data to a file

c change a partition's name

d delete a partition     #删除分区

i show detailed information on a partition

l list known partition types

n add a new partition    #新建分区

o create a new empty GUID partition table (GPT)

p print the partition table     #显示分区

q quit without saving changes     #不保存退出

r recovery and transformation options (experts only)

s sort partitions

t change a partition's type code

v verify disk

w write table to disk and exit     #保存退出

x extra functionality (experts only)

? print this menu     #帮助

2.查看分区表

Command (? for help): p

Disk /dev/sda: 41943040 sectors, 20.0 GiB

Logical sector size: 512 bytes

Disk identifier (GUID): FAE2CF35-09B0-4F2C-B43D-B8E6DCD48693

Partition table holds up to 128 entries

First usable sector is 34, last usable sector is 41943006

Partitions will be aligned on 2048-sector boundaries

Total free space is 11820989 sectors (5.6 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name

   1            2048         1026047   500.0 MiB   8300  Linux filesystem

   2         1026048        29714431   13.7 GiB    8E00  Linux LVM

   3        29714432        30124031   200.0 MiB   8300  Linux filesystem

3.新建分区

Command (? for help): n    #新建分区

Partition number (4-128, default 4):    #设置分区号,回车使用默认值

First sector (34-41943006, default = 30124032) or {+-}size{KMGTP}:

#设置起始扇区,回车使用默认值

Last sector (30124032-41943006, default = 41943006) or {+-}size{KMGTP}: +300M

#设置结束扇区,也可以直接指定容量

Current type is 'Linux filesystem'

Hex code or GUID (L to show codes, Enter = 8300):

#设置分区类型,回车使用默认值8300

Changed type of partition to 'Linux filesystem'

#新建分区完成

4.删除分区

Command (? for help): d   #删除分区

Partition number (1-4):  4  #指定要删除的分区号

5.保存

Command (? for help): w     #保存

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING

PARTITIONS!!

Do you want to proceed? (Y/N): y   #输入y,否则会退出

OK; writing new GUID partition table (GPT) to /dev/vda.

Warning: The kernel is still using the old partition table.

The new table will be used at the next reboot.

The operation has completed successfully.

6.更新内核信息

[root@localhost ~]# partprobe /dev/sda

7.说明

u  MBR和GPT分区不能共存

u  2T以内的硬盘建议使用MBR

u  2T以上的硬盘建议使用GPT

u  MBR有主分区、扩展分区、逻辑分区的区别

u  MBR一共只有4个分区

u  GPT可以有128个


猜你喜欢

转载自blog.51cto.com/5482173/2490763
今日推荐