[Command] Linux disk partition, format, mount command, create a swap partition (fdisk, mkfs, mount, umount)

Required to command

command

effect

fdisk -l View the current partition status
fdisk /dev/vda Let into the hard disk partition mode, the partition of the disk in the / dev vda of this disk

partprobe

The partition table information read the kernel
mkfs.ext4 /dev/vda1 The disk format at vda / dev format type of ext4
mount /dev/vda1 /thy The / dev / vda1 mounted to the thy
umount / dev / vda1 or umount / thy Uninstall the mount / dev / vda1 disk is mounted as a directory interface thy
mount  Mount View detailed information on the current disk
df -h View disk information
blkid Check the corresponding partition UUID number
lsblk View the current hard disk partition

 

First, disk partition

 1.1 when the partition needs to check the partitions:

1) fdisk -l View

PS: Note that the above "*" indicates that the disk is a system boot disk, the boot system for guiding i.e.

start, end represents the sector where the partition start position and end sector.
blocks: block information indicating a block which has a plurality of sectors.
Id: represents the disk number
system: Linux partition information represents a system partition.

2) lsblk view the current hard disk partition

1.2. Fdisk command to partition

# 指定要分区的硬盘,进入分区模式
[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): m    #按m键查看帮助
Command action
   a   开关可启动标志
   b   编辑嵌套的BSD磁盘标签
   c  开关dos兼容性标志
   d   删除一个分区
   g   新建一个GPT分区表
   G   新建一个空的GPT (RIX (SGI) )分区表
   l   列出已知分区类型
   m   查看帮助菜单
   n   添加一个新分区
   o   新建一个新的DOS分区表
   p   打印分区表
   q   不保存退出
   s   新建一个空的Sun分区表
   t   改变系统id号,更改分区类型
   u   更改,显示/记录 单位
   v   检查分区表
   w   保存退出
   x   更多功能,仅限专业人员

1.3. 开始创建分区

# 开始创建分区
Command (m for help): n    # 输入n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)    # p为主分区
   e   extended    # e为扩展分区
Select (default p):p    # 这里我选择新建主分区,回车
Partition number (2-4, default 2):2    # 选择分区编号2,回车
第一个扇区(2048-4194303,默认2048):  回车  # 这里回车即可,一般为上一个分区的结束大小作为此分区的开始大小
上一个扇区,+sectors或+size{K,M,G,T,P}(2048-4194303,默认4194303): +1024M   # 这里可以回车,回车代表将所有空间大小分配给这个磁盘,也可以设置分区的大小,然后回车
Command (m for help): p    # 这样就创建好了分区,可以使用p查看新建的分区
Command (m for help): w    # 最后要记得保存,不然上面新建的分区是无效的哈

# 新建分区之后。可能会发现找不到这个分区,这时可以使用下面这个命令
[root@VM_0_10_centos ~]# partprobe

# 接下来就是格式化

二、磁盘格式化

  格式化就是将分区格式化为不同的文件系统。文件系统:指操作系统用于明确存储设备或分区上的文件的方法和数据结构:即在存储设备上组织文件的方法。就好比一个教室,同学们的坐的位置总是与桌子凳子排列的方式有关系。桌子,凳子怎么摆放,就导致了同学坐的位置在哪里。文件系统存放数据也是这么个道理。

  Linux下的文件类型有ext2、ext3、ext4、xfs等等,我们可以使用命令:mkfs. 然后用按TABTAB来查看都有哪些文件类型。

2.1. 磁盘格式化

  对上面创建的分区进行格式化操作。

# 输入一下命令,格式化为ext4的文件系统,后面的分区为你自己的分区,然后回车即可
[root@VM_0_10_centos ~]# mkfs.ext4 /dev/vda1
或
[root@VM_0_10_centos ~]# mkfs -t ext4 /dev/vda1

# 格式化完成,可以查看该分区的类型
[root@VM_0_10_centos ~]# ll /dev/vda1 
brw-rw---- 1 root disk 253, 1 Nov 27 09:16 /dev/vda1
PS:vda1为一个块设备文件。253表示主设备号,1表示的为从设备号。

# 接下来是挂载,挂载之后才能使用哈

