폴더 구조
| /data01/libvirt_images/origin | 원본 이미지 | 리눅스 공식 웹사이트에서 다운로드 한 클라우드 이미지 원본 |
| /data01/libvirt_images/base/config | 기본 설정 | 원본 이미지에 추가 또는 변경할 설정 |
| /data01/libvirt_images/base | 기본 이미지 | 원본를 변경한 것 |
| /data01/libvirt_images | VM 이미지 | 실제 운영되는 VM 이미지 |
클라우드 이미지 다운로드
Ubuntu 와 CentOS 는 클라우드 이미지를 제공한다.
...
| 코드 블럭 |
|---|
| language | bash |
|---|
| theme | Emacs |
|---|
| linenumbers | true |
|---|
|
sudo -s
cd /var/libdata01/libvirt_images/imagesorigin
wget https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud-2009.qcow2 -O CentOS-7-2009.qcow2
wget https://cloud.centos.org/centos/8-stream/x86_64/images/CentOS-Stream-GenericCloud-8-20210603.0.x86_64.qcow2 -O CentOS-Stream-8-20210603.qcow2
wget https://cloud-images.ubuntu.com/hirsute/current/hirsute-server-cloudimg-amd64-disk-kvm.img -O Ubuntu-21.04-hirsute.qcow2 |
기본 설정
| /data01/libvirt_images/base/config/sshd_config | SSH 서버 설정 | - 루트 접속 비활성화
- 키 접속만 허용
- 특정 IP 대역에서만 암호접속 허용
|
| /data01/libvirt_images/base/config/authorized_keys | 허용 SSH 키 | 이 파일에 등록된 SSH 키만 VM에 접속할 수 있음 |
| /data01/libvirt_images/base/config/pwd.txt | 기본 비번 |
|
| 코드 블럭 |
|---|
| language | bash |
|---|
| theme | Emacs |
|---|
| title | /data01/libvirt_images/base/config/sshd_config |
|---|
| linenumbers | true |
|---|
|
...
AuthorizedKeysFile .ssh/authorized_keys
PermitRootLogin no
PasswordAuthentication no
Match Address 10.100.10.0/24 User gdhong
PasswordAuthentication yes
Match Address 192.168.0.1/24 User gdhong
PasswordAuthentication yes
Match all
... |
기본 설정 적용
| 코드 블럭 |
|---|
| language | bash |
|---|
| theme | Emacs |
|---|
| title | /data01/libvirt_images/base/patch.sh |
|---|
| linenumbers | true |
|---|
| collapse | true |
|---|
|
[[ $# -ne 1 ]] && echo Usage: ./patch.sh xxx.qcow2 && exit
IMG=$1
PASSWD=$(<config/pwd.txt)
echo $IMG patch start
[[ "$IMG" =~ "centos" ]] && GRP=wheel
[[ "$IMG" =~ "ubuntu" ]] && GRP=sudo
virt-customize -a $IMG \
--install net-tools,tmux,zsh,wget,unzip,tar \
--root-password password:${PASSWD} \
--ssh-inject root:file:config/authorized_keys \
--run-command "useradd -m kreonet -g ${GRP}" \
--password kreonet:password:${PASSWD} \
--ssh-inject kreonet:file:config/authorized_keys \
--run-command 'cp -arp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak' \
--upload config/sshd_config:/etc/ssh/sshd_config
echo $IMG patch end |
| 코드 블럭 |
|---|
| language | bash |
|---|
| theme | Emacs |
|---|
| title | /data01/libvirt_images/base/patch.sh |
|---|
| linenumbers | true |
|---|
|
cd /data01/libvirt_images/base
./patch.sh centos-7.qcow2
./patch.sh centos-stream-8.qcow2
./patch.sh ubuntu-21.04.qcow2 |
(참고) 원본 이미지 확인
테스트로 우분투 이미지를 확인한다.
현재 531MB 이고, 2.1GB 까지 확장되는 동적 디스크 파일이다.
...
| 코드 블럭 |
|---|
| language | bash |
|---|
| theme | Emacs |
|---|
| linenumbers | true |
|---|
|
cd /data01/libvirt_images/base
IMG=Ubuntuubuntu-21.04-hirsute.qcow2
ls -alh $IMG
qemu-img info $IMG
virt-filesystems -l -h -a $IMG |
...
| 코드 블럭 |
|---|
| language | bash |
|---|
| theme | Emacs |
|---|
| linenumbers | true |
|---|
|
# cd /data01/libvirt_images/base
# IMG=Ubuntuubuntu-21.04-hirsute.qcow2
# ls -alh $IMG
531M Ubuntuubuntu-21.04-hirsute.qcow2
# qemu-img info $IMG
image: Ubuntuubuntu-21.04-hirsute.qcow2
file format: qcow2
virtual size: 2.2 GiB (2361393152 bytes)
disk size: 530 MiB
cluster_size: 65536
Format specific information:
compat: 0.10
refcount bits: 16
# virt-filesystems -l -h -a $IMG
Name Type VFS Label Size Parent
/dev/sda1 filesystem ext4 cloudimg-rootfs 2.1G -
/dev/sda15 filesystem vfat UEFI 106M - |
계정 설정
클라우드 이미지의 장점은 계정 비번을 잊어버리면 다시 설정할 수 있습니다.
루트 비번 수정
| 코드 블럭 |
|---|
| language | bash |
|---|
| theme | Emacs |
|---|
| linenumbers | true |
|---|
|
sudo -s
IMG=CentOS-7-2009.qcow2
export PWD=$(<secure/pwd.txt)
virt-customize -a $IMG --root-password password:$PWD |
루트 비번 수정
| 코드 블럭 |
|---|
| language | bash |
|---|
| theme | Emacs |
|---|
| linenumbers | true |
|---|
|
sudo -s
IMG=CentOS-7-2009.qcow2
export PWD=$(<pwd.txt)
virt-customize -a $IMG --root-password password:$PWD |