云主机初始化swap与数据盘

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了云主机初始化swap与数据盘相关的知识,希望对你有一定的参考价值。

背景   

     当前市面上大部分的云服务器产品,在购买Linux服务器并启动后,通常只帮我们挂载了系统盘到/目录。我们所购买的数据盘并没有帮我们挂载到系统。查看内存配置,一般swap也为0。

    这里我们可以利用购买的数据盘来创建swap分区与数据分区,并将他们挂载到系统中去。


操作

 1,查看当前的磁盘,如下,/dev/vda为系统盘,/dev/vdb为数据盘:

# fdisk -l 
Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x0008e9bc
   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200   104857566    51379183+  83  Linux
Disk /dev/vdb: 429.5 GB, 429496729600 bytes, 838860800 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


2,对/dev/vdb进行分区操作,分割16G空间做swap,剩余空间做数据盘。

# 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.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0xb07be21f.
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): 
First sector (2048-838860799, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-838860799, default 838860799): +16G
Partition 1 of type Linux and of size 16 GiB is set
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 (33556480-838860799, default 33556480): 
Using default value 33556480
Last sector, +sectors or +size{K,M,G} (33556480-838860799, default 838860799): 
Using default value 838860799
Partition 2 of type Linux and of size 384 GiB is set
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
# fdisk -l

Disk /dev/vda: 53.7 GB, 53687091200 bytes, 104857600 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: 0x0008e9bc

   Device Boot      Start         End      Blocks   Id  System
/dev/vda1   *        2048     2099199     1048576   83  Linux
/dev/vda2         2099200   104857566    51379183+  83  Linux

Disk /dev/vdb: 429.5 GB, 429496729600 bytes, 838860800 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: 0x53b4d701

   Device Boot      Start         End      Blocks   Id  System
/dev/vdb1            2048    33556479    16777216   83  Linux
/dev/vdb2        33556480   838860799   402652160   83  Linux

可以看到创建了2个分区/dev/vdb1,/dev/vdb2。


3,创建swap分区,并启用:

# mkswap /dev/vdb1
# swapon /dev/vdb1


4,把/dev/vdb2格式化,并挂载到/data目录下:(这里格式成xfs文件系统)

# mkdir -p /data
# mkfs.xfs /dev/vdb2
# mount /dev/vdb2 /data


5,检查是否生效:

# df -kh
Filesystem      Size  Used Avail Use% Mounted on
/dev/vda2        49G  1.7G   45G   4% /
devtmpfs        7.8G     0  7.8G   0% /dev
tmpfs           7.8G     0  7.8G   0% /dev/shm
tmpfs           7.8G   25M  7.8G   1% /run
tmpfs           7.8G     0  7.8G   0% /sys/fs/cgroup
/dev/vda1       976M  146M  764M  16% /boot
tmpfs           1.6G     0  1.6G   0% /run/user/0
/dev/vdb2       384G   33M  384G   1% /data
# free -m
              total        used        free      shared  buff/cache   available
Mem:          15885         393       14628          24         863       15158


6,把磁盘挂载信息写进fstab,使之开启自动挂载:

# vi /etc/fstab
/dev/vdb1                                swap                     swap    defaults        0 0
/dev/vdb2                                  /data                  xfs     defaults       0 0


以上是关于云主机初始化swap与数据盘的主要内容,如果未能解决你的问题,请参考以下文章

阿里云Ubuntu主机搭建Lamp环境

阿里云服务器linux主机如何添加swap分区

zabbix Lack of free swap space

记一次 openstack 云主机热迁移失败与恢复过程

腾讯云新购主机怎么添加数据盘

腾讯云新购主机怎么添加数据盘