Linux img镜像扩容方法
扩容image文件方法:
我们使用qemu-img 工具
使用下面命令给img文件追加50G空间:
qemu-img resize $img +50G
Image resized.
You can mount the image and check that it has the correct size:
用kpartx工具挂载img文件:
kpartx -v -a $img
add map loop0p1 (254:13): 0 195318207 linear /dev/loop0 63
查看挂载后的信息:parted /dev/loop0 print
Model: (file) Disk /dev/loop0: 215GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system Flags 1 32.3kB 100GB 100GB primary ext3 .. 删掉当前分区表分区1parted /dev/loop0 rm 1
重新按照新空间大学创建分区表(上面信息中有新大小和开始字节数)parted /dev/loop0 mkpart primary ext3 32.3kB 215GB
重新打印分区检查分区信息parted /dev/loop0 print
重新挂载镜像:kpartx -d $img
loop deleted : /dev/loop0kpartx -v -a $img
add map loop0p1 (254:13): 0 419430337 linear /dev/loop0 63
扩容新增空间到最后一个分区检查后扩容的分区e2fsck -f /dev/mapper/loop0p1
e2fsck 1.42.5 (29-Jul-2012) /dev/mapper/loop0p1: recovering journal 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/mapper/loop0p1: 41753/6111232 files (9.1% non-contiguous), 23479003/24414775 blocks扩容分区resize2fs /dev/mapper/loop0p1
resize2fs 1.42.5 (29-Jul-2012) Resizing the filesystem on /dev/mapper/loop0p1 to 52428792 (4k) blocks. The filesystem on /dev/mapper/loop0p1 is now 52428792 blocks long.卸载镜像,重新挂载,mount 分区到系统,用df查看空间mount /dev/mapper/loop0p1 /mnt
df -h /mnt
Filesystem Size Used Avail Use% Mounted on /dev/mapper/loop0p1 197G 89G 109G 45% /mnt卸载img文件收工umount /mnt
kpartx -d $img
loop deleted : /dev/loop0