SECURITY/OpenWrt

OpenWrt: Package νŒ¨ν‚€μ§€ λΉŒλ“œ :: Hello, world!

\b\t 2021. 7. 22. 01:46

λͺ¨λ“  ν”„λ‘œκ·Έλž˜λ°μ˜ 첫 μ‹œμž‘μ€ Hello, world! λž¬λ‹€.

κ·ΈλŸ¬λ‹ˆ μ§€κΈˆ ν¬μŠ€νŒ…ν•  λ‚΄μš©λ„ OpenWrt 에 helloworld λΌλŠ” νŒ¨ν‚€μ§€λ₯Ό λΉŒλ“œν•˜κ³  μ„€μΉ˜ν•˜λŠ” λ‚΄μš©μ΄λ‹€.

 

μ•„λž˜ λ‚΄μš©μ€ OpenWrt helloworld 곡식 λ¬Έμ„œλ₯Ό μ°Έκ³ ν•˜μ˜€μŠ΅λ‹ˆλ‹€.

https://openwrt.org/docs/guide-developer/helloworld/start

 


Part 1. helloworld νŒ¨ν‚€μ§€ μž‘μ„±ν•˜κ³  λΉŒλ“œν•˜κΈ°

 

λ¨Όμ € SDK κ°€ ν•„μš”ν•˜λ‹€.

λ‚˜μ˜ 경우, OpenWrt SDK 도컀 μ»¨ν…Œμ΄λ„ˆλ‘œ ν•΄λ‹Ή ν™˜κ²½μ„ κ΅¬μΆ•ν–ˆλ‹€.

ν•˜μ§€λ§Œ μžμ‹ μ˜ 기기에 λ§žλŠ” μ†ŒμŠ€ μ½”λ“œμ—μ„œ SDK 파일 λ˜ν•œ μ œκ³΅ν•˜κ³  μžˆμœΌλ‹ˆ, Linux μ—μ„œ κ·Έκ±Έ λ‹€μš΄λ°›κ³  진행해도 상관없겠닀.

 

λ‹€λ§Œ staging_dir 은 ꡐ차 μ»΄νŒŒμΌλ§μ„ μœ„ν•΄ ν•„μš”ν•˜λ‹ˆ, ν™•μΈν•˜κΈ°!

 

λ¨Όμ € μ†ŒμŠ€ μ½”λ“œλ₯Ό μž‘μ„±ν•΄μ£Όμž.

$ cd /home/build/openwrt/package/mypackages/examples/helloworld/
$ vi helloworld.c

vi 에디터 (μ—†μœΌλ©΄ λ‹€λ₯Έ ν”„λ‘œκ·Έλž¨ μ‚¬μš©ν•˜κ±°λ‚˜, vim μ„€μΉ˜ ꢌμž₯) λ₯Ό μ‚¬μš©ν•˜μ—¬ helloworld.c λΌλŠ” μ΄λ¦„μ˜ κ°„λ‹¨ν•œ μ†ŒμŠ€ μ½”λ“œλ₯Ό μž‘μ„±ν•˜μž.

κ²½λ‘œλŠ” μ–΄λ””λ“  상관 μ—†μœΌλ‚˜, λ‚˜μ€‘μ— 이 경둜λ₯Ό μ†ŒμŠ€ μ½”λ“œ ν΄λ”λ‘œ 지정해야 ν•˜κΈ° λ•Œλ¬Έμ— κΈ°μ–΅ν•΄μ£Όκ³  μžˆμ–΄μ•Ό ν•œλ‹€.

(μ € 폴더가 μ‘΄μž¬ν•΄μ•Ό ν•˜λŠ”λ°, μ΄λŠ” 밑에 λ‚΄μš© ν•˜λ‹€κ°€ 경둜λ₯Ό μƒμ„±ν•œ 것이기 λ•Œλ¬Έμ— μ•„λž˜ λ‚΄μš© πŸŽ‰ μ°Έκ³ )

