OpenWrt: Package ํจํค์ง ๋น๋ :: Hello, world!
๋ชจ๋ ํ๋ก๊ทธ๋๋ฐ์ ์ฒซ ์์์ 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 ํ ์คํธ ๋์ปค ์ปจํ ์ด๋์ ๋ณด๋ด์ ์ค์นํด๋ณผ ๊ฒ์ด๋ค.
(๊ด๋ จ ๋ด์ฉ์
์์ ์์ธํ ๋ค๋ฃจ์๋ค.)
๋ค์๊ณผ ๊ฐ์ด 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