无法调整 AWS EBS 根卷的大小或创建更大的交换分区

Cannot resize AWS EBS root volume or create larger swap partition

我有一个 Amazon Web Services EC2 Debian 8.0 实例,其弹性块存储 (EBS) 卷为 35GB。 fdisk 正确显示了大小,但我的根分区仍然是 7.8G,尽管我尝试执行 resize2fs。

root@ip-10-0-3-164:/home/admin# fdisk -l

Disk /dev/xvda: 35 GiB, 37580963840 bytes, 73400320 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
Disklabel type: dos
Disk identifier: 0x7d936f86

Device     Boot Start      End  Sectors Size Id Type
/dev/xvda1 *     4096 16773119 16769024   8G 83 Linux

root@ip-10-0-3-164:/home/admin# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1      7.8G  2.1G  5.3G  29% /
udev             10M     0   10M   0% /dev
tmpfs           200M  4.3M  196M   3% /run
tmpfs           500M     0  500M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           500M     0  500M   0% /sys/fs/cgroup

root@ip-10-0-3-164:/home/admin# resize2fs /dev/xvda1
resize2fs 1.42.12 (29-Aug-2014)
The filesystem is already 2096128 (4k) blocks long.  Nothing to do!

root@ip-10-0-3-164:/home/admin# fdisk /dev/xvda

Welcome to fdisk (util-linux 2.25.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/xvda: 35 GiB, 37580963840 bytes, 73400320 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
Disklabel type: dos
Disk identifier: 0x7d936f86

Device     Boot Start      End  Sectors Size Id Type
/dev/xvda1 *     4096 16773119 16769024   8G 83 Linux


Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2048-73400319, default 2048): 2048
Last sector, +sectors or +size{K,M,G,T,P} (2048-4095, default 4095): 

Created a new partition 2 of type 'Linux' and of size 1 MiB.

Command (m for help): p
Disk /dev/xvda: 35 GiB, 37580963840 bytes, 73400320 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
Disklabel type: dos
Disk identifier: 0x7d936f86

Device     Boot Start      End  Sectors Size Id Type
/dev/xvda1 *     4096 16773119 16769024   8G 83 Linux
/dev/xvda2       2048     4095     2048   1M 83 Linux

Partition table entries are not in disk order.

Command (m for help): t   
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): L

 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
Hex code (type L to list all codes): 82

Changed type of partition 'Linux' to 'Linux swap / Solaris'.

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: 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).

root@ip-10-0-3-164:/home/admin# fdisk /dev/xvda

Welcome to fdisk (util-linux 2.25.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help): p
Disk /dev/xvda: 35 GiB, 37580963840 bytes, 73400320 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
Disklabel type: dos
Disk identifier: 0x7d936f86

Device     Boot Start      End  Sectors Size Id Type
/dev/xvda1 *     4096 16773119 16769024   8G 83 Linux
/dev/xvda2       2048     4095     2048   1M 82 Linux swap / Solaris

Partition table entries are not in disk order.

如您所见,resize2fs 没有效果。我也无法创建 space 超过 1M 的交换分区。

这个问题出现在我的所有实例上。尽管它们都有 30-35GB 的 EBS 卷,但我无法调整它们的大小,它们保持在 8GB。 当我在根安装的卷之外附加一个额外的卷时,它工作正常。

你能帮帮我吗?

取消附加 xvda 的卷。将其附加到另一个实例,但不要将其装载到卷上。然后使用 fdisk 删除第一个分区并重新添加所有可用的分区 space。设置 bootable 标志并确保文件系统类型仍然是 83。写入 fdisk 分区 table 并退出 fdisk。 运行 新的第一个分区上的 e2fsck 和 resize2fs

从另一个实例中取消附加磁盘并将其重新附加到原始实例。

当原始实例使用修改后的卷重新启动时,您应该会看到 35GB

这样做的原因是您无法修改 运行 的分区 table,附加的根磁盘

aws 文档中详细介绍了所有这些内容

稍微复杂一点,假设您只有一些易于识别的 "too-big" 目录树块(例如 /opt/var/log),将创建文件系统(s) 在未分配的 28GB 上,使用 rsync 将您的工作文件复制到新的文件系统,并擦除过时的文件(以释放磁盘空间),将新的文件系统挂载到它们的位置(当然是通过编辑 /etc/fstab ).

如果您为 /var/log 执行此操作,则必须在迁移文件后立即重新启动,因为系统仍然引用原始文件。当然,您可能想在测试机上练习该过程。

