Adeel Ahmad 答案的补充:
如果您尝试从具有交换分区的 AMI 启动实例,则必须执行其他步骤。
例如,如果 ami 包含以下内容:
# fdisk -l
Disk /dev/xvde: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xe211223f
Device Boot Start End Blocks Id System
/dev/xvde1 * 1 1291 10369926 83 Linux
/dev/xvde2 1292 1305 112455 82 Linux swap / Solaris
如果我必须将容量升级到 20GB,我将创建一个 AMI 并尝试启动另一个具有 20GB 空间的实例。在此之后,如果我尝试上述步骤,磁盘空间不会增加,因为在 xvde1 和新空间之间有一个 xvde2 分区。
$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/xvde1 9.8G 7.5G 1.8G 81% /
$ fdisk -l
Disk /dev/xvde: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xe211223f
Device Boot Start End Blocks Id System
/dev/xvde1 * 1 1291 10369926 83 Linux
/dev/xvde2 1292 1305 112455 82 Linux swap / Solaris
$ resize2fs /dev/xvde1
resize2fs 1.41.12 (17-May-2010)
The filesystem is already 2592481 blocks long. Nothing to do!
在这种情况下,请执行以下操作
- 删除两个分区
- 使用新的所需大小减去交换空间的大小创建新的主分区
- 为此分区添加可引导标志
- 创建第二个分区
- 将其标记为交换
- 写入更改并重新启动
- 扩展分区 1
- 设置交换或
删除分区 1 选定的分区 1
Command (m for help): d <<6>>
Partition number (1-4): 1 <<6.0.1>>
删除分区 2 选定的分区 2
Command (m for help): d <<6.2>>
创建调整大小的主分区 1
Command (m for help): n <<7>>
Command action
e extended
p primary partition (1-4)
p <<8>>
Partition number (1-4): 1 <<9>>
First sector (17-41943039, default 17): 2048 <<10>>
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039):<<NEW_UPPER_LIMIT>> <<11>>
TAKE CARE : 2048 应该替换为您原来的起始扇区,否则系统将无法启动。NEW_UPPER_LIMIT 将是上限的新扇区号,其余的将留作交换。为了保持相同的交换空间,减去原始的开始和结束扇区号,然后从 41943039(或您的上限)中减去结果
创建交换分区
Command (m for help): n <<12>>
Command action
e extended
p primary partition (1-4)
p <<13>>
Partition number (1-4): 2 <<14>>
First sector (<<NEW_UPPER_LIMIT+1>>-41943039, default <<NEW_UPPER_LIMIT+1>>): <<USE_DEFAULT>> <<15>>
Last sector, +sectors or +size{K,M,G}(<<NEW_UPPER_LIMIT+1>>-41943039,default 41943039):<<USE_DEFAULT>> <<16>>
Using default value 41943039
为分区 1 添加可引导位
Command (m for help): a <<17>>
Partition number (1-4): 1 <<18>>
将分区 2 标记为交换
Command (m for help): l <<19>>
现在您将看到一个文件系统列表。注意对应于 Linux 交换的那个(比如 82)
Command (m for help): t <<20>>
Partition number (1-4): 2 <<21>>
Hex Code (type l to list codes) : 82 <<22>>
写入更改并重新启动
Command (m for help): w <<23>> The partition table has been altered!
....
$ sudo reboot
重启后运行
resize2fs /dev/xvde1
这将调整你的 fs
现在使用第二个分区作为交换
$ mkswap /dev/<<SECOND SWAP PARTITION(run fdisk -l to get the name)>>
$ swapon /dev/<<SECOND SWAP PARTITION(run fdisk -l to get the name)>>
您可以检查 /proc/swaps 文件以验证
$ cat /proc/swaps
现在将以下内容添加到 /etc/fstab 以使这些更改保持不变
在 /etc/fstab 末尾(使用 nano 或 vi 等打开)
/dev/<<SECOND SWAP PARTITION>> swap swap defaults 0 0
保存并退出
重启并检查