Linux中磁盘的分区管理

一.本地存储设备的识别
fdisk -l ##真实存在的设备

[root@foundation4 ~]# fdisk -l

Disk /dev/sda: 500.1 GB, 500107862016 bytes, 976773168 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk label type: dos
Disk identifier: 0x72d0e306

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048   209719295   104858624    7  HPFS/NTFS/exFAT
/dev/sda2       209719296   976773119   383526912    f  W95 Ext'd (LBA)
/dev/sda5       209721344   465575935   127927296    7  HPFS/NTFS/exFAT
/dev/sda6       465577984   721432575   127927296    7  HPFS/NTFS/exFAT
/dev/sda7       721434624   723531775     1048576   83  Linux
/dev/sda8       723533824   976773119   126619648   8e  Linux LVM

Disk /dev/mapper/rhel_foundation4-root: 125.5 GB, 125489381376 bytes, 245096448 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/mapper/rhel_foundation4-swap: 4160 MB, 4160749568 bytes, 8126464 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes


Disk /dev/sdb: 15.6 GB, 15597568000 bytes, 30464000 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: 0x00000000

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1              32    30463999    15231984    c  W95 FAT32 (LBA)

cat /proc/partition ##系统识别的设备

[root@foundation4 ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0  488386584 sda
   8        1  104858624 sda1
   8        2          1 sda2
   8        5  127927296 sda5
   8        6  127927296 sda6
   8        7    1048576 sda7
   8        8  126619648 sda8
  11        0    1048575 sr0
 253        0  122548224 dm-0
 253        1    4063232 dm-1
   8       16   15232000 sdb
   8       17   15231984 sdb1

blkid ##系统可使用的设备

[root@foundation4 ~]# blkid
/dev/sda1: UUID="40486AB4486AA87C" TYPE="ntfs" 
/dev/sda5: LABEL="M-hM-=M-/M-dM-;M-6" UUID="CD2195AC587757F8" TYPE="ntfs" 
/dev/sda6: LABEL="M-fM-^VM-^GM-fM-!M-#" UUID="71FB325ED60ECB36" TYPE="ntfs" 
/dev/sda7: UUID="70e9c141-7b3b-4742-8627-fc08be1a3079" TYPE="xfs" 
/dev/sda8: UUID="9hA1yC-UT6e-COXM-RGDx-YoUx-bmVO-2yx3ot" TYPE="LVM2_member" 
/dev/mapper/rhel_foundation4-root: UUID="f08aeac2-c3c7-475c-b7c1-ce127b354bec" TYPE="xfs" 
/dev/mapper/rhel_foundation4-swap: UUID="b85136f7-b85a-4c66-88a4-c5019295ab54" TYPE="swap" 
/dev/sdb1: UUID="5E2E-4CF6" TYPE="vfat" 

df ##系统正在挂载的设备

[root@foundation4 ~]# df
Filesystem                        1K-blocks     Used Available Use% Mounted on
/dev/mapper/rhel_foundation4-root 122488388 13753268 108735120  12% /
devtmpfs                            1958032        0   1958032   0% /dev
tmpfs                               1974520      244   1974276   1% /dev/shm
tmpfs                               1974520     9204   1965316   1% /run
tmpfs                               1974520        0   1974520   0% /sys/fs/cgroup
/dev/sda7                           1038336   161016    877320  16% /boot
tmpfs                                394904       28    394876   1% /run/user/1000
/dev/sdb1                          15223792  3996592  11227200  27% /run/media/kiosk/5E2E-4CF6

二.设备的挂载和卸载
1.设备名称
/dev/xdx ##/dev/hd0 /dev/hd1 /dev/sda /dev/sdb等等
/dev/sr0 ##光驱
/dev/mapper/* ##虚拟设备
2.设备的挂载
mount 设备 挂载点
eg: mount /dev/sdb1 /mnt ##挂载sdb1到mnt

[root@foundation4 ~]# df
Filesystem                        1K-blocks     Used Available Use% Mounted on
/dev/mapper/rhel_foundation4-root 122488388 10059244 112429144   9% /
devtmpfs                            1958032        0   1958032   0% /dev
tmpfs                               1974520      244   1974276   1% /dev/shm
tmpfs                               1974520     9204   1965316   1% /run
tmpfs                               1974520        0   1974520   0% /sys/fs/cgroup
/dev/sda7                           1038336   161016    877320  16% /boot
tmpfs                                394904       36    394868   1% /run/user/1000
/dev/sdb1                          15223792  3996592  11227200  27% /run/media/kiosk/5E2E-4CF6
[root@foundation4 ~]# mount /dev/sdb1  /mnt
[root@foundation4 ~]# df
Filesystem                        1K-blocks     Used Available Use% Mounted on
/dev/mapper/rhel_foundation4-root 122488388 10059244 112429144   9% /
devtmpfs                            1958032        0   1958032   0% /dev
tmpfs                               1974520      244   1974276   1% /dev/shm
tmpfs                               1974520     9204   1965316   1% /run
tmpfs                               1974520        0   1974520   0% /sys/fs/cgroup
/dev/sda7                           1038336   161016    877320  16% /boot
tmpfs                                394904       36    394868   1% /run/user/1000
/dev/sdb1                          15223792  3996592  11227200  27% /mnt  ##挂载成功

umount /mnt| /dev/sdb1 ##卸载

[root@foundation4 ~]# umount /mnt
[root@foundation4 ~]# df
Filesystem                        1K-blocks     Used Available Use% Mounted on
/dev/mapper/rhel_foundation4-root 122488388 10059164 112429224   9% /
devtmpfs                            1958032        0   1958032   0% /dev
tmpfs                               1974520      244   1974276   1% /dev/shm
tmpfs                               1974520     9204   1965316   1% /run
tmpfs                               1974520        0   1974520   0% /sys/fs/cgroup
/dev/sda7                           1038336   161016    877320  16% /boot
tmpfs                                394904       36    394868   1% /run/user/1000
/dev/sdb1                          15223792  3996592  11227200  27% /run/media/kiosk/5E2E-4CF6  ##卸载成功

mount -o ro /dev/sdb1 /mnt ##只读挂载
mount ##查看挂载信息
mount -o remount, rw /dev/sdb1 | /mnt ##重新读写挂载
3.解决设备正忙的情况
(1)方法1 fuser -kvm /mnt umount /mnt

[root@foundation4 ~]# mount /dev/sdb1  /mnt
[root@foundation4 ~]# cd /mnt

[root@foundation4 mnt]# umount /mnt
umount: /mnt: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
[root@foundation4 mnt]# fuser -kvm /mnt
                     USER        PID ACCESS COMMAND
/mnt:                root     kernel mount /run/media/kiosk/5E2E-4CF6
                     root      10718 ..c.. bash
Killed
[kiosk@foundation4 ~]$ su - root
Password: 
Last login: Wed Apr 17 04:19:07 CST 2019 on pts/0
[root@foundation4 ~]# umount /mnt
[root@foundation4 ~]# df
Filesystem                        1K-blocks     Used Available Use% Mounted on
/dev/mapper/rhel_foundation4-root 122488388 10060392 112427996   9% /
devtmpfs                            1958032        0   1958032   0% /dev
tmpfs                               1974520      244   1974276   1% /dev/shm
tmpfs                               1974520     9204   1965316   1% /run
tmpfs                               1974520        0   1974520   0% /sys/fs/cgroup
/dev/sda7                           1038336   161016    877320  16% /boot
tmpfs                                394904       36    394868   1% /run/user/1000
/dev/sdb1                          15223792  3996592  11227200  27% /run/media/kiosk/5E2E-4CF6 

(2)方法2 lsof /mnt
[root@foundation4 ~]# lsof /mnt
lsof: WARNING: can’t stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
Output information may be incomplete.

[root@foundation4 ~]# mount /dev/sdb1 /mnt
[root@foundation4 ~]# cd /mnt
[root@foundation4 mnt]# umount /dev/sdb1
umount: /mnt: target is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))
[root@foundation4 mnt]# cd
[root@foundation4 ~]# lsof /mnt
lsof: WARNING: can't stat() fuse.gvfsd-fuse file system /run/user/1000/gvfs
      Output information may be incomplete.
[root@foundation4 ~]# umount /mnt

三.磁盘的分区
1.磁盘分区
一块硬盘最多只能划分4个主分区,即使硬盘还有剩余空间,也无法继续进行分区。
如果需要更多的分区,则第四个主分区应为扩展分区,此时扩展分区相当于一个容器,可以在扩展分区中创建逻辑分区,所有逻辑分区的总和等于扩展分区的大小。
2.磁盘的分区步骤
1.fdisk -l ##查看真实存在的设备
2.fdisk /dev /vdb ##创建分区
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 ##删除
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)

[root@localhost Desktop]# fdisk -l  ##查看分区的信息

Disk /dev/vda: 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 label type: dos
Disk identifier: 0x00013f3e

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux    ##/dev/vda1已满

Disk /dev/vdb: 10.7 GB, 10737418240 bytes, 20971520 sectors   ##在/dev/vdb建立分区
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 Desktop]# fdisk /dev/vdb
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.

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

Command (m for help): n  ##新建
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p  ##建立主分区
Partition number (1-4, default 1):  ##主分区id用默认,即直接回车
First sector (2048-20971519, default 2048):  ##主分区起始直接用默认,即直接回车
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +100M
Partition 1 of type Linux and of size 100 MiB is set

Command (m for help): p  ##显示建立分区的信息

Disk /dev/vdb: 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 label type: dos
Disk identifier: 0xa00b31f2

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 
First sector (206848-20971519, default 206848): 
Using default value 206848
Last sector, +sectors or +size{K,M,G} (206848-20971519, default 20971519): +100M
Partition 2 of type Linux and of size 100 MiB is set

Command (m for help): p

Disk /dev/vdb: 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 label type: dos
Disk identifier: 0xa00b31f2

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux

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): 
First sector (411648-20971519, default 411648): 
Using default value 411648
Last sector, +sectors or +size{K,M,G} (411648-20971519, default 20971519): +100M
Partition 3 of type Linux and of size 100 MiB is set

Command (m for help): n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): p
Selected partition 4
First sector (616448-20971519, default 616448): 
Using default value 616448
Last sector, +sectors or +size{K,M,G} (616448-20971519, default 20971519): +100M
Partition 4 of type Linux and of size 100 MiB is set

Command (m for help): p  ##4块主分区建立完成

Disk /dev/vdb: 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 label type: dos
Disk identifier: 0xa00b31f2

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448      821247      102400   83  Linux

Command (m for help): n
If you want to create more than four partitions, you must replace a  ##如果需要建立超过4块的分区,则需要使用扩展分区,由此可见一块硬盘最多只能建立4个主分区
primary partition with an extended partition first.

Command (m for help): d  ##删除4主分区
Partition number (1-4, default 4): 4
Partition 4 is deleted

Command (m for help): n
Partition type:
   p   primary (3 primary, 0 extended, 1 free)
   e   extended
Select (default e): e  ##建立为扩展分区
Selected partition 4
First sector (616448-20971519, default 616448): 
Using default value 616448
Last sector, +sectors or +size{K,M,G} (616448-20971519, default 20971519): 
Using default value 20971519
Partition 4 of type Extended and of size 9.7 GiB is set

Command (m for help): p

Disk /dev/vdb: 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 label type: dos
Disk identifier: 0xa00b31f2

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448    20971519    10177536    5  Extended  ##4为扩展分区

Command (m for help): n
All primary partitions are in use
Adding logical partition 5
First sector (618496-20971519, default 618496): 
Using default value 618496
Last sector, +sectors or +size{K,M,G} (618496-20971519, default 20971519): +100M
Partition 5 of type Linux and of size 100 MiB is set

Command (m for help): p

Disk /dev/vdb: 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 label type: dos
Disk identifier: 0xa00b31f2

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448    20971519    10177536    5  Extended
/dev/vdb5          618496      823295      102400   83  Linux  ##5分区为4扩展分区下的逻辑分区

Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (825344-20971519, default 825344): 
Using default value 825344
Last sector, +sectors or +size{K,M,G} (825344-20971519, default 20971519): +100M
Partition 6 of type Linux and of size 100 MiB is set


Command (m for help): n
All primary partitions are in use
Adding logical partition 7
First sector (1032192-20971519, default 1032192): 
Using default value 1032192
Last sector, +sectors or +size{K,M,G} (1032192-20971519, default 20971519): +100M
Partition 7 of type Linux and of size 100 MiB is set


Command (m for help): n
All primary partitions are in use
Adding logical partition 8
First sector (1239040-20971519, default 1239040): 
Using default value 1239040
Last sector, +sectors or +size{K,M,G} (1239040-20971519, default 20971519): +100M
Partition 8 of type Linux and of size 100 MiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 9
First sector (1445888-20971519, default 1445888): 
Using default value 1445888
Last sector, +sectors or +size{K,M,G} (1445888-20971519, default 20971519): +100M
Partition 9 of type Linux and of size 100 MiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 10
First sector (1652736-20971519, default 1652736): 
Using default value 1652736
Last sector, +sectors or +size{K,M,G} (1652736-20971519, default 20971519): +100M
Partition 10 of type Linux and of size 100 MiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 11
First sector (1859584-20971519, default 1859584): 
Using default value 1859584
Last sector, +sectors or +size{K,M,G} (1859584-20971519, default 20971519): +100M
Partition 11 of type Linux and of size 100 MiB is set

Command (m for help): P

Disk /dev/vdb: 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 label type: dos
Disk identifier: 0xa00b31f2

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448    20971519    10177536    5  Extended
/dev/vdb5          618496      823295      102400   83  Linux
/dev/vdb6          825344     1030143      102400   83  Linux
/dev/vdb7         1032192     1236991      102400   83  Linux
/dev/vdb8         1239040     1443839      102400   83  Linux
/dev/vdb9         1445888     1650687      102400   83  Linux
/dev/vdb10        1652736     1857535      102400   83  Linux
/dev/vdb11        1859584     2064383      102400   83  Linux

Command (m for help): n
All primary partitions are in use
Adding logical partition 12
First sector (2066432-20971519, default 2066432): 
Using default value 2066432
Last sector, +sectors or +size{K,M,G} (2066432-20971519, default 20971519): +100M
Partition 12 of type Linux and of size 100 MiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 13
First sector (2273280-20971519, default 2273280): 
Using default value 2273280
Last sector, +sectors or +size{K,M,G} (2273280-20971519, default 20971519): +100M
Partition 13 of type Linux and of size 100 MiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 14
First sector (2480128-20971519, default 2480128): 
Using default value 2480128
Last sector, +sectors or +size{K,M,G} (2480128-20971519, default 20971519): +100M
Partition 14 of type Linux and of size 100 MiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 15
First sector (2686976-20971519, default 2686976): 
Using default value 2686976
Last sector, +sectors or +size{K,M,G} (2686976-20971519, default 20971519): +100M
Partition 15 of type Linux and of size 100 MiB is set

Command (m for help): p

Disk /dev/vdb: 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 label type: dos
Disk identifier: 0xa00b31f2

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448    20971519    10177536    5  Extended
/dev/vdb5          618496      823295      102400   83  Linux
/dev/vdb6          825344     1030143      102400   83  Linux
/dev/vdb7         1032192     1236991      102400   83  Linux
/dev/vdb8         1239040     1443839      102400   83  Linux
/dev/vdb9         1445888     1650687      102400   83  Linux
/dev/vdb10        1652736     1857535      102400   83  Linux
/dev/vdb11        1859584     2064383      102400   83  Linux
/dev/vdb12        2066432     2271231      102400   83  Linux
/dev/vdb13        2273280     2478079      102400   83  Linux
/dev/vdb14        2480128     2684927      102400   83  Linux
/dev/vdb15        2686976     2891775      102400   83  Linux

Command (m for help): n
All primary partitions are in use
Adding logical partition 16
First sector (2893824-20971519, default 2893824): 
Using default value 2893824
Last sector, +sectors or +size{K,M,G} (2893824-20971519, default 20971519): +100M
Partition 16 of type Linux and of size 100 MiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 17
First sector (3100672-20971519, default 3100672): 
Using default value 3100672
Last sector, +sectors or +size{K,M,G} (3100672-20971519, default 20971519): +100M
Partition 17 of type Linux and of size 100 MiB is set

Command (m for help): p  

Disk /dev/vdb: 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 label type: dos
Disk identifier: 0xa00b31f2

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448    20971519    10177536    5  Extended
/dev/vdb5          618496      823295      102400   83  Linux
/dev/vdb6          825344     1030143      102400   83  Linux
/dev/vdb7         1032192     1236991      102400   83  Linux
/dev/vdb8         1239040     1443839      102400   83  Linux
/dev/vdb9         1445888     1650687      102400   83  Linux
/dev/vdb10        1652736     1857535      102400   83  Linux
/dev/vdb11        1859584     2064383      102400   83  Linux
/dev/vdb12        2066432     2271231      102400   83  Linux
/dev/vdb13        2273280     2478079      102400   83  Linux
/dev/vdb14        2480128     2684927      102400   83  Linux
/dev/vdb15        2686976     2891775      102400   83  Linux
/dev/vdb16        2893824     3098623      102400   83  Linux
/dev/vdb17        3100672     3305471      102400   83  Linux  ##共建立17块分区

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

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost Desktop]# cat /proc/partitions =fdisk -l ##查看建立的分区
major minor  #blocks  name

 253        0   10485760 vda
 253        1   10484142 vda1
 253       16   10485760 vdb
 253       17     102400 vdb1
 253       18     102400 vdb2
 253       19     102400 vdb3
 253       20          1 vdb4
 253       21     102400 vdb5
 253       22     102400 vdb6
 253       23     102400 vdb7
 253       24     102400 vdb8
 253       25     102400 vdb9
 253       26     102400 vdb10
 253       27     102400 vdb11
 253       28     102400 vdb12
 253       29     102400 vdb13
 253       30     102400 vdb14
 253       31     102400 vdb15   ##虽然在vdb下建立17块分区,但只显示16块,由此可知一个硬盘中最多只能有16块分区
[root@localhost Desktop]# partprobe   ##同步分区
Error: Error informing the kernel about modifications to partition /dev/vdb16 -- Invalid argument.  This means Linux won't know about any changes you made to /dev/vdb16 until you reboot -- so you shouldn't mount it or use it in any way before rebooting.
Error: Failed to add partition 16 (Invalid argument)
[root@localhost Desktop]# fdisk /dev/vdb
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): d
Partition number (1-17, default 17): 4  ##当将4扩展分区删除后,其容器内的所有逻辑分区均被删除
Partition 4 is deleted

Command (m for help): p

Disk /dev/vdb: 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 label type: dos
Disk identifier: 0xa00b31f2

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448    20971519    10177536    5  Extended
/dev/vdb5          618496      823295      102400   83  Linux
/dev/vdb6          825344     1030143      102400   83  Linux
/dev/vdb7         1032192     1236991      102400   83  Linux
/dev/vdb8         1239040     1443839      102400   83  Linux

注意:此时建立的分区均不能用

[root@localhost Desktop]# mount /dev/vdb5 /mnt  ##将vdb5挂载到/mnt中此时挂载失败
mount: /dev/vdb5 is write-protected, mounting read-only
mount: unknown filesystem type '(null)'
[root@localhost Desktop]# mkfs.xfs /dev/vdb5  ##对分区5进行格式化
meta-data=/dev/vdb5              isize=256    agcount=4, agsize=6400 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=25600, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost Desktop]# blkid  ##查看
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs" 
/dev/vdb3: UUID="a944c074-2834-4996-9a75-0217f8612787" TYPE="xfs" 
[root@localhost Desktop]# mount /dev/vdb3 /mnt  ##临时挂载
[root@localhost Desktop]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3166256   7307644  31% /
devtmpfs          469332       0    469332   0% /dev
tmpfs             484920     140    484780   1% /dev/shm
tmpfs             484920   12768    472152   3% /run
tmpfs             484920       0    484920   0% /sys/fs/cgroup
/dev/vdb3          98988    5280     93708   6% /mnt
[root@localhost Desktop]# umount /mnt  ##卸载
[root@localhost Desktop]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3166260   7307640  31% /
devtmpfs          469332       0    469332   0% /dev
tmpfs             484920     140    484780   1% /dev/shm
tmpfs             484920   12768    472152   3% /run
tmpfs             484920       0    484920   0% /sys/fs/cgroup
[root@localhost Desktop]# vim /etc/fstab  ##文件形式进行永久挂载
[root@localhost Desktop]# mount -a
[root@localhost Desktop]# df  ##文件永久挂载成功
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3166264   7307636  31% /
devtmpfs          469332       0    469332   0% /dev
tmpfs             484920     140    484780   1% /dev/shm
tmpfs             484920   12768    472152   3% /run
tmpfs             484920       0    484920   0% /sys/fs/cgroup
/dev/vdb5          98988    5280     93708   6% /mnt
mount: /dev/vdb3 is write-protected, mounting read-only
mount: unknown filesystem type '(null)'
[root@localhost Desktop]# fdisk -l

Disk /dev/vda: 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 label type: dos
Disk identifier: 0x00013f3e

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048    20970332    10484142+  83  Linux

Disk /dev/vdb: 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 label type: dos
Disk identifier: 0xa00b31f2

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
[root@localhost Desktop]# mkfs.xfs /dev/vdb3  ##对分区3进行格式化
meta-data=/dev/vdb3              isize=256    agcount=4, agsize=6400 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=25600, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost Desktop]# blkid  ##查看系统可使用的设备
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs" 
/dev/vdb3: UUID="a944c074-2834-4996-9a75-0217f8612787" TYPE="xfs"  ##格式化以后vdb3可以使用 
[root@localhost Desktop]# mount /dev/vdb3 /mnt  ##临时挂载
[root@localhost Desktop]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3166256   7307644  31% /
devtmpfs          469332       0    469332   0% /dev
tmpfs             484920     140    484780   1% /dev/shm
tmpfs             484920   12768    472152   3% /run
tmpfs             484920       0    484920   0% /sys/fs/cgroup
/dev/vdb3          98988    5280     93708   6% /mnt
[root@localhost Desktop]# umount /mnt  ##卸载
[root@localhost Desktop]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3166260   7307640  31% /
devtmpfs          469332       0    469332   0% /dev
tmpfs             484920     140    484780   1% /dev/shm
tmpfs             484920   12768    472152   3% /run
tmpfs             484920       0    484920   0% /sys/fs/cgroup
[root@localhost Desktop]# vim /etc/fstab  ##文件形式进行永久挂载

在这里插入图片描述

[root@localhost Desktop]# mount -a
[root@localhost Desktop]# df  ##文件永久挂载成功
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 3166264   7307636  31% /
devtmpfs          469332       0    469332   0% /dev
tmpfs             484920     140    484780   1% /dev/shm
tmpfs             484920   12768    472152   3% /run
tmpfs             484920       0    484920   0% /sys/fs/cgroup
/dev/vdb3          98988    5280     93708   6% /mnt

四.swap分区的创建
1.更改swap分区类型82

[root@localhost ~]# fdisk /dev/vdb
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): p

Disk /dev/vdb: 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 label type: dos
Disk identifier: 0x5e1a473e

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448    20971519    10177536    5  Extended
/dev/vdb5          618496      823295      102400   83  Linux
/dev/vdb6          825344     1030143      102400   83  Linux
/dev/vdb7         1032192     1236991      102400   83  Linux
/dev/vdb8         1239040     1443839      102400   83  Linux

Command (m for help): t
Partition number (1-8, default 8): 6
Hex code (type L to list all codes): l  ##查看分区类型

 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): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): p

Disk /dev/vdb: 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 label type: dos
Disk identifier: 0x5e1a473e

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048      206847      102400   83  Linux
/dev/vdb2          206848      411647      102400   83  Linux
/dev/vdb3          411648      616447      102400   83  Linux
/dev/vdb4          616448    20971519    10177536    5  Extended
/dev/vdb5          618496      823295      102400   83  Linux
/dev/vdb6          825344     1030143      102400   82  Linux swap / Solaris  ##swap分区id更改成功
/dev/vdb7         1032192     1236991      102400   83  Linux
/dev/vdb8         1239040     1443839      102400   83  Linux

Command (m for help): wq
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.
[root@localhost ~]# partprobe  ##同步分区
[root@localhost ~]# cat /proc/partitions  ##查看系统所识别的设备
major minor  #blocks  name

 253        0   10485760 vda
 253        1   10484142 vda1
 253       16   10485760 vdb
 253       17     102400 vdb1
 253       18     102400 vdb2
 253       19     102400 vdb3
 253       20          1 vdb4
 253       21     102400 vdb5
 253       22     102400 vdb6
 253       23     102400 vdb7
 253       24     102400 vdb8
[root@localhost ~]# blkid  ##查看可用文件,此时/dev/vdb6不可用
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs" 
/dev/vdb5: UUID="689c5935-b133-46ec-a719-7bc7cb1a99ca" TYPE="xfs" 

2.建立swap分区
(1)建立swap分区

[root@localhost ~]# mkswap /dev/vdb6  ##建立swap分区
Setting up swapspace version 1, size = 102396 KiB
no label, UUID=df730e8e-00c6-4272-95ef-be784dc068bb
[root@localhost ~]# blkid
/dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs" 
/dev/vdb5: UUID="689c5935-b133-46ec-a719-7bc7cb1a99ca" TYPE="xfs" 
/dev/vdb6: UUID="df730e8e-00c6-4272-95ef-be784dc068bb" TYPE="swap"   ##可用

(2)将设备临时添加至swap分区

[root@localhost ~]# swapon -a /dev/vdb6  ##将/dev/vdb6添加到swap分区
[root@localhost ~]# swapon -s  ##查看添加结果成功
Filename				Type		Size	Used	Priority
/dev/vdb6                              	partition	102396	0	-1

(3)将设备永久添加至swap分区

[root@localhost ~]# swapoff /dev/vdb6  ##将/dev/vdb6从swap分区移除
[root@localhost ~]# swapon -s  ##移除成功
[root@localhost ~]# vim /etc/fstab  ##永久添加

在这里插入图片描述
[root@localhost ~]# swapon -a /dev/vdb6
swapon: /dev/vdb6: swapon failed: Device or resource busy
[root@localhost ~]# swapon -a
[root@localhost ~]# swapon -s ##永久添加成功
Filename Type Size Used Priority
/dev/vdb6 partition 102396 0 -1
五.磁盘分区的配额
注意:配额仅针对磁盘分区
1.设备进行有配额的挂载

[root@localhost ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 4184464   6289436  40% /
devtmpfs          469332       0    469332   0% /dev
tmpfs             484920     472    484448   1% /dev/shm
tmpfs             484920   18876    466044   4% /run
tmpfs             484920       0    484920   0% /sys/fs/cgroup
/dev/vdb5          98988    5280     93708   6% /mnt
[root@localhost ~]# umount /mnt
[root@localhost ~]# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 4184464   6289436  40% /
devtmpfs          469332       0    469332   0% /dev
tmpfs             484920     472    484448   1% /dev/shm
tmpfs             484920   18876    466044   4% /run
tmpfs             484920       0    484920   0% /sys/fs/cgroup
[root@localhost ~]# mount -o usrquota /dev/vdb5 /mnt  ##有配额的挂载
[root@localhost ~]# chmod 777 /mnt

2.给指定用户进行配额

[root@localhost ~]# quotaon -uv /dev/vdb5  ##开启配额功能
quotaon: Enforcing user quota already on /dev/vdb5
[root@localhost ~]# edquota -u student  ##指定配额用户

在这里插入图片描述

[root@localhost ~]# su - student
mkdir: cannot create directory '/home/student/.cache': Permission denied

3.测试配额功能是否开启成功

[student@localhost ~]$ dd if=/dev/zero of=/mnt/studentfile bs=1M count=200  ##截取200M给student用户
dd: error writing ‘/mnt/studentfile’: Disk quota exceeded  ##出现报错,给与配额为20M超过20M则报错
21+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.0130351 s, 1.6 GB/s
[student@localhost ~]$ su - 
Password: 
Last login: Thu Apr 18 02:38:02 EDT 2019 from 172.25.254.5 on pts/0
[root@localhost ~]# vim /etc/fstab  ##设定设备是永久性的配额挂载

在这里插入图片描述

[root@localhost ~]# su - student
Last login: Thu Apr 18 03:56:53 EDT 2019 on pts/0
mkdir: cannot create directory '/home/student/.cache': Permission denied
[student@localhost ~]$ dd if=/dev/zero of=/mnt/studentfile bs=1M count=15  ##给与15M小于配额,则截取成功
15+0 records in
15+0 records out
15728640 bytes (16 MB) copied, 0.0178393 s, 882 MB/s
[student@localhost ~]$ df
df: ‘/run/user/0/gvfs’: Permission denied
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/vda1       10473900 4184500   6289400  40% /
devtmpfs          469332       0    469332   0% /dev
tmpfs             484920     472    484448   1% /dev/shm
tmpfs             484920   18876    466044   4% /run
tmpfs             484920       0    484920   0% /sys/fs/cgroup
/dev/vdb5          98988   25768     73220  27% /mnt

六.磁盘的加密与解密
1.对磁盘进行加密

[root@localhost student]# cryptsetup luksFormat /dev/vdb7

WARNING!
========
This will overwrite data on /dev/vdb7 irrevocably.

Are you sure? (Type uppercase yes): yes
[root@localhost student]# cryptsetup luksFormat /dev/vdb7

WARNING!
========
This will overwrite data on /dev/vdb7 irrevocably.

Are you sure? (Type uppercase yes): YES  ##YES必须是大写,否则不会出现设置密码的步骤
Enter passphrase:             ##设置加密密码
Verify passphrase: 

2.对磁盘进行解密并生成解密文件

[root@localhost student]# cryptsetup open /dev/vdb7 westos  ##解密为westos
Enter passphrase for /dev/vdb7: 
[root@localhost student]# ll /dev/mapper/westos
lrwxrwxrwx 1 root root 7 Apr 18 04:28 /dev/mapper/westos -> ../dm-0

3.对磁盘进行格式化并挂载磁盘

[root@localhost student]# mkfs.xfs /dev/mapper/westos  ##对westos进行格式化 
meta-data=/dev/mapper/westos     isize=256    agcount=4, agsize=6272 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0
data     =                       bsize=4096   blocks=25088, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal log           bsize=4096   blocks=853, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
[root@localhost student]# mount /dev/mapper/westos /mnt

4.测试加密与解密是否设置成功

[root@localhost student]# touch /mnt/file{1..10}
[root@localhost student]# ls /mnt
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9
[root@localhost student]# umount /mnt
[root@localhost student]# cryptsetup close westos
[root@localhost student]# ls /mnt  ##卸载关闭解密虚拟盘此时无法看到/mnt下的文件
[root@localhost student]# cryptsetup open /dev/vdb7 westos  ##打开解密文件
Enter passphrase for /dev/vdb7:   ##输入密码
[root@localhost student]# mount /dev/mapper/westos /mnt  ##进行挂载
[root@localhost student]# ls /mnt  ##可以查看到/mnt下的文件,则设置成功
file1  file10  file2  file3  file4  file5  file6  file7  file8  file9

5.加密磁盘的开机自动挂载

[root@localhost student]# vim /etc/fstab  ##设置永久挂载

在这里插入图片描述

[root@localhost student]# vim /root/diskpass  ##设置解密密码
[root@localhost student]#chmod 600 /root/diskpass

在这里插入图片描述

[root@localhost student]# vim /etc/crypttab  ##

在这里插入图片描述
[root@localhost student]# reboot
注意:重启无报错则成功
6.删除加密设备

[root@localhost ~]# vim /etc/fstab  ##注释之前的挂载
[root@localhost ~]# ll /etc/fstab
-rw-r--r--. 1 root root 314 Apr 16 03:03 /etc/fstab
[root@localhost ~]# rm -fr /etc/.fstab.swp
[root@localhost ~]# vim /etc/crypttab  ##删除配置文件内容
[root@localhost ~]# > /etc/crypttab
[root@localhost ~]# rm -fr /root/diskpass
[root@localhost ~]# df
Filesystem         1K-blocks    Used Available Use% Mounted on
/dev/vda1           10473900 3155120   7318780  31% /
devtmpfs              469332       0    469332   0% /dev
tmpfs                 484920      84    484836   1% /dev/shm
tmpfs                 484920   12808    472112   3% /run
tmpfs                 484920       0    484920   0% /sys/fs/cgr
/dev/mapper/westos    506540   25656    480884   6% /mnt
[root@localhost ~]# umount /mnt  ##卸载
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值