본문 바로가기

블로그

dpkg 패키지 만들기

안녕하세요. 오늘은 우분투에서 dpkg 패키지를 만드는 방법에 대해 알아보겠습니다.

dpkg는 Debian Package의 약자로, 데비안 계열의 리눅스 배포판에서 사용되는 패키지 관리 시스템입니다. dpkg는 .deb 파일을 설치하거나 제거하거나 정보를 조회하는 등의 기능을 제공합니다.
apt는 Advanced Packaging Tool의 약자로, dpkg를 보완하는 패키지 관리 도구입니다. apt는 인터넷이나 로컬 저장소에서 패키지를 다운로드하고 설치하며, 의존성 문제를 자동으로 해결해줍니다.

dpkg 패키지를 만들기 위해서는 다음과 같은 과정이 필요합니다.

1. 패키징 대상 디렉토리 생성
2. DEBIAN 디렉토리 생성 및 control 파일 작성
3. 설치할 파일 및 디렉토리 구조 생성
4. dpkg 명령어로 .deb 파일 생성

예시: hello-world 프로그램을 패키징하기

hello-world 프로그램은 /usr/bin/hello-world에 실행파일이 있고, /usr/share/doc/hello-world에 문서가 있는 간단한 프로그램이라고 가정합시다.

1. 패키징 대상 디렉토리 생성

$ mkdir hello-world-1.0

2. DEBIAN 디렉토리 생성 및 control 파일 작성

$ mkdir hello-world-1.0/DEBIAN
$ vi hello-world-1.0/DEBIAN/control

control 파일은 다음과 같은 형식으로 작성합니다.

Package: hello-world
Version: 1.0
Section: utils
Priority: optional
Architecture: amd64
Maintainer: Your Name <your@email.com>
Description: A simple program that prints "Hello, world!"
 This is a long description of the package.

3. 설치할 파일 및 디렉토리 구조 생성

$ mkdir -p hello-world-1.0/usr/bin
$ mkdir -p hello-world-1.0/usr/share/doc/hello-world
$ cp /path/to/hello-world hello-world-1.0/usr/bin/
$ cp /path/to/hello-world-doc/* hello-world-1.0/usr/share/doc/hello-world/

4. dpkg 명령어로 .deb 파일 생성

$ dpkg -b hello-world-1.0

위 과정이 완료되면 hello-world-1.deb 파일이 생성됩니다

dpkg 패키지를 설치하고 제거하기

dpkg 패키지를 설치하기 위해서는 다음과 같은 명령어를 사용합니다.

$ sudo dpkg -i hello-world-1.deb

설치된 패키지의 정보를 확인하기 위해서는 다음과 같은 명령어를 사용합니다.

$ dpkg -l | grep hello-world

패키지를 제거하기 위해서는 다음과 같은 명령어를 사용합니다.

$ sudo dpkg -r hello-world


# preinst란?

preinst는 패키지의 설치 전에 실행되는 메인터너 스크립트입니다. preinst 파일은 패키지의 control 아카이브(즉, 패키지 생성 시 DEBIAN/preinst)에 실행 가능한 파일로 포함되어 있습니다.

- 패키지의 의존성을 확인하고 필요한 경우 다른 패키지를 설치하거나 제거하기
- 지의 구성 파일을 백업하거나 복원하기
- 지와 충돌할 수 있는 다른 프로그램이나 서비스를 중단시키기
- 지가 필요로 하는 디렉토리나 사용자 계정 등을 생성하기

preinst는 쉘 스크립트 또는 다른 실행 가능한 언어로 작성할 수 있습니다. preinst 파일은 반드시 첫 줄에 #! (shebang)으로 시작해야 하며, 실행 권한이 있어야 합니다.

preinst 파일은 dpkg 명령어로 패키지를 설치할 때 자동으로 실행됩니다. preinst 파일은 DPKG_MAINTSCRIPT_NAME 환경 변수에 preinst라고 정의되어 있습니다2.

예시: hello-world 프로그램의 preinst 스크립트

다음은 hello-world 프로그램의 preinst 스크립트 예시입니다.

#!/bin/sh

# Check if hello-user package is installed
if ! dpkg -s hello-user >/dev/null 2>&1; then
  # Install hello-user package from local file
  dpkg -i /path/to/hello-user.deb || exit 1
fi

# Create a backup of /etc/hello-world.conf if it exists
if [ -f /etc/hello-world.conf ]; then
  cp /etc/hello-world.conf /etc/hello-world.conf.bak || exit 1
fi

# Stop any running instance of hello-world service
if [ -x /usr/bin/systemctl ]; then
  systemctl stop hello-world.service || exit 1
fi

exit 0


## postinst란?

postinst는 패키지의 설치 후에 실행되는 메인터너 스크립트입니다. postinst 파일은 패키지의 control 아카이브(즉, 패키지 생성 시 DEBIAN/postinst)에 실행 가능한 파일로 포함되어 있습니다.

- 패키지의 구성 파일을 업데이트하거나 복구하기
- 패키지와 관련된 프로그램이나 서비스를 시작하거나 재시작하기
- 패키지와 관련된 데이터베이스나 캐시를 초기화하거나 갱신하기
- 패키지와 관련된 사용자에게 안내 메시지를 출력하기

postinst는 쉘 스크립트 또는 다른 실행 가능한 언어로 작성할 수 있습니다. postinst 파일은 반드시 첫 줄에 #! (shebang)으로 시작해야 하며, 실행 권한이 있어야 합니다.

postinst 파일은 dpkg 명령어로 패키지를 설치할 때 자동으로 실행됩니다. postinst 파일은 DPKG_MAINTSCRIPT_NAME 환경 변수에 postinst라고 정의되어 있습니다.

예시: hello-world 프로그램의 postinst 스크립트

다음은 hello-world 프로그램의 postinst 스크립트 예시입니다.

#!/bin/sh

# Update /etc/hello-world.conf with new settings if it exists
if [ -f /etc/hello-world.conf ]; then
  sed -i 's/old_setting/new_setting/g' /etc/hello-world.conf || exit 1
fi

# Start or restart hello-world service
if [ -x /usr/bin/systemctl ]; then
  systemctl enable hello-world.service || exit 1
  systemctl start hello-world.service || exit 1
fi

# Print a message to the user
echo "Thank you for installing hello-world!"

exit 0