今天在虚拟机内玩了一下centos7 添加虚拟硬盘并分区,结束之后总结并写出来分享下。
先添加一块虚拟硬盘默认20G就可以了,最好是虚拟机关机的情况下去做。不然添加硬盘后有可能虚拟机识别不了,还需要重新启动虚拟机。
图一:
添加之后查看系统盘,sda是我的系统盘。sdb是刚刚添加的硬盘。
[root@linuxprobe Desktop]# ls -l /dev/sd*
brw-rw----. 1 root disk 8, 0 Jul 5 2019 /dev/sda
brw-rw----. 1 root disk 8, 1 Jul 5 2019 /dev/sda1
brw-rw----. 1 root disk 8, 2 Jul 5 2019 /dev/sda2
brw-rw----. 1 root disk 8, 16 Jul 5 2019 /dev/sdb
继续对磁盘进行分区,关于分区理论如果不是太明白的话推荐参考一下鸟哥的linux私房菜。里面介绍的很详细。
[root@linuxprobe Desktop]# 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 0x87d45f61.
Command (m for help): m **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 **n参数用于创建新的分区**
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p **参数p是创建主分区,e是创建扩展分区**
Partition number (1-4, default 1): **输入创建分区编号,默认为1**
First sector (2048-41943039, default 2048): **输入开始扇区,此处无需输入默认即可**
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +3G **输入+空间大小**
Partition 1 of type Linux and of size 3 GiB is set
Command (m for help): p **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: 0x87d45f61
Device Boot Start End Blocks Id System
/dev/sdb1 2048 6293503 3145728 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 (6293504-41943039, default 6293504):
Using default value 6293504
Last sector, +sectors or +size{K,M,G} (6293504-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: 0x87d45f61
Device Boot Start End Blocks Id System
/dev/sdb1 2048 6293503 3145728 83 Linux
/dev/sdb2 6293504 16779263 5242880 83 Linux
Command (m for help): n
Partition type:
p primary (2 primary, 0 extended, 2 free)
e extended
Select (default p): e
Partition number (3,4, default 3):
First sector (16779264-41943039, default 16779264):
Using default value 16779264
Last sector, +sectors or +size{K,M,G} (16779264-41943039, default 41943039):
Using default value 41943039
Partition 3 of type Extended and of size 12 GiB is set
Command (m for help): n
Partition type:
p primary (2 primary, 1 extended, 1 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (16781312-41943039, default 16781312):
Using default value 16781312
Last sector, +sectors or +size{K,M,G} (16781312-41943039, default 41943039): +3G
Partition 5 of type Linux and of size 3 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: 0x87d45f61
Device Boot Start End Blocks Id System
/dev/sdb1 2048 6293503 3145728 83 Linux
/dev/sdb2 6293504 16779263 5242880 83 Linux
/dev/sdb3 16779264 41943039 12581888 5 Extended
/dev/sdb5 16781312 23072767 3145728 83 Linux
Command (m for help): n
Partition type:
p primary (2 primary, 1 extended, 1 free)
l logical (numbered from 5)
Select (default p): l
Adding logical partition 6
First sector (23074816-41943039, default 23074816):
Using default value 23074816
Last sector, +sectors or +size{K,M,G} (23074816-41943039, default 41943039):
Using default value 41943039
Partition 6 of type Linux and of size 9 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: 0x87d45f61
Device Boot Start End Blocks Id System
/dev/sdb1 2048 6293503 3145728 83 Linux
/dev/sdb2 6293504 16779263 5242880 83 Linux
/dev/sdb3 16779264 41943039 12581888 5 Extended
/dev/sdb5 16781312 23072767 3145728 83 Linux
/dev/sdb6 23074816 41943039 9434112 83 Linux
Command (m for help): w **w参数保存并退出**
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
下面是最终分区效果,上图片更直观一些
图二:
下面用mkfs命令进行格式化,注意:
1.格式化命令要具体到sdb的下一级,否则出错
2.要先分区然后再格式化
[root@linuxprobe Desktop]# mkfs. **用命令补全功能,双击两下tab键,显示所有可用格式**
mkfs.btrfs mkfs.ext2 mkfs.ext4 mkfs.minix mkfs.vfat
mkfs.cramfs mkfs.ext3 mkfs.fat mkfs.msdos mkfs.xfs
[root@linuxprobe Desktop]# 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
196608 inodes, 786432 blocks
39321 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=805306368
24 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912
Allocating group tables: done
Writing inode tables: done
Creating journal (16384 blocks): done
Writing superblocks and filesystem accounting information: done
用同样的方法格式化其它分区,这里就不演示了
格式化后需要将其挂载到系统文件夹后才可以使用,创建用于挂载分区的文件夹
[root@linuxprobe media]# mkdir 1 2 5 6
[root@linuxprobe media]# ls
1 2 5 6
执行挂载操作
[root@linuxprobe media]# mount /dev/sdb1 /media/1
[root@linuxprobe media]# mount /dev/sdb2 /media/2
[root@linuxprobe media]# mount /dev/sdb5 /media/5
[root@linuxprobe media]# mount /dev/sdb6 /media/6
挂载成功后桌面自动显示已挂载上的磁盘如图
图三:
如果没有将挂载信息写入到系统的配置文件中。将导致下次系统重启失效,还需要重新挂载。
所以继续上面步骤修改配置文件
[root@linuxprobe media]# vim /etc/fstab
[root@linuxprobe media]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel_linuxprobe-root 18G 3.0G 15G 17% /
devtmpfs 481M 0 481M 0% /dev
tmpfs 490M 140K 490M 1% /dev/shm
tmpfs 490M 7.1M 483M 2% /run
tmpfs 490M 0 490M 0% /sys/fs/cgroup
/dev/sda1 497M 119M 379M 24% /boot
/dev/sdb1 2.9G 9.0M 2.8G 1% /media/1
/dev/sdb2 4.8G 20M 4.6G 1% /media/2
/dev/sdb5 2.9G 9.0M 2.8G 1% /media/5
/dev/sdb6 8.8G 37M 8.3G 1% /media/6
接下来就可以使用已挂载的设备了看看效果吧
[root@linuxprobe media]# cp /etc/* /media/1
这里就只看看大小吧,可以使用ls -l /media/1 查看具体文件。由于信息太多,就偷个懒吧。
[root@linuxprobe media]# du -sh /media/*
1.3M /media/1
20K /media/2
20K /media/5
20K /media/6
一个新的磁盘分区挂载完成了。
设置交换分区与上例基本一致,区别是分完区后格式化有专用命令。
进行到 图二: 步骤后,格式化分区使用命令 mkswap /dev/sdb1(这里把sdb1的空间作为交换分区)
[root@linuxprobe media]# mkswap /dev/sdb1
mkswap: /dev/sdb1: warning: wiping old ext4 signature.
Setting up swapspace version 1, size = 3145724 KiB
no label, UUID=52ddae81-43b8-4f04-b2fc-a693e14691ea
[root@linuxprobe media]# free -m
total used free shared buffers cached
Mem: 979 779 200 4 0 85
-/+ buffers/cache: 693 286
Swap: 2047 11 2036 **交换分区大小注意查看区别**
开启交换分区并查看是否成功
[root@linuxprobe media]# swapon /dev/sdb1
[root@linuxprobe media]# free -m
total used free shared buffers cached
Mem: 979 806 173 4 0 109
-/+ buffers/cache: 695 284
Swap: 5119 10 5109 **交换分区大小**
将相关信息写入到配置文件