ubuntu 20.04 cloud-init을 통한 Custom ISO 설치 자동화 (autoinstall)

CentOS(Redhat)의 경우 kickstart가 강력하여 모든 설치가 kickstart를 통해서 가능하다.
ubuntu의 경우 설치 자동화를 구현하려면 preseed를 통한 자동화 또는 kickstart를 사용해야한다.
그러나 이 2가지에 큰 단점이 있는데 간단하게 정리하면 아래와 같다.

o. preseed 설치 방식
   ubuntu 설치 상에 보이는 gui의 실행을 정의해서 자동 설치를 구현한다.
   단점으로 installer software가 업데이트되거나, gui에 나오는 메뉴에 대한 정의를 하나하나 다 찾아야한다.

o. kickstart를 통한 설치 방식
  어느 정도 ubuntu에서는 kickstart를 지원한다. 그러나 어느 정도이지 CentOS 정도의 자세한 기능까지는
  지원하지 않아 제약사항이 너무 많다.

이런 이유 때문인지 ubuntu에서는 두가지 방식이 아닌 cloud-init 방식을 적극적으로 지원 & 개발하는 것으로
보여진다. 그렇다고 preseed & kickstart 보다 사용하기 편할 뿐이지, 전체 기능을 모두 지원하는 것은 또한 아니다.
 

일단 설치하기 앞서 아래 레퍼런스 사이트에서 관련 정보를 얻을 수 있다.
https://ubuntu.com/server/docs/install/autoinstall

특히나 중요한 부분이 아래 ubuntu에서 cloud-init로 지원하는 부분을 확인할 수 있는 Schema 부분으로 아래 링크를
통해서 정보를 얻을 수 있다.
https://ubuntu.com/server/docs/install/autoinstall-schema

그럼 아래 가이드로 iso 부팅이 가능하니 간단한 설치 자동화를 구성해본다.
https://blog.encicle.com/ubuntueseo-grubleul-tonghan-iso-buting

Custom ISO를 만들기 위해서는 기존 ISO 파일에서 아래 명령어로 데이터를 추출한다.

# wget https://mirror.kakao.com/ubuntu-releases/focal/ubuntu-20.04.4-live-server-amd64.iso

# mkdir iso-mount
# mkdir new-iso

# mount ubuntu-20.04.4-live-server-amd64.iso iso-mount
# cp -rP iso-mount/* new-iso/

# umount iso-mount

 

cloud-init 파일이 위치할 디렉토리 생성

# mkdir new-iso/nocloud

 

cloud-init에 사용할 파일 2개 생성

# touch new-iso/nocloud/meta-data
# touch new-iso/nocloud/user-data

 

meta-data에는 데이터를 넣을 필요가 없다.

# cat new-iso/nocloud/meta-data
#

user-data에 설정되는 기본 포멧은 아래와 같다.

#cloud-config
autoinstall:
  version: 1
  locale:
  keyboard:
  network:
  identity: 
  apt:
  storage:
  packages:
  ssh:
  chpasswd:
  ssh_pwauth: True
  users:
  early-commands:
  late-commands:
  user-data:

 

이중에 메뉴얼하게 설치 중간에 GUI메뉴를 통해서(interactive) 설정하고 싶다면 아래와 같이
"interactive-sections"로 빼주면 된다.

#cloud-config
autoinstall:
  version: 1
  interactive-sections:
    - storage
    - packages
    - network
    - identity
    - apt
    - packages
    - ssh
    - chpasswd
    - ssh_pwauth
    - users
  locale:
  keyboard:
  early-commands:
  late-commands:
  user-data:

 

위 설정으로 user-data를 만들면  locale과 keboard 설정만 자동화해주는 user-data 샘플이다.

#cloud-config
autoinstall:
  version: 1
  interactive-sections:
    - storage
    - packages
    - network
    - identity
    - apt
    - packages
    - ssh
    - chpasswd
    - ssh_pwauth
    - users
  locale: en_US.UTF-8
  keyboard:
    layout: us
  early-commands:
  late-commands:
  user-data:
    disable_root: true

 

이제 부팅 시, 해당 파일을 읽어오도록 ISO의 grub 설정을 변경한다.

# cat new-iso/boot/grub/grub.cfg

if loadfont /boot/grub/font.pf2 ; then
	set gfxmode=auto
	insmod efi_gop
	insmod efi_uga
	insmod gfxterm
	terminal_output gfxterm
fi

set menu_color_normal=white/black
set menu_color_highlight=black/light-gray

