如何将交换添加到运行 Ubuntu 12.04 LTS 的 Amazon EC2 实例? [英] How to add swap to and Amazon EC2 instance running Ubuntu 12.04 LTS?

查看:24
本文介绍了如何将交换添加到运行 Ubuntu 12.04 LTS 的 Amazon EC2 实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,默认的 Ubuntu 12.04 LTS 不会创建交换.安装后是否有正确"的添加方式?

<前>root@aux3:/root# df -h已使用的文件系统大小 Avail Use% Mounted on/dev/xvda1 8.0G 1.4G 6.3G 18%/udev 1.9G 12K 1.9G 1%/devtmpfs 751M 188K 750M 1%/运行无 5.0M 0 5.0M 0%/运行/锁定无 1.9G 0 1.9G 0%/run/shm/dev/xvdb 394G 79G 296G 21%/mntroot@aux3:/root# swapon -s文档名称root@aux3:/root# free -m缓存的已用空闲共享缓冲区总数内存:3750 3730 20 0 75 3433-/+ 缓冲区/缓存:221 3528交换:0 0 0root@aux3:/root#

更新:通过交换文件找到临时解决方案:

(不过我仍在寻找正确"的方式)

<前>dd if=/dev/zero of=/mnt/swap1 bs=1024 count=4Mmkswap/mnt/swap1chown root.root/mnt/swap1chmod 600/mnt/swap1交换/mnt/swap1

解决方案

因为我花了一天的大部分时间为自己理解这个问题,而且因为拉起 ayurchen 的链接需要谷歌缓存搜索,我想我'd 发布一个稍微详细的演练,摘自 ayurchen 非常有用的答案.

什么是交换空间,为什么要使用它?

交换空间是 Linux(和大多数其他操作系统)在实际物理内存开始变得拥挤时用于从内存中存储对象的磁盘空间.因为磁盘通常比内存慢,所以 Linux 首先将最近最少使用的对象存储在交换中,并尽可能多地保留在内存中.如果可能,通常建议使用与内存大小相同的交换空间.在此处查看有关交换空间的更多详细信息.

我应该在哪里设置?

我也开始使用的 Ubuntu 12.04 LTS AMI 最初配置为没有交换空间,因为您的存储设备的大小和数量可能会有所不同.但大多数自动带有一个大型免费临时存储设备.由于 S3 存储受成本限制,临时驱动器是交换文件的好地方.我的设备名称与问题中提到的相同,/dev/xvdb,但您可以在实例启动期间进行配置.

如何在临时存储驱动器上设置交换空间?

可以在单个文件或设备的分区上设置交换空间.文件可以由磁盘上的非相邻块表示,而分区是一组预定义的相邻块.由于磁盘读写速度更快,您不必将读取头长距离移动,因此使用分区可以获得更好的性能.(这就是为什么上述问题使用文件作为临时解决方案打折的原因.)

Linux 带有许多管理磁盘分区的程序,包括 fdisk、sfdisk、parted 等.我们将使用 sfdisk,因为它可以接受来自 shell 脚本的所有必要参数.这很重要,因为每次我们停止"我们的实例时,临时存储都会丢失.因此,我们在每次启动实例时自动运行的文件中设置了一个脚本,/etc/rc.local.

# 卸载驱动器以防它已经安装.如果 Umount 抛出错误# 它没有被挂载,所以我们添加 ||: 在这种情况下继续脚本卸载/dev/xvdb ||:# 下面的每一行是一个分区(这个分区最多4个主分区# 类型).通常可以使用默认参数,只提供数量# 我们想要的块空间(512 对我来说大约 4gb),以及分区# type (swap 为 82,一般 linux 为 83 为默认值).这将创建:# dev/xvdb1 有 1024 个块# dev/xvdb2 与磁盘的剩余部分sfdisk/dev/xvdb <<EOF,512,82;;;EOF# 现在我们格式化交换分区:mkswap/dev/xvdb1# 剩下的.您可以选择各种文件系统类型,但是# 确保你安装了必要的格式化程序.要检查,ls/sbin/mk*mkfs.ext4/dev/xvdb2

最后,我更愿意将我的挂载信息包含在 /etc/fstab 中,这是 Ubuntu 上的一个系统文件,它规定了如何自动处理各种可用设备.它也在启动时运行.

/dev/xvdb1 swap swap sw,nobootwait 0 0/dev/xvdb2/mnt2 ext4 默认值,nobootwait 0 0

nobootwait 选项是为了确保 Ubuntu 不会在启动时挂起/dev/xvdb(1/2) 的磁盘驱动器尚未准备好或不存在.继续等待,或按 S 跳过安装或M 用于手动恢复."

确保您在 /mnt2 或任何您计划使用 mkdir 安装它的地方创建一个文件夹.

如何调试我遇到的问题?

您可以使用 sudo/etc/rc.local 运行 rc.local 脚本,以观察它是否运行以查找问题.sudo fdisk -l 运行后应该会显示你的新分区.如果看起来没问题,请尝试使用 sudo mount/dev/xvdb1 挂载您的设备.这将使用您在 fstab 中保存的配置.如果失败,请尝试调整您的安装参数并相应地调整 fstab.

default Ubuntu 12.04 LTS doesn't create swap for some reason. Is there "proper" way to add it after install?

root@aux3:/root# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1      8.0G  1.4G  6.3G  18% /
udev            1.9G   12K  1.9G   1% /dev
tmpfs           751M  188K  750M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            1.9G     0  1.9G   0% /run/shm
/dev/xvdb       394G   79G  296G  21% /mnt

root@aux3:/root# swapon -s
Filename
root@aux3:/root# free -m
             total       used       free     shared    buffers     cached
Mem:          3750       3730         20          0         75       3433
-/+ buffers/cache:        221       3528
Swap:            0          0          0
root@aux3:/root# 

Update: found temporary solution via swap file:

(I'm still looking for "proper" way though)

dd if=/dev/zero of=/mnt/swap1 bs=1024 count=4M
mkswap /mnt/swap1
chown root.root /mnt/swap1
chmod 600 /mnt/swap1
swapon /mnt/swap1

解决方案

Because I've spent the better part of a day understanding this problem for myself, and because pulling up ayurchen's link required a google cache search, I thought I'd post a slightly more detailed walkthrough, drawing from ayurchen's very helpful answer.

What is swap space, and why use it?

Swap space is disk space used by Linux (and most other operating systems) to store objects from memory when real physical memory starts becoming crowded. Because disk is generally slower than memory, Linux stores in swap the least recently used objects first, and keeps as much as it can in memory. It's generally recommended to have a swap space equal in size to your memory, if possible. See more details about swap space here.

Where should I set this up?

The Ubuntu 12.04 LTS AMI, which I also started with, is configured with no swap space initially, as the size and number of your storage devices can vary. But most come with a large free ephemeral storage device automatically. Since S3 storage is limited by cost, the ephemeral drive is a good place for a swap file. Mine has the same device name mentioned in the question, /dev/xvdb, but you can configure this during instance launch.

How can I set up a swap space on my ephemeral storage drive?

Swap space can be set up in a single file or on a partition of a device. A file can be represented by non-adjacent blocks on a disk, whereas a partition is a predefined set of adjacent blocks. Since disk read-write is faster you don't have to keep moving the read head long distances, we get better performance using a partition. (This is why the question above discounts using a file as a temporary solution.)

Linux comes with many programs to manage disk partitions, including fdisk, sfdisk, parted, etc. We will be using sfdisk, because it can accept all the necessary arguments from a shell script. This is important, because ephemeral storage is lost every time we "stop" our instance. Thus, we set up a script in a file that automatically runs each time the instance is started, /etc/rc.local.

# Unmount the drive in-case it is already mounted. Umount throws an error if
# it wasn't mounted, so we add || : to continue the script in that case
umount /dev/xvdb || :

# Each line below is a partition (4 maximum master partitions for this partition 
# type). Can can generally use the default arguments, supplying only the amount
# of space we want in blocks (512 came out to ~4gb for me), and the partition
# type (82 for swap, 83 for general linux is default). This will create:
# dev/xvdb1 with 1024 blocks
# dev/xvdb2 with the remainder of the disk
sfdisk /dev/xvdb << EOF
,512,82
;
;
;
EOF

# Now we format the swap partition:
mkswap /dev/xvdb1

# And the remainder. You can choose amoung the various filesystem types, but
# make sure you have the necessary formatter installed. To check, ls /sbin/mk*
mkfs.ext4 /dev/xvdb2

Finally, I preferred to include my mounting information in /etc/fstab, which is a system file on Ubuntu that prescribes how to handle various available devices automatically. It is also run at start up.

/dev/xvdb1      swap    swap    sw,nobootwait      0 0
/dev/xvdb2      /mnt2   ext4    defaults,nobootwait        0 0

The nobootwait option is to ensure Ubuntu doesn't hang on boot with "The disk drive for /dev/xvdb(1/2) is not ready yet or not present. Continue to wait, or Press S to skip mounting or M for manual recovery."

Make sure you create a folder at /mnt2 or wherever you plan on mounting this with mkdir.

How do I debug the problems I'm experiencing with this?

You can run the rc.local script with sudo /etc/rc.local to watch it run an look for problems. sudo fdisk -l should show your new partitions after it runs. If that looks ok, try mounting your devices with sudo mount /dev/xvdb1. This will use the configuration you've saved in fstab. If that fails, try playing around with your mounting parameters and adjust fstab accordingly.

这篇关于如何将交换添加到运行 Ubuntu 12.04 LTS 的 Amazon EC2 实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