#include <stdio.h>

int main(){
	printf("Hello, world!\n\n");
    return 0;
}

Hello, world λ₯Ό 좜λ ₯ν•˜λŠ” κ°„λ‹¨ν•œ C μ½”λ“œ 이닀.

 

이 아이λ₯Ό ν•œλ²ˆ μ»΄νŒŒμΌν•΄λ³΄μž. 잘 μž‘μ„±ν–ˆλŠ”μ§€ 확인차!

$ gcc -o helloworld helloworld.c
$ ./helloworld

좜λ ₯이 잘 되면 μ„±κ³΅ν•œ 것이닀.

 

이제 본격적으둜 νŒ¨ν‚€μ§€λ₯Ό λ§Œλ“€μ–΄λ³΄μž.

 

νŒ¨ν‚€μ§€λ₯Ό λ§Œλ“€ 폴더λ₯Ό μƒμ„±ν•œλ‹€. (πŸŽ‰)

$ mkdir -p /home/build/openwrt/package/mypackages/examples/helloworld
$ cd /home/build/openwrt/package/mypackages/examples/helloworld

(이해λ₯Ό μœ„ν•΄ 계속 μ ˆλŒ€κ²½λ‘œλ‘œ ν•˜κ³ λŠ” μžˆμ§€λ§Œ, λ„ˆλ¬΄ κΈΈμ–΄μ§€λ―€λ‘œ 취사선택.. )

 

여기에 Makefile 을 λ§Œλ“€ 것이닀.

make λŠ” κ°„λ‹¨νžˆ λ§ν•΄μ„œ μ—¬λŸ¬ μž‘μ—…μ„ ν•œ 파일둜 μž‘μ„±ν•΄μ„œ κ΄€λ¦¬ν•˜λŠ” 것인데, λ‚˜μ€‘μ— κΈ°νšŒκ°€ 되면 닀뀄보도둝 ν•˜κ² λ‹€.

 

