Linux系统新加硬盘的分区、格式化、挂载、添加交换分区

Linux系统新加硬盘的分区、格式化、挂载、添加交换分区

一、添加新硬盘
虚拟机关机—点击“编辑虚拟机设置”----添加----硬盘----选择 推荐的SCSI(S)----创建新虚拟磁盘----选择最大磁盘大小20G,将虚拟磁盘拆分成多个文件
----指定磁盘文件选择默认----完成
Linux硬盘都会以“/dev/sd”开头的,系统采用a~p来代表16块不同的硬盘(默认从a开始分配)
硬盘主分区或扩展分区的编号从1开始,到4结束。
逻辑分区从编号5开始。
所以这块新加的硬盘应该会被保存为dev/sdb这个文件
在这里插入图片描述

二、
#cd/dev
#ls ./查看确实有dev/sdb这个硬盘设备配置文件

三、分区,fdisk命令用于管理磁盘分区
[root@bogon liveuser]# fdisk /dev/sdb
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 0x1b7cde2a.

Command (m for help): p 查看分区信息

Disk /dev/sdb: 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: 0x1b7cde2a

Device Boot Start End Blocks Id System

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 主分区的编号从1开始
First sector (2048-41943039, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +2G 创建一个2G的硬盘分区
Partition 1 of type Linux and of size 2 GiB is set

Command (m for help): p

Disk /dev/sdb: 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: 0x5b0d5da9

Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux

Command (m for help): w 保存并退出
The partition table has been altered!

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

四、分区信息同步到linux内核,这里表示已经同步了,如果没有同步连续两次执行“partprobe”命令进行同步。
[root@bogon /]# file /dev/sdb1
/dev/sdb1: block special

五、格式化这个新的硬盘分区
[root@bogon /]# mkf
mkfifo mkfs mkfs.ext2 mkfs.fat mkfs.vfat
mkfontdir mkfs.btrfs mkfs.ext3 mkfs.minix mkfs.xfs
mkfontscale mkfs.cramfs mkfs.ext4 mkfs.msdos
[root@bogon /]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=131072 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=524288, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
log =internal log bsize=4096 blocks=2560, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0

六、先创建一个用于挂载设备的挂载点目录newstorage,使用mount命令将存储设备与挂载点进行关联,最后用df -h命令查看挂载状态和硬盘使用量信息
[root@bogon /]# mkdir /newstorage
[root@bogon /]# mount /dev/sdb1 /newstorage
[root@bogon /]# df -h
Filesystem Size Used Avail Use% Mounted on
devtmpfs 955M 0 955M 0% /dev
tmpfs 991M 4.0K 991M 1% /dev/shm
tmpfs 991M 19M 972M 2% /run
tmpfs 991M 0 991M 0% /sys/fs/cgroup
/dev/sr0 1.5G 1.5G 0 100% /run/initramfs/live
/dev/mapper/live-rw 7.8G 4.5G 3.3G 58% /
tmpfs 991M 24K 991M 1% /tmp
varcacheyum 991M 93M 898M 10% /var/cache/yum
vartmp 991M 0 991M 0% /var/tmp
tmpfs 199M 52K 198M 1% /run/user/1000
/dev/sdb1 2.0G 33M 2.0G 2% /newstorage

七、使用mount命令挂载的设备文件在系统下一次重启时会失效,想挂载永久有效,需要把信息写入到配置文件中。

vim /etc/fstab

/dev/root / ext4 defaults,noatime 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs defaults 0 0
proc /proc proc defaults 0 0
sysfs /sys sysfs defaults 0 0
/dev/sdb1 /newstorage xfs defaults 0 0

八、添加交换分区
SWAP交换分区是将内存中暂时不常用的数据临时存放到硬盘里,其设计目的是为了解决真实物理内存不足的问题,建议为真实物理内存的2倍。
1、继续分区
[root@bogon /]# fdisk /dev/sdb
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): p
Partition number (2-4, default 2): 2
First sector (4196352-41943039, default 4196352):
Using default value 4196352
Last sector, +sectors or +size{K,M,G} (4196352-41943039, default 41943039): +5G
Partition 2 of type Linux and of size 5 GiB is set

Command (m for help): p

Disk /dev/sdb: 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: 0x5b0d5da9

Device Boot Start End Blocks Id System
/dev/sdb1 2048 4196351 2097152 83 Linux
/dev/sdb2 4196352 14682111 5242880 83 Linux

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.

2、同步到内核
[root@bogon dev]# file /dev/sdb2
/dev/sdb2: cannot open (No such file or directory)
[root@bogon dev]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Error: The partition’s data region doesn’t occupy the entire partition.
[root@bogon dev]# partprobe
Warning: Unable to open /dev/sr0 read-write (Read-only file system). /dev/sr0 has been opened read-only.
Error: The partition’s data region doesn’t occupy the entire partition.
[root@bogon dev]# file /dev/sdb2
/dev/sdb2: block special

这里用到了partprobe命令进行同步到linux内核,
#cd/dev
#ls ./查看确实有dev/sdb2这个硬盘设备配置文件

3、格式化
[root@bogon dev]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 5242876 KiB
no label, UUID=ea486a15-91e1-4742-b4b3-815d3c842862

4、使用swapon命令吧swap分区挂载到系统中,使用free -m命令查看交换分区的大小变化(增加了5119MB)
[root@bogon dev]# free -m
total used free shared buff/cache available
Mem: 1980 742 71 125 1166 880
Swap: 0 0 0

[root@bogon dev]# swapon /dev/sdb2
[root@bogon dev]# free -m
total used free shared buff/cache available
Mem: 1980 744 68 125 1167 878
Swap: 5119 0 5119

5、写入配置文件,系统重启后不会丢失

vim /etc/fstab

/dev/root / ext4 defaults,noatime 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
tmpfs /dev/shm tmpfs defaults 0 0
proc /proc proc de
sysfs /sys sysfs defaults 0 0
/dev/sdb1 /newstorage xfs defaults 0 0
/dev/sdb2 swap swap defaults 0 0

发布了8 篇原创文章 · 获赞 0 · 访问量 558

猜你喜欢

转载自blog.csdn.net/m0_46121636/article/details/104986002
今日推荐