磁盘的相关知识

1、磁盘的命名

 2、硬盘的分区方式

MBR 分区方式(小于2T),使用fdisk命令:4个主分区或者3个主分区+一个扩展分区(N各逻辑分区)

GPT分区方案(大于2T),使用gdisk(parted):128个主分区

3、fdisk分区磁盘

首先查看磁盘的分区情况 lsblk

[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0    2G  0 disk 
sr0              11:0    1  4.4G  0 rom  

查看正在挂载的设备情况

[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 898M     0  898M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.5M  901M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root   17G  1.4G   16G   9% /
/dev/sda1               1014M  150M  865M  15% /boot
tmpfs                    182M     0  182M   0% /run/user/0

开始分区

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

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): 1
First sector (2048-4194303, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4194303, default 4194303): +1G
Partition 1 of type Linux and of size 1 GiB is set

Command (m for help): p

Disk /dev/sdb: 2147 MB, 2147483648 bytes, 4194304 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: 0xfd063994

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   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): 2
First sector (2099200-4194303, default 2099200): 
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-4194303, default 4194303): +500M
Partition 2 of type Linux and of size 500 MiB is set

Command (m for help): p

Disk /dev/sdb: 2147 MB, 2147483648 bytes, 4194304 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: 0xfd063994

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux
/dev/sdb2         2099200     3123199      512000   83  Linux

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

Calling ioctl() to re-read partition table.
Syncing disks.

分区后查看,其中sdb3是扩展分区,

[root@localhost ~]# lsblk
NAME            MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda               8:0    0   20G  0 disk 
├─sda1            8:1    0    1G  0 part /boot
└─sda2            8:2    0   19G  0 part 
  ├─centos-root 253:0    0   17G  0 lvm  /
  └─centos-swap 253:1    0    2G  0 lvm  [SWAP]
sdb               8:16   0    2G  0 disk 
├─sdb1            8:17   0    1G  0 part 
├─sdb2            8:18   0  500M  0 part 
├─sdb3            8:19   0    1K  0 part 
└─sdb5            8:21   0  522M  0 part 
sr0              11:0    1  4.4G  0 rom  

 刷新分区表

[root@localhost ~]# partx -a /dev/sdb
partx: /dev/sdb: error adding partitions 1-3
partx: /dev/sdb: error adding partition 5

 格式化

[root@localhost ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 262144 blocks
13107 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

挂载:

[root@localhost ~]# mount /dev/sdb1 u01
[root@localhost ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
devtmpfs                 898M     0  898M   0% /dev
tmpfs                    910M     0  910M   0% /dev/shm
tmpfs                    910M  9.6M  901M   2% /run
tmpfs                    910M     0  910M   0% /sys/fs/cgroup
/dev/mapper/centos-root   17G  1.4G   16G   9% /
/dev/sda1               1014M  150M  865M  15% /boot
tmpfs                    182M     0  182M   0% /run/user/0
/dev/sdb1                976M  2.6M  907M   1% /root/u01

重新挂载一个已经挂载的设备

[root@localhost ~]# mount -o remount,ro /dev/sdb1

也可以使用UUID挂载

[root@localhost ~]# blkid /dev/sdb5
/dev/sdb5: UUID="35ead5d1-7cb8-41de-89e1-f4676fa324d1" TYPE="ext4" 
[root@localhost ~]# mount  UUID="35ead5d1-7cb8-41de-89e1-f4676fa324d1" u05

4、主分区、扩展分区、逻辑分区

   一块硬盘可以不要主分区,可以直接分为扩展分区和逻辑分区

  • 主分区是硬盘的启动分区,他是独立的,也是硬盘的第一个分区。
  • 分出主分区后,其余的部分可以分成扩展分区,一般是剩下的部分全部分成扩展分区,也可以不全分,那剩的部分就浪费了。
  • 但扩展分区是不能直接用的,他是以逻辑分区的方式来使用的,所以说扩展分区可分成若干逻辑分区。他们的关系是包含的关系,所有的逻辑分区都是扩展分区的一部分 

 5、逻辑卷管理

创建PV

[root@localhost ~]# pvcreate /dev/sdc1
  Physical volume "/dev/sdc1" successfully created.
[root@localhost ~]# pvcreate /dev/sdc2
  Physical volume "/dev/sdc2" successfully created.

创建VG

[root@localhost ~]# vgcreate vg01 /dev/sdc1 /dev/sdc2
  Volume group "vg01" successfully created

创建LV

[root@localhost ~]# lvcreate -n lv01 -L 1.9G vg01
  Rounding up size to full physical extent 1.90 GiB
  Logical volume "lv01" created.

格式化

[root@localhost ~]# mkfs.ext4 /dev/vg01/lv01
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
124672 inodes, 498688 blocks
24934 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=511705088
16 block groups
32768 blocks per group, 32768 fragments per group
7792 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done 

挂载

[root@localhost ~]# mount /dev/vg01/lv01 u06

6、软连接

创建软连接

ln   -s     目标文件      连接文件

[root@localhost ~]# touch file1
[root@localhost ~]# ln -s file1 test1

查看链接文件

[root@localhost ~]# ll
lrwxrwxrwx. 1 root root       5 Jun 16 04:24 test1 -> file1

修改原文件,连接文件会变,修改连接文件,原文件也会变

[root@localhost ~]# echo hello >> file1
[root@localhost ~]# cat test1
hello
[root@localhost ~]# echo world >> test1

原文件和链接文件是两个不同的文件

[root@localhost ~]# ll -i
33940254 -rw-r--r--. 1 root root      12 Jun 16 04:25 file1
33940255 lrwxrwxrwx. 1 root root       5 Jun 16 04:24 test1 -> file1

删除原文件,链接文件变为无效文件

[root@localhost ~]# rm file1
rm: remove regular file ‘file1’? y

 删除目录的软链接,一定要注意软链接的目录后边不能加斜杠,如果加了会删除目录里边的内容

[root@localhost link]# rm -rf test2

也可以用unlink命令后面跟软链接的名称

[root@localhost link]# unlink testlink1

7、硬连接

硬链接特点:

  •     文件内容相同;
  •      一个文件改变另一个文件也改变;
  •     删除一个文件不会影响其他文件;
  •     inode号相同
  •     Linux中,不能对目录创建硬链接。

创建硬连接

[root@localhost link]# touch file1
[root@localhost link]# ln file1 test1
[root@localhost link]# ll -i
total 0
16785871 -rw-r--r--. 2 root root 0 Jun 16 05:04 file1
16785871 -rw-r--r--. 2 root root 0 Jun 16 05:04 te

 

posted @ 2023-06-15 17:37  中仕  阅读(10)  评论(0编辑  收藏  举报