最後更新: 2019-06-11
介紹
原理:
VM Image 預裝了cloud-init, 我們將 config 放到 iso 內
在 VM 第一次啟動時讓 cloud-init 去設定它
cloud-init 的 Boot Stages
Generator
# 決定是否行 cloud-init
- A file exists: /etc/cloud/cloud-init.disabled
- /proc/cmdline contains cloud-init=disabled
Local
# cloud-init-local.service
# As soon as possible with / mounted read-write
- locate “local” data sources
- apply networking configuration to the system
Network
# cloud-init.service
# After local stage and configured networking is up
# runs modules: disk_setup, mounts, bootcmd ...
Config
# cloud-config.service
# After network stage
- runs config modules only (cloud_config_modules in /etc/cloud/cloud.cfg)
Final
# cloud-final.service
此 step 會執行 user-scripts (including runcmd)
目錄
- Download Cloud Image
- Resize image & Create swap file
- growpart
Download Cloud Image
# C7
mkdir /ssd/cloudimg
cd /ssd/cloudimg
wget https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qc...
unxz -k CentOS-7-x86_64-GenericCloud.qcow2.xz
Remark
- CentOS-7-x86_64-GenericCloud.qcow2.xz --> Last version image # 它已是最 update img
- .xz 比 .tar.gz 有更好壓縮比, 所以使用它
Resize image & Create swap file
# resize image
qemu-img resize cloudimg.qcow2 20G
qemu-img info cloudimg.qcow2
# create swap
fallocate -l 1G swap.raw
建立設定檔
cloud-config.txt
#cloud-config hostname: cloudimg.local user: root password: MyPassword chpasswd: { expire: False } ssh_pwauth: True growpart: mode: auto devices: ['/'] locale: en_HK.UTF-8 timezone: Asia/Hong_Kong write_files: - encoding: b64 content: U0VMSU5VWD1kaXNhYmxlZApTRUxJTlVYVFlQRT10YXJnZXRlZCAK owner: root:root path: /etc/selinux/config permissions: '0644' disk_setup: /dev/vdb: table_type: 'mbr' layout: True overwrite: True fs_setup: - label: 'swap' filesystem: 'swap' device: '/dev/vdb1' mounts: - [ '/dev/vdb1', 'none', 'swap', 'sw', '0', '0' ] bootcmd: - ifdown eth0 - ifup eth0 package_upgrade: true packages: - pwgen - wget - screen - vim runcmd: - [ yum, -y, remove, cloud-init ] - [ yum, -y, remove, cloud-utils-growpart ] power_state: delay: "+1" mode: reboot message: Initial configuration done by cloud-init, forcing reboot to apply changes.
meta-data.txt
network-interfaces: | iface eth0 inet static address 192.168.123.44 network 192.168.123.0 netmask 255.255.255.0 broadcast 192.168.123.255 gateway 192.168.123.1 dns-nameservers 8.8.8.8 dns-nameservers 8.8.4.4 dns-search local
cloud-localds
安裝 tool
apt install cloud-image-utils
Usage
cloud-localds [ options ] output user-data [meta-data]
NoCloud
The data source NoCloud allows the user to provide user-data and meta-data to the instance without running a network service
(or even without having a network at all)
You can provide meta-data and user-data to a local vm boot via files on a vfat or iso9660 filesystem.
The filesystem volume label must be cidata or CIDATA
Example
cloud-localds cloud-config.img cloud-config.txt meta-data.txt
用設定檔去安裝 VM - virtinst
apt-get install virtinst
virt-install --connect=qemu:///system \ --name cloudimg \ --ram 512 \ --vcpus=2 \ --virt-type kvm \ --os-type=linux \ --os-variant=centos7.0 \ --disk cloudimg.qcow2,device=disk,bus=virtio \ --disk swap.raw,device=disk,bus=virtio \ --disk cloud-config.img,device=cdrom \ --network bridge=br0,model=virtio \ --graphics vnc,listen=0.0.0.0,port=5944,password=foobar \ --import
Final
# 離開
Ctrl + ]
Remark
* 當 "--graphics none"時, cmd 後會 auto 進入了 console
* swap 放在分配好空間的 raw 檔內
growpart
# Installation (C7)
yum install cloud-utils-growpart
growpart [OPTIONS] DISK PARTITION-NUMBER
ie.
growpart /dev/sda 3
應用: Online resize HDD
1. Resize VM Image
2. 找出要 resize 的 partition
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 102G 0 disk
├─sda1 8:1 0 7.8G 0 part [SWAP]
└─sda2 8:2 0 74.2G 0 part /
3. 令系統知到新的 Disk Size
echo 1 > /sys/block/sda/device/rescan
dmesg
[23741366.603849] sd 0:0:0:0: [sda] 213909504 512-byte logical blocks: (109 GB/102 GiB) [23741366.603974] sda: detected capacity change from 88046829568 to 109521666048
4. Install Tools
dnf install cloud-utils-growpart -y # Centos 8
yum install cloud-utils-growpart -y # Centos 7
5. Resize partition
growpart /dev/sda 2
CHANGED: partition=2 start=16386048 old: size=155580416 end=171966464 new: size=197523423 end=213909471
lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 102G 0 disk
├─sda1 8:1 0 7.8G 0 part [SWAP]
└─sda2 8:2 0 94.2G 0 part /
6. Resize FS
mount | grep sda2
/dev/sda2 on / type xfs (rw,relatime,attr2,inode64,noquota)
xfs_growfs /
meta-data=/dev/sda2 isize=512 agcount=6, agsize=3551168 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0 spinodes=0 data = bsize=4096 blocks=19447552, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal bsize=4096 blocks=6935, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 data blocks changed from 19447552 to 24690427
df -h
Doc
https://cloudinit.readthedocs.io/en/latest/