리눅스 시스템 부팅 절차

 

이전 버전들의 리눅스 init 프로세스의 부팅 순서와 CentOS7 버전의 systemd에서 부팅 순서는 약간 다릅니다.

 

커널을 메모리에 적재하는 부분까지는 동일하나 그 이후에 실행하는 첫 번째 프로세스가 다릅니다.

init 프로세스의 경우 Run level을 실행하며 systemd 의 경우 Target unit을 실행합니다.

 

init 프로세스 부팅 절차

 

1. BIOS/UEFI

 시스템에 전원이 공급되면 시스템 펌웨어인 BIOS나 UEFI가 특정 프로그램으로 하드웨어를 점검하는 POST를 진행합니다.

이때 정상적으로 연결되지 않은 하드웨어가 존재하거나 또는 손상된 하드웨어가 발견되면 부팅되지 않습니다.

 

이후 연결된 장치 중 부팅이 가능한 장치에서 MBR 파티션의 첫 번째 섹터인 512byte를 읽어 Boot loader를 찾습니다. 그리고 Boot loader를 메모리에 적재한 뒤 시스템의 제어권을 Boot loader에게 전달합니다.

 

2. Boot Loader

부트 로더가 메모리에 적재되면 /boot/grub2/grub.cfg , /etc/default/grub , /etc/grub.d 디렉터리에 존재하는 파일들을 메모리에 적재하고, 부팅 가능한 커널의 목록을 화면에 출력합니다.

 

 

이 화면에서 'e' 를 누르면 /boot/grup2/grub.cfg 파일의 내용이 출력되고 설정값을 변경하여 변경된 값으로 커널을 실행할 수 있습니다.

또한, 이 단계에서 root 패스워드 복구를 위한 Ram disk 초기화 중단이나 시스템 부팅 시 Target Unit을 지정할 수 있습니다.

 

부팅하려는 커널 목록을 선택하면 /boot 디렉터리에 vmlinuz로 시작하는 커널 파일을 메모리에 적재시키고 시스템 제어권을 커널에게 전달합니다.

 

 

3. 커널 (Kernel)

커널이 메모리에 적재되면 /boot 디렉토리에 존재하는 initramfs 압축파일을 해제합니다. 동시에 램디스크 초기화를 진행하고, 루트 파일 시스템은

/sysroot에 임시로 마운트됩니다.

 

initramfs 파일은 /sysroot에 압축을 해제하고 필요한 파일들을 메모리에 적재시키는데, 이때 init프로세스를 사용하는 시스템은 init프로세스를 실행합니다. 그리고 시스템 제어권을 init 프로세스에게 전달합니다.

 

4. init

init 프로세스에는 PID 1번이 할당되고 필요한 모든 프로세스를 실행합니다. 그 뒤 /etc/initab 파일에 등록된 정보를 토대로 런 레벨이 결정되고, /etc/rc.d/rc.sysinit 파일을 실행하여 스왑이나 네트워크 설정을 초기화합니다. 마지막으로 /etc/initab 파일에 지정된 런 레벨을 실행하고 사용자에게 로그인 화면을 출력합니다.

 

 

systemd 부팅 절차

 

1. systemd

 

systemd의 부팅 절차는 init 프로세스와 마찬가지로 커널을 메모리에 적재시키는 부분까지 동일합니다. 그리고 initramfs 파일을 해제하고 systemd를 실행하여 메모리에 적재시킵니다.

systemd는 default.target을 활성화하고 의존성 관계에 있는 유닛들을 모두 활성화합니다.

 

2. default.target

default.target의 유닛 파일 자체는 multi-user.target 또는 graphical.target으로 연결된 심벌링 링크 파일입니다.

default.target은 가장 먼저 실행되어 어떤 타깃 유닛으로 부팅을 진행할지 정하게 됩니다.

 

ls -l /etc/systemd/system/default.target

 

 

3. graphical.target

 

graphical.target은 GUI를 지원하는 다중 사용자 모드입니다.  init 프로세스의 런 레벨 5와 매핑됩니다.

 

4. multi-user.target

 

multi-user.target은 커맨드 라인을 제공하는 다중 사용자 모드입니다.  init 프로세스의 런레벨 3과 매핑됩니다.

시스템이 부팅될 때 multi-user.target 단계에서는 /etc/systemd/system/multi-user.target.wants 내에 존재하는 유닛들을 실행합니다.

 

5. basic.target

 

basic.target 은 firewalld, microcode, SELinux, 커널 메시지와 관련된 서비스를 시작하거나 모듈을 Load 합니다.

시스템이 부팅될 때 basic.target 단계에서는 /etc/systemd/system/basic.target.wants 디렉터리에 존재하는 유닛들을 존재합니다.

 