set timeout=5
menuentry "Install Ubuntu Server" {
	set gfxpayload=keep
	linux	/casper/vmlinuz   quiet  ---
	initrd	/casper/initrd
}
grub_platform
if [ "$grub_platform" = "efi" ]; then
menuentry 'Boot from next volume' {
	exit 1
}
menuentry 'UEFI Firmware Settings' {
	fwsetup
}
fi
submenu 'Boot and Install with the HWE kernel' {
menuentry "Install Ubuntu Server" {
	set gfxpayload=keep
	linux	/casper/hwe-vmlinuz   quiet  ---
	initrd	/casper/hwe-initrd
}
}

to

# cat new-iso/boot/grub/grub.cfg

if loadfont /boot/grub/font.pf2 ; then
	set gfxmode=auto
	insmod efi_gop
	insmod efi_uga
	insmod gfxterm
	terminal_output gfxterm
fi

set menu_color_normal=white/black
set menu_color_highlight=black/light-gray

set timeout=5
menuentry "Install Ubuntu Server" {
	set gfxpayload=keep
	linux	/casper/vmlinuz   quiet  autoinstall ds=nocloud\;s=/cdrom/nocloud/ ---
	initrd	/casper/initrd
}
grub_platform
if [ "$grub_platform" = "efi" ]; then
menuentry 'Boot from next volume' {
	exit 1
}
menuentry 'UEFI Firmware Settings' {
	fwsetup
}
fi
submenu 'Boot and Install with the HWE kernel' {
menuentry "Install Ubuntu Server" {
	set gfxpayload=keep
	linux	/casper/hwe-vmlinuz   quiet  autoinstall ds=nocloud\;s=/cdrom/nocloud/ ---
	initrd	/casper/hwe-initrd
}
}

 

isolinux 설정도 변경한다. (이것이 무엇인지 물어보지 말자... ISO를 다루는데 기본은...)

# cat new-iso/isolinux/txt.cfg
default live
label live
  menu label ^Install Ubuntu Server
  kernel /casper/vmlinuz
  append   initrd=/casper/initrd quiet  ---
label hwe-live
  menu label ^Install Ubuntu Server with the HWE kernel
  kernel /casper/hwe-vmlinuz
  append   initrd=/casper/hwe-initrd quiet  ---
label memtest
  menu label Test ^memory
  kernel /install/mt86plus
label hd
  menu label ^Boot from first hard disk
  localboot 0x80

to

# cat new-iso/isolinux/txt.cfg
default live
label live
  menu label ^Install Ubuntu Server
  kernel /casper/vmlinuz
  append   initrd=/casper/initrd quiet  autoinstall ds=nocloud;s=/cdrom/nocloud/ ---
label hwe-live
  menu label ^Install Ubuntu Server with the HWE kernel
  kernel /casper/hwe-vmlinuz
  append   initrd=/casper/hwe-initrd quiet  autoinstall ds=nocloud;s=/cdrom/nocloud/ ---
label memtest
  menu label Test ^memory
  kernel /install/mt86plus
label hd
  menu label ^Boot from first hard disk
  localboot 0x80

 

아래 명령어를 사용해서 신규 ISO 이미지를 만든다.

# apt install -y p7zip-full xorriso isolinux

# md5sum new-iso/.disk/info > new-iso/md5sum.txt
# sed -i 's|iso/|./|g' new-iso/md5sum.txt
# mv new-iso/ubuntu .
# (cd new-iso; find '!' -name "md5sum.txt" '!' -path "./isolinux/*" -follow -type f -exec "$(which md5sum)" {} \; > ../md5sum.txt)
# mv md5sum.txt new-iso/
# mv ubuntu new-iso

# apt-get install isolinux

# xorriso -as mkisofs -r \
  -V Ubuntu\ custom\ amd64 \
  -o ubuntu-20.04.4-live-server-amd64-autoinstall.iso \
  -J -l -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot \
  -boot-load-size 4 -boot-info-table \
  -eltorito-alt-boot -e boot/grub/efi.img -no-emul-boot \
  -isohybrid-gpt-basdat -isohybrid-apm-hfsplus \
  -isohybrid-mbr /usr/lib/ISOLINUX/isohdpfx.bin  \
  iso/boot new-iso

 

해당 명령어를 사용해서 "ubuntu-20.04.4-live-server-amd64-autoinstall.iso" 파일이 생성되면 정상 생성된 것이다.

이걸 사용해서 OS를 설치해서 자동화되는지 확인해본다.

 

추가로 user-data를 설치 ISO 또는 커널과 연동하려면 몇 가지만 지원한다.
1. iso 파일에 내장해서 CDROM과 연동하는 방법
2. http로 연동하는 방법
3. nfs로 연동하는 방법

그래서 해당 방법은 ISO 이미지에 파일을 내장 시켜서 CDROM과 연동하는 방법을 정리한 것이다.