三、磁盘挂载

  我们要在vda1中写入文件时,首先vda1要先建立一个联系,这个联系就是一个目录。建立联系的过程我们叫做挂载

  挂载点目录linux系统上有两个目录是进行挂载的。也称之为挂载点

  临时挂载目录:将指定的一个目录作为挂载点目录时,如果挂载点的目录有文件,那么文件会被隐藏。因此当我们需要挂载目录时,最好新建一个空文件夹来作为挂在点目录。(重启后失效

3.1 临时挂载

# 首先再根目录下创建挂载点
[root@VM_0_10_centos ~]# mkdir /thy

# 然后挂载
[root@VM_0_10_centos ~]# mount /dev/vda1 /thy


# 挂载之后,/thy目录下会有一个lost+found文件
# 这个时候你可以往这个目录写入文件操作。

# 卸载挂载点使用umount命令
[root@VM_0_10_centos ~]# umount /dev/vda1
或
[root@VM_0_10_centos ~]# umount /thy
PS:注意卸载的时候,需要退出该目录去卸载。不然无法进行卸载,会报错“设备正忙”。另外,挂载点创建的文件是保存在vda1设备中的,,而不是保存在/thy目录下。再次挂载之后创建的文件还是会存在

3.2 永久挂载且开机自动挂载

也可以使用blkid来查看分区的UUID,UUID唯一标识每一个分区,防止错误的挂载。除此之外,还会显示分区的类型。

 

# 查看到当前系统上所有磁盘的挂载信息
[root@VM_0_10_centos ~]# mount
......
/dev/vda1 on / type ext4 (rw,noatime,data=ordered)
......

# 将挂载写入配置文件(第一个0为不备份,第二个0为不检查,这里也可以通过blkid查看的uuid进行挂载。)
[root@VM_0_10_centos ~]# less /etc/fstab
/dev/vda1 /thy ext4 defaults 0 0

# 设置自动挂载(让内核读取该文件,不然需要reboot之后才能挂载上去)
[root@VM_0_10_centos ~]# mount -a

四、创建交换分区

4.1 分区之前查看内存,磁盘情况

 

 

4.2 创建交换分区

方法一:通过创建分区创建交换分区

  新加一块磁盘用于交换分区/dev/vdb(如果vda还有分区空间,可以直接从vda创建分区)

分区操作:

  先创建扩展分区,从扩展分区创建逻辑分区

[root@VM_0_10_centos ~]# 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.

Command (m for help): n  # 新建分区
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e  
Partition number (2-4, default 2): 4    # 创建扩展分区
First sector (20973568-62914559, default 20973568): 
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-62914559, default 62914559): +512M
Partition 4 of type Linux and of size 10 GiB is set
...
# 创建逻辑分区:n ==》l ==》回车 ==》+512M ==》回车
...
Command (m for help): t # 修改系统ID Partition number (2,4, default 4): 5 # 选择刚刚创建的逻辑分区序号 Hex code (type L to list all codes): 82 # 改成swap的ID Changed type of partition 'Linux' to 'Linux swap / Solaris' Command (m for help): p Disk /dev/xvdb: 32.2 GB, 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 Disk label type: dos Disk identifier: 0x25a2caf5 Device Boot Start End Blocks Id System /dev/vda1 2048 20973567 10485760 83 Linux /dev/vda2 20973568 41945087 10485760 82 Linux swap / Solaris Command (m for help): w # 保存分区 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.

# 强制内核更新分区表:
partprobe 或 partx -a /dev/vda 
# 开始创建交换分区: mkswap /dev/vda2
# 开启交换分区:
swapon /dev/vda2

  # 加入开机自启:vim /etc/fstab 尾部增加:

  UUID=XXXX swap swap defaults 0 0

  # 运行:swapon –a

# 查看交换分区大小:swapon -s

PS:如果是用上面的分区,需要使用vda2,这里用的是新增的磁盘

创建交换分区:

 

# 格式化
mkswap /dev/vdb

# 设置为交换分区
swapon /dev/vdb

# 设置为重启也生效,修改配置文件
vi /etc/fstab
/dev/vdb swap                    swap    defaults        0 0

# 取消交换分区
swapoff /dev/vdb
或
swapoff -a

 

方法二:通过创建交换分区文件创建交换分区

 

# 创建交换分区文件
[root@VM_0_10_centos ~]# dd if=/dev/zero of=/swapfile bs=1M count=512
512+0 records in
512+0 records out
524288 bytes (537 MB) copied, 2.81936 s, 190 MB/s# 格式化交换分区文件
[root@VM_0_10_centos ~]# mkswap /swapfile 
Setting up swapspace version 1, size = 524284 KiB
no label, UUID=ba4d9309-8e01-43bc-b443-8dcca750ae99

# 设置为交换分区
[root@VM_0_10_centos ~]# swapon /swapfile
swapon: /swapfile: insecure permissions 0644, 0600 suggested.
PS:这个提示是建议将交换分区的权限设置为600,这里没关系

# 使用free -m查看交换分区
[root@VM_0_10_centos ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:           1838         478          77           1        1282        1160
Swap:           511           0         511

# 关闭交换分区
[root@VM_0_10_centos ~]# swapoff /swapfile

 

 

 

参考博客:https://www.cnblogs.com/liuyisai/p/5333865.html

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/HeiDi-BoKe/p/11936998.html