6. sysinit.target

 

sysinit.target 은 시스템 마운트 , 스왑 , 커널의 추가 옵션을 실행하는 서비스를 실행합니다.

 

7. local-fs.target

local-fs.target은 /etc/fstab에 등록된 마운트 정보로 파일 시스템을 마운트 합니다.

 

 

systemd 종료

systemd에선 시스템을 종료할 때 시스템에 등록된 모든 서비스를 종료하지 않고 실행 중인 서비스만 종료합니다.

 

systemctl poweroff

 

systemctl reboot

 

 

systemd 타겟 유닛

타겟 유닛은 유닛을 그룹화시키고 시스템 부팅 시 유닛의 동기화 포인트로 사용됩니다.  systemd 부팅 절차에서 default.target부터  sysinit.target까지 각 타겟 유닛의 이름뒤에 .wants 또는 .requires 가 추가된 디렉토리가 존재합니다.

 

다음은 시스템에 Load 된 타겟 유닛들입니다.

[root@localhost ~]# systemctl list-units -t target
UNIT                   LOAD   ACTIVE SUB    DESCRIPTION
basic.target           loaded active active Basic System
cryptsetup.target      loaded active active Local Encrypted Volumes
getty-pre.target       loaded active active Login Prompts (Pre)
getty.target           loaded active active Login Prompts
graphical.target       loaded active active Graphical Interface
local-fs-pre.target    loaded active active Local File Systems (Pre)
local-fs.target        loaded active active Local File Systems
multi-user.target      loaded active active Multi-User System
network-online.target  loaded active active Network is Online
network-pre.target     loaded active active Network (Pre)
network.target         loaded active active Network
nfs-client.target      loaded active active NFS client services
nss-user-lookup.target loaded active active User and Group Name Lookups
paths.target           loaded active active Paths
remote-fs-pre.target   loaded active active Remote File Systems (Pre)
remote-fs.target       loaded active active Remote File Systems
rpc_pipefs.target      loaded active active rpc_pipefs.target
rpcbind.target         loaded active active RPC Port Mapper
slices.target          loaded active active Slices
sockets.target         loaded active active Sockets
sound.target           loaded active active Sound Card
swap.target            loaded active active Swap
sysinit.target         loaded active active System Initialization
timers.target          loaded active active Timers

 

systemd 타겟 유닛과 런 레벨 매핑

 

systemd에서는 런 레벨을 사용하지 않고, 타겟유닛을 사용합니다. systemd에는 런레벨과 같은 기능을 하도록 매핑되는 타겟 유닛이 존재합니다. 하지만, 타겟 유닛과 모든 런레벨이 매핑되지는 않습니다.

런레벨과 매핑되는 타겟 유닛은 다음과 같습니다.

 

Run level Target Unit 설명
0 poweroff.target 시스템 셧다운 및 종료
1 rescue.target 복구 쉘(rescue shell)
2 multi-user.target 커맨드 환경 다중 사용자 모드
3 multi-user.target 커맨드 환경 다중 사용자 모드
4 multi-user.target 커맨드 환경 다중 사용자 모드
5 graphical.target 그래픽 환경 다중 사용자 모드
6 reboot.target 시스템 셧다운 및 재부팅

이는 /usr/lib/systemd/systemd 디렉터리에는 runlevel#.target 파일이 존재합니다.

이 파일은 링크 파일이고 실제 가리키는 원본 파일은 위 표에서 확인할 수 있습니다.

 

타겟 유닛 제어

 

사용자는 시스템을 관리하면서 원하는 타겟 유닛으로 전환할 수 있습니다. 타겟유닛을 전환할 때에는 systemctl 명령의 서브 커맨드를 사용합니다.

 

 

1. 현재 사용 중인 런레벨에 해당하는 타겟 유닛 확인

 

 

who -r 옵션을 통해 현재 사용 중인 타겟 유닛을 런레벨 형태로 확인 가능

 

2. default.target 에 연결된 타겟 유닛 확인

 

systemctl get-default 명령을 이용하여 확인 가능

 

 

3. default.target 설정

 

systemctl set-default target-unit 이용하여 확인 가능

 

 

4. 현재 사용 중인 타겟유닛을 런타임 상태에서 변경

 

 

시스템을 재부팅하지 않고 런타임 상태에서 타겟유닛 전환 가능

systemctl isolate target-unit

 

 

5. 시스테 부팅 과정 중 타겟 유닛 지정

1.boot loader 커널 항목에서 카운터 중단

2. 부팅할 커널 목록에서 'e'키를 눌러 편집 모드 진입

3. linux16으로 시작하는 줄의 제일 마지막 'systemd.unit=target-unit' 입력

4.ctrl + X를 눌러 재시작

 

+ Recent posts