Linux磁盘分区、挂载
Linux磁盘分区
原理介绍
Linux来说无论有几个分区,分给哪一个目录使用(挂载),他归根结底就只有一个根目录,一个独立且唯一的文件结构,Linux中每个分区都是用来组成整个文件系统的一部分的。
Linux采用了一种叫 “载入” 的处理方法,他的整个文件系统中包含了一整套的文件和目录,且将一个分区和一个目录联系起来。
简单来说,一个分区代表了一块具体的存储空间,Linux通过挂载的技术将这块空间挂载了一个具体的目录下面。
使用lsblk指令查看当前系统的分区情况
[root@k8smaster ~]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
vda
└─vda1 ext4 1114fe9e-2309-4580-b183-d778e6d97397 /
#vda是硬盘的名字
[root@k8smaster ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 40G 0 disk
└─vda1 253:1 0 40G 0 part /
挂载
需求:当目前的硬盘不够用时,就要尝试给Linux新增硬盘了,并且挂载到某一个具体的目录下面/home/newdisk
原理:新增一块硬盘sdb1,并挂载到/home/newdisk,此时对/home/newdisk的操作其实就是在操作sdb1这块硬盘。
分区
分区命令 fdisk /dev/sdb
[root@k8smaster /]# fdisk /dev/vda
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.
#输入:m,获取帮助,如下,我们看到输入n是:添加一个新的分区
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
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
#如下,输入p是创建一个主分区,e是创建扩展分区,我们创建主分区
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
#输入分区编号,默认为2,默认就行,回车
Partition number (2-4, default 2):
#起始扇区:默认83875840,默认就行,回车
First sector (83875365-83886079, default 83875840):
Using default value 83875840
#最后的扇区:默认83886079,默认就行,回车
Last sector, +sectors or +size{K,M,G} (83875840-83886079, default 83886079):
Using default value 83886079
Partition 2 of type Extended and of size 5 MiB is set
#输入w: write table to disk and exit
Command (m for help):
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@k8smaster /]# shutdown -r now
Session was closed
#此时可以看到创建了一个扩展分区
[root@k8smaster ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 40G 0 disk
├─vda1 253:1 0 40G 0 part /
└─vda2 253:2 0 1K 0 part
#删除分区
[root@k8smaster /]# fdisk /dev/vda
Command (m for help): m
Command (m for help): d
Command (m for help): w
配置挂载
#格式化磁盘分区
[root@k8smaster /]# mkfs -t ext4 /dev/vda2
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
1280 inodes, 5120 blocks
256 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=5242880
1 block group
8192 blocks per group, 8192 fragments per group
1280 inodes per group
Allocating group tables: done
Writing inode tables: done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: done
#把分区/dev/vda2挂在到/chentest目录上
#此时操作/chentest目录就是操作/dev/vda2分区
[root@k8smaster /]# mount /dev/vda2 /chentest
#挂载成功
[root@k8smaster /]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
vda 253:0 0 40G 0 disk
├─vda1 253:1 0 40G 0 part /
└─vda2 253:2 0 5M 0 part /chentest
[root@k8smaster /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 7.3G 31G 20% /
devtmpfs 869M 0 869M 0% /dev
tmpfs 879M 0 879M 0% /dev/shm
tmpfs 879M 472K 878M 1% /run
tmpfs 879M 0 879M 0% /sys/fs/cgroup
tmpfs 176M 0 176M 0% /run/user/0
/dev/vda2 3.9M 53K 3.5M 2% /chentest


1万+

被折叠的 条评论
为什么被折叠?