如果Amazon的root volumes使用LVM的话会简单很多

在 AWS EC2 + EBS 上的 Debian 8 Jessie 上使用 resize2fs 调整分区大小不工作。

我的解决方案是为此手动创建一个单独的卷。

我将在此处添加一个手册,逐步说明如何操作。

首先备份您的实例,创建映像 AMI 或快照卷。

正在创建图像。

或创建快照。

现在让我们创建一个 Debian 8 实例,只对您需要的卷进行重新分区。

并完成创建实例的所有步骤,直到它开始。

使用 ssh 访问实例。

以根用户身份登录。

sudo -i

现在让我们设置source.list能够安装'parted'。

pico /etc/apt/sources.list

并更新文件。

##########################################################################
deb http://cloudfront.debian.net/debian jessie main contrib non-free
deb-src http://cloudfront.debian.net/debian jessie main contrib non-free

deb http://security.debian.org/ jessie/updates main contrib non-free
deb-src http://security.debian.org/ jessie/updates main contrib non-free

deb http://cloudfront.debian.net/debian jessie-updates main contrib non-free
deb-src http://cloudfront.debian.net/debian jessie-updates main contrib non-free

deb http://cloudfront.debian.net/debian jessie-backports main contrib non-free
deb-src http://cloudfront.debian.net/debian jessie-backports main contrib non-free
##########################################################################

运行 apt-get.

apt-get clean && apt-get update && apt-get upgrade

现在让我们安装 parted。

apt-get install parted

现在停止创建的实例。

现在让我们从您创建的快照备份创建一个卷。

注意: 在这里告知你想要的分区的总大小。

添加音量 /dev/sdf

现在启动实例。

使用 ssh 访问实例。

以根用户身份登录。

sudo -i

检查分区

df -h

lsblk

你会看到这样的输出。

NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0   8G  0 disk
└─xvda1 202:1    0   8G  0 part /
 xvdf    202:80   0  40G  0 disk
└─xvdf1 202:81   0   8G  0 part

40G 的 xvdf 和 8G 的 xvdf1。

现在让我们运行分卷/dev/xvdf。

parted /dev/xvdf

分手之内。设置单位。

(parted) unit s

打印分区信息看看

(parted) print

你会看到这样的输出。

Model: Xen Virtual Block Device (xvd)
Disk /dev/xvdf: 83886080s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start  End        Size       Type     File system  Flags
 1      1s     16777215s  16777215s  primary  ext4         boot

删除分区。 (这不会删除数据,数据稍后检索)。

(parted) rm 1

现在我们从卷的开始 1s 到结束 100% 装载新分区。

(parted) mkpart primary 1 100%

 Warning: The resulting partition is not properly aligned for best performance.
 Ignore/Cancel? : Ignore

打印分区信息看看

(parted) print

你会看到这样的输出。

Model: Xen Virtual Block Device (xvd)
Disk /dev/xvdf: 83886080s
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:

Number  Start  End        Size       Type     File system  Flags
 1      1s     83886079s  83886079s  primary               lba

现在将分区设置为可引导分区。

(parted) set 1 boot on

并完成分手。

(parted) quit

忽略信息打印。

Information: You may need to update /etc/fstab.

检查分区

lsblk

你会看到这样的输出。

NAME    MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
xvda    202:0    0   8G  0 disk
└─xvda1 202:1    0   8G  0 part /
xvdf    202:80   0  40G  0 disk
└─xvdf1 202:81   0  40G  0 part

现在看到xvdf和xvdf1的大小是40G。 \o/

现在我们只需要制作e2fsck来检查和索引数据。

注意:e2fsck 可能需要很长时间才能完成检查。你应该等待。

e2fsck -f /dev/xvdf1

你会看到这样的输出。

e2fsck 1.42.12 (29-Aug-2014)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/xvdf1: 208603/524288 files (0.2% non-contiguous), 1918667/2097151 blocks

分区一切正常。

现在对分区进行快照备份,就像我们在开始教程中所做的那样。

使用创建的快照创建一个新卷,并添加原始实例。

记得先停止实例

删除旧卷实例并添加新卷实例。

附上 (/dev/****) 原件,我的情况是 /dev/sda。

启动实例,一切就绪!调整分区大小。 \o/

忘记其他答案。我只花了 3 天时间找出根本原因和一个简单的解决方法:

https://serverfault.com/questions/748063/debian-8-ami-only-using-8gb-partition