μ—¬κΈ°μ„œλŠ” OpenWrt 곡식 λ¬Έμ„œ Helloworld μ—μ„œ μ œκ³΅ν•΄μ£ΌλŠ” Makefile (https://openwrt.org/_export/code/docs/guide-developer/helloworld/chapter3?codeblock=2) 을 μ‚¬μš©ν•΄λ„ λ¬΄κ΄€ν•˜λ‹€.

단, μ†ŒμŠ€ μ½”λ“œ ν΄λ”λŠ” λ³€κ²½ν•΄μ•Ό ν•œλ‹€. : SOURCE_DIR

 

κ°„λ‹¨ν•˜κ²Œ 쑰금만 μ‚΄νŽ΄λ³΄κΈ° μœ„ν•΄ 핡심이라고 μƒκ°ν•˜λŠ” 4가지 뢀뢄을 κ°€μ Έμ™”λ‹€.

# [1]
PKG_NAME:=helloworld
PKG_VERSION:=1.0
PKG_RELEASE:=1

# [2]
SOURCE_DIR:=/home/build/openwrt/package/mypackages/examples/helloworld

# [3]
# Package definition; instructs on how and where our package will appear in the overall configuration menu ('make menuconfig')
define Package/helloworld
  SECTION:=examples
  CATEGORY:=Examples
  TITLE:=Hello, World!
endef

# [4]
# Package build instructions; invoke the target-specific compiler to first compile the source file, and then to link the file into the final executable
define Build/Compile
        $(TARGET_CC) $(TARGET_CFLAGS) -o $(PKG_BUILD_DIR)/helloworld.o -c $(PKG_BUILD_DIR)/helloworld.c
        $(TARGET_CC) $(TARGET_LDFLAGS) -o $(PKG_BUILD_DIR)/$1 $(PKG_BUILD_DIR)/helloworld.o
endef
  • [1] νŒ¨ν‚€μ§€ 이름/버전/릴리즈 정보λ₯Ό μž‘μ„±ν•˜λŠ” 곳이닀. μΆ”ν›„ 생성할 νŒ¨ν‚€μ§€ νŒŒμΌλ„ 이 λ‚΄μš©μ„ 기반으둜 μž‘μ„±λœλ‹€.
  • [2] μ†ŒμŠ€ μ½”λ“œκ°€ μœ„μΉ˜ν•˜λŠ” ν΄λ”μ˜ κ²½λ‘œμ΄λ‹€.
  • [3] 전체 configuration μ—μ„œ 우리 νŒ¨ν‚€μ§€λ₯Ό 어디에 μœ„μΉ˜ν• κ±°λƒλŠ” 것이닀
    타이틀 정보에 λ”°λΌμ„œ 보여지고, μΉ΄ν…Œκ³ λ¦¬λŠ” make menuconfig λ₯Ό ν•˜λ©΄ λ‚˜μ˜€λŠ” μ°½μ—μ„œμ˜ μΉ΄ν…Œκ³ λ¦¬λ“€μ„ μ˜λ―Έν•œλ‹€. μ΄λŠ” λ‚˜μ€‘μ— make menuconfig λ₯Ό 해보면 감이 λ°”λ‘œ 올 것이닀. (🚩)
  • [4] compile 속성을 μ •μ˜ν•˜λŠ” 뢀뢄이닀. μœ„μ—μ„œ gcc -o helloworld helloword.c λ₯Ό ν–ˆλ˜ κ²ƒμ²˜λŸΌ, 컴파일러λ₯Ό μ§€μ •ν•˜κ³  μ—¬λŸ¬ μ˜΅μ…˜μœΌλ‘œ μ΅œμ’… μ‹€ν–‰ νŒŒμΌμ„ 컴파일 ν•˜λŠ” 과정이닀. $() 을 λΉΌκ³  보면 이해가 더 λ μˆ˜λ„..

 

이제

OpenWrt build system 은 feeds.conf λΌλŠ” νŒŒμΌμ„ μ‚¬μš©ν•΄μ„œ firmware configuration stage μ—μ„œ μ‚¬μš©ν•  수 μžˆλ„λ‘ λ§Œλ“€μ–΄μ§ˆ package feeds λ₯Ό κ΄€λ¦¬ν•œλ‹€.

이 νŒŒμΌμ— 우리의 νŒ¨ν‚€μ§€λ₯Ό 적어주면 λ˜λŠ”λ°, openwrt 루트 폴더에 이미 μžˆμ„ μˆ˜λ„,  없을 μˆ˜λ„ μžˆλ‹€.

λ‚˜μ˜ 경우 이미 μ‘΄μž¬ν•˜λŠ” feess.conf.default 파일이 μ‘΄μž¬ν•˜μ—¬ κ·Έ νŒŒμΌμ— νŒ¨ν‚€μ§€ 정보λ₯Ό μž‘μ„±ν•˜μ˜€λ‹€.

λ‹€μŒκ³Ό 같이 μž‘μ„±ν•˜μ˜€λ‹€.

$ src-link mypackages /home/build/openwrt/package/mypackages

 

이걸 μ €μž₯ν•΄μ£Όλ©΄ 이제 feeds λ₯Ό μ—…λ°μ΄νŠΈν•˜κ³  μ„€μΉ˜ν•  μ‹œκ°„μ΄λ‹€.

λ‹€μŒμ„ μž…λ ₯ν•΄μ£Όλ©΄ λœλ‹€.

λŒ€μΆ© "이제 helloworld λΌλŠ” νŒ¨ν‚€μ§€λ₯Ό λΉŒλ“œν•  λ•Œ 인식할 수 μžˆλ„λ‘ λ“±λ‘ν•΄μ€¬κ΅¬λ‚˜" 라고 μƒκ°ν•˜λ©΄ λœλ‹€.

$ cd /home/build/openwrt
$ ./scripts/feeds update mypackages
$ ./scripts/feeds install -a -p mypackages

 

 

이제 우리의 νŒ¨ν‚€μ§€κ°€ firmware configuration 에 ν¬ν•¨λ˜λ„λ‘, λ‹€μŒ λͺ…λ Ήμ–΄λ₯Ό 치고 섀정을 ν•΄λ³΄μž.

$ make menuconfig

그럼 λ‹€μŒκ³Ό 같은 창으둜 μ „ν™˜λœλ‹€.

 

κ·Έ 쀑 Examples 클릭 (🚩)

μ•„κΉŒ Makefile 에 μž‘μ„±ν•œ κ²ƒμ²˜λŸΌ, Examples μΉ΄ν…Œκ³ λ¦¬μ— λ“€μ–΄κ°€λ©΄ helloworld νŒ¨ν‚€μ§€κ°€ 보일 κ²ƒμœΌλ‘œ μ˜ˆμƒλœλ‹€.

 

 

μ—­μ‹œ μš°λ¦¬κ°€ μž‘μ„±ν•œ helloworld κ°€ 보인닀!

우츑 μ„€λͺ…도 Makefile 에 μž‘μ„±ν•œ λŒ€λ‘œ νŒ¨ν‚€μ§€κ°€ λ³΄μ΄λŠ” 것을 확인할 수 μžˆλ‹€.

 

 

μ„ νƒλœ μƒνƒœμ—μ„œ (νŒŒλž€μƒ‰ ν•˜μ΄λΌμ΄λ“œ) Y λ₯Ό λˆ„λ₯΄λ©΄ μ„ νƒλœλ‹€.

μ΄λŠ” 이 νŒ¨ν‚€μ§€λ₯Ό firmware configuration 에 ν¬ν•¨ν•œλ‹€λŠ” 것을 μ˜λ―Έν•œλ‹€.

 

 

save λˆ„λ₯΄λ©΄ μ•„λž˜μ™€ 같이 .config μ €μž₯이 뜨고, ok λˆ„λ₯΄κ³  exit ν•˜λ©΄ λœλ‹€.

(λͺ¨λ“  μ‘°μž‘μ€ ν‚€νŒ¨λ“œλ‘œ, 쒌-우-μ—”ν„° λ₯Ό 해보면 감이 작힐 것이닀.)

 

 

이제 우리의 νŒ¨ν‚€μ§€λ₯Ό μ»΄νŒŒμΌν•  μ€€λΉ„κ°€ 끝났닀! (_μ§„μ§œ_μ§„μ§œ_μ΅œμ’…)

 

λ‹€μŒ λͺ…λ Ήμ–΄λ₯Ό μ‹€ν–‰ν•΄μ£Όμž.

$  make package/helloworld/compile

μ—¬κΈ°μ„œ λ‹€μŒκ³Ό  같이 μ„±κ³΅ν•˜λ©΄ (= μ‹€νŒ¨ν–ˆλ‹€κ³  λœ¨μ§€ μ•ŠμœΌλ©΄) νŒ¨ν‚€μ§€ λΉŒλ“œκ°€ μ™„λ£Œλœ 것이닀!

 

 

단, 였λ₯˜κ°€ λ‚˜κΈ°λ„ ν•˜λŠ”λ° 그런 κ²½μš°μ—” V=s -j1 으둜 μ˜΅μ…˜μ„ μ£Όκ³  μ‚΄νŽ΄λ³΄μž.

λ‚˜μ˜ 경우 Makefile μ—μ„œ μ†ŒμŠ€ μ½”λ“œ 폴더λ₯Ό 잘λͺ» μ§€μ •ν–ˆμ—ˆλ‹€;; (μ•ŒνŒŒλ²³ ν•˜λ‚˜λ‘œ λ‹¬λΌμ§€λŠ” 0κ³Ό 1의 γ……-ㅔ계)

ν•΄λ‹Ή κ²½μš°λŠ” μ•„λž˜μ™€ κ°™μ•˜λ‹€.

μ˜΅μ…˜μ„ μ£Όκ³  λ‹€μ‹œ μ‹€ν–‰ν•œ κ²°κ³Ό 였λ₯˜λ₯Ό μžμ„Ένžˆ μ•Œ 수 μžˆμ—ˆλ‹€.

 

 

λΉŒλ“œν•œ νŒ¨ν‚€μ§€ νŒŒμΌμ€ /bin μͺ½μœΌλ‘œ κ°€λ©΄ 찾을 수 μžˆλ‹€.

 

ipk 파일이 λ³΄μ΄λŠ”λ°, 그게 λ°”λ‘œ νŒ¨ν‚€μ§€ νŒŒμΌμ΄λ‹€.

이둜써 helloworld νŒ¨ν‚€μ§€ νŒŒμΌμ„ μž‘μ„±ν•˜κ³  λΉŒλ“œκΉŒμ§€ μ™„λ£Œν–ˆλ‹€!

 


Part 2. OpenWrt 에 helloworld νŒ¨ν‚€μ§€ μ„€μΉ˜

 

이제 이 νŒŒμΌμ„ OpenWrt ν…ŒμŠ€νŠΈ 도컀 μ»¨ν…Œμ΄λ„ˆμ— λ³΄λ‚΄μ„œ μ„€μΉ˜ν•΄λ³Ό 것이닀.

(κ΄€λ ¨ λ‚΄μš©μ€

2021.07.14 - [PROGRAMMING/기타] - Docker for windows 10 : 도컀 개랡, windows 10 에 도컀 μ„€μΉ˜ν•˜κΈ° 방법

μ—μ„œ μžμ„Ένžˆ λ‹€λ£¨μ—ˆλ‹€.)

 

 

λ‹€μŒκ³Ό 같이 2개의 도컀 μ»¨ν…Œμ΄λ„ˆλ₯Ό μ€€λΉ„ν•˜μž.

μ™Όμͺ½μ€ OpenWrt μ»¨ν…Œμ΄λ„ˆ (μ΄ν•˜ A) κ³ , 였λ₯Έμͺ½μ€ μ—¬νƒœ μž‘μ—…ν•˜λ˜ OpenWrt SDK 도컀 μ»¨ν…Œμ΄λ„ˆ (μ΄ν•˜ B) λ‹€.

(λ‘˜ λ‹€ 도컀가 μ•„λ‹Œ λ‹€λ₯Έ ν™˜κ²½μœΌλ‘œ κ΅¬μΆ•ν•˜μ—¬λ„ 무관!)

 

B μ—μ„œ λ‹€μŒκ³Ό 같이 μž…λ ₯ν•΄μ£Όμž.

$ scp helloworld_1.0-1_x86_64.ipk root@172.17.0.4:/

(ip μ£Όμ†Œμ™€ 파일λͺ…은 μžμ‹ μ— 맞게 λ³€κ²½!)

 

잘 μ „μ†‘λœ 것을 μ•„λž˜μ™€ 같이 확인할 수 μžˆλ‹€.

 

 

이제 opkg 둜 전솑받은 helloworld ipk νŒŒμΌμ„ μ„€μΉ˜ν•΄μ£Όμž.

그리고 helloworld λ₯Ό μž…λ ₯ν•˜λ©΄ λ‹€μŒκ³Ό 같이 μš°λ¦¬κ°€ μž‘μ„±ν•œ μ½”λ“œκ°€ μ‹€ν–‰λ˜λ©° "Hello, world!" κ°€ 좜λ ₯λœλ‹€.

# opkg install helloworld_1.0-1_x86_64.ipk
# helloworld

 


 

μ΄λ ‡κ²Œ helloworld νŒ¨ν‚€μ§€λ₯Ό μž‘μ„±ν•˜κ³  OpenWrt 에 μ„€μΉ˜κΉŒμ§€ λ§ˆμ³€λ‹€.

 

* /home/build/openwrt/package/mypackages/examples/helloworld/
*~/openwrt/bin/packages/x86_64/base