对已有的img镜像文件如何扩容

记录一下:对于已经存在img镜像文件,常用于linux系统的文件。有时候镜像文件太小了,想加入一些新的大的内容时空间不够,重新生成文件太麻烦并拷贝内容比较麻烦和耗时。记录一下如何在虚拟机的环境下通过简单命令,快速进行扩容和验证的方法。

1,已有的镜像文件为Ubuntu-22.04-arm64-desktop.img,大小8GB

ls -l /mnt/C/workspace/Ubuntu-22.04-arm64-desktop.img 
-rwxrwx--- 1 root vboxsf 8388608000 8月  27 12:56 /mnt/C/workspace/Ubuntu-22.04-arm64-desktop.img

2,扩容(在virtualbox环境下ubuntu的环境下)

2.1 增加空间

  • qemu-img resize /mnt/C/workspace/Ubuntu-22.04-arm64-desktop.img +3G

WARNING: Image format was not specified for '/mnt/C/workspace/Ubuntu-22.04-arm64-desktop.img' and probing guessed raw.
         Automatically detecting the format is dangerous for raw images, write operations on block 0 will be restricted.
         Specify the 'raw' format explicitly to remove the restrictions.
Image resized.

ls -l /mnt/C/workspace/Ubuntu-22.04-arm64-desktop.img
-rwxrwx--- 1 root vboxsf 11737309184 9月  25 09:12 /mnt/C/workspace/Ubuntu-22.04-arm64-desktop.img

2.2 强制检查镜像文件信息

  • e2fsck -f /mnt/C/workspace/Ubuntu-22.04-arm64-desktop.img

 e2fsck -f /mnt/C/workspace/Ubuntu-22.04-arm64-desktop.img
e2fsck 1.45.5 (07-Jan-2020)
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
/mnt/C/workspace/Ubuntu-22.04-arm64-desktop.img: 190002/524288 files (0.1% non-contiguous), 1821503/2079122 blocks

2.3 拓展分区和文件系统

  • resize2fs /mnt/C/workspace/Ubuntu-22.04-arm64-desktop.img

resize2fs /mnt/C/workspace/Ubuntu-22.04-arm64-desktop.img
resize2fs 1.45.5 (07-Jan-2020)
Resizing the filesystem on /mnt/C/workspace/Ubuntu-22.04-arm64-desktop.img to 2865554 (4k) blocks.
The filesystem on /mnt/C/workspace/Ubuntu-22.04-arm64-desktop.img is now 2865554 (4k) blocks long.

2.4 检查并仿真下模拟

sudo mount /mnt/C/workspace/Ubuntu-22.04-arm64-desktop.img ubuntu

./chroot-mount.sh -m ubuntu
MOUNTING
root@dcic:/# df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/loop2      11174712 7051956   3533268  67% /
tmpfs            2002732       0   2002732   0% /run
udev             1957808       0   1957808   0% /dev
root@dcic:/# exit             
exit

./chroot-mount.sh -u ubuntu

备注:chroot-mount.sh内容

dcic@dcic:~/workspace$ cat ./chroot-mount.sh 
#!/bin/bash

function mnt() {
    echo "MOUNTING"
    sudo mount -t proc proc ${2}/proc
    sudo mount -t tmpfs tmpfs ${2}/run
    sudo mount -o bind /sys ${2}/sys
    sudo mount -o bind /dev ${2}/dev
    #sudo mount -o bind /run ${2}/run
#    sudo mount --bind / ${2}host
#   sudo cp /usr/bin/qemu-aarch64-static ${2}/usr/bin/
    sudo chroot ${2}
}

function umnt() {
    echo "UNMOUNTING"
    sudo umount ${2}/proc
    sudo umount ${2}/sys
    sudo umount ${2}/run
    sudo umount ${2}/dev
#    sudo umount ${2}host
}


if [ "$1" == "-m" ] && [ -n "$2" ] ;
then
    mnt $1 $2
elif [ "$1" == "-u" ] && [ -n "$2" ];
then
    umnt $1 $2
else
    echo ""
    echo "Either 1'st, 2'nd or both parameters were missing"
    echo ""
    echo "1'st parameter can be one of these: -m(mount) OR -u(umount)"
    echo "2'nd parameter is the full path of rootfs directory(with trailing '/')"
    echo ""
    echo "For example: ch-mount -m /media/sdcard/"
    echo ""
    echo 1st parameter : ${1}
    echo 2nd parameter : ${2}
fi


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值