Change Ubuntu Cloud Image Size

Ubuntu Cloud Image Size 似乎各個版本的特性不太一樣, 而且似乎也要用不同的方式修改 Image size

一般 change image size 是用這個指令

$ sudo qemu-img resize $UBUNTUVERSION-server-cloudimg-$ARCH.img +50G0
ex:

$ sudo qemu-img resize xenial-server-cloudimg-amd64-uefi1.img +50G

將 qcow2 image format 變成 raw disk

$ sudo qemu-img convert  xenial-server-cloudimg-amd64-uefi1.img xenial-server-cloudimg-amd64-uefi1.raw

之前測 ARM64 17.04 和 17.10 都會自動增加 partition size
但是最近碰到 18.04 似乎不會. (Both X64 and ARM64)

如果碰到的是 16.04 要增加 partition size 有問題, 要用 parted 修改 (似乎 fdisk 和 gdisk 都有問題)
ex:

$ parted xenial-server-cloudimg-amd64-uefi1.raw
(parted) p
Model:  (file)
Disk /work/research/vm/1.raw: 56.0GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
 
Number  Start   End     Size    File system  Name  Flags
14      1049kB  5243kB  4194kB                     bios_grub
15      5243kB  116MB   111MB   fat32              boot, esp
 1      116MB   2361MB  2245MB  ext4
 
(parted) resizepart
Warning: Not all of the space available to /work/research/vm/1.raw appears to be used, you can fix the GPT to use
all of the space (an extra 104857600 blocks) or continue with the current setting?
parted: invalid token: 1
 
Partition number? 1
End?  [2361MB]? 56G

離開就會儲存

Ubuntu 16.04 的 cloud image 不知道為什麼 cloud-init 會無法結束, 如果不能接觸到 login 那 cloud image 也沒有用.

這時可以用另一個方式移除 cloud-init 相關的套件,
1. 使用 kpartx 將 image 掛到 loop devices 如果沒有, 就先安裝 kpartx 套件

$ sudo apt install kpartx

2. mount partition (還有 proc, dev, sys)
3. 移除 cloud 相關套件
4. umount partition (要先 umount proc, dev, sys
5. 移除 loop device
6. convert raw file 到 qcow2 格式

mount cloud image partition

$ sudo kpartx -av xenial-server-cloudimg-amd64-uefi1.raw
add map loop0p1 (253:0): 0 109147673 linear 7:0 227328
add map loop0p14 (253:1): 0 8192 linear 7:0 2048
add map loop0p15 (253:2): 0 217088 linear 7:0 10240

loop0p1 就是我們要 mount 的 disk , (註: 有可能因系統不同, 所以會有其他的 device name)

mount disk

$ sudo su
$ mkdir disk
$ mount /dev/mapper/loop0p1 disk 
$ cd disk
$ mount --bind /proc proc
$ mount --bind /dev dev
$ mount --bind /sys sys
$ cd ..
$ chroot disk

移除 cloud-init 相關的套件

$ dpkg  -l |grep cloud
ii  cloud-guest-utils                0.27-0ubuntu25.1                           all          cloud guest utilities
ii  cloud-init                       18.3-9-g2e62cb8a-0ubuntu1~16.04.2          all          Init scripts for cloud instances
ii  cloud-initramfs-copymods         0.27ubuntu1.5                              all          copy initramfs modules into root filesystem for later use
ii  cloud-initramfs-dyn-netconf      0.27ubuntu1.5                              all          write a network interface file in /run for BOOTIF
ii  ubuntu-cloudimage-keyring        2013.11.11                                 all          GnuPG keys of the Ubuntu Cloud Image builder
 
$ dpkg -r cloud-guest-utils  cloud-init cloud-initramfs-copymods  ubuntu-server

umount disk

$ exit 
$ cd disk 
$ umount proc
$ umount dev
$ umount sys
$ cd ..
$ umount disk

最後將 raw disk image 轉成 qcow2

$ sudo qemu-img convert  xenial-server-cloudimg-amd64-uefi1.raw xenial-server-cloudimg-amd64-uefi1.img

這樣 16.04 開機就不會卡在 cloud init script 了.

On this day..

Related Posts

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>