프로그래밍/Mysis 연구실

EtherCAT Master for Linux 설치

lastdefiance20 2022. 1. 13. 18:27

저는 우분투 환경을 가진 데스크탑이 없기 때문에 window에서 vmware를 이용하여 가상 ubuntu 환경에 ethercat master를 설치하게 되었습니다.

 

우분투 환경에 적어도 gcc와 g++, make 등 기본 라이브러리정도는 설치되어야 빌드할 수 있습니다. 설치되지 않아 에러메세지가 발생할 경우 그때 추가적으로 pip을 통해 설치해주어도 괜찮습니다.

 

설치하는 과정을 적은 이유는 EtherCAT 1.5.2 documentation에 나와 있지만 상세하게 설치하는 과정이나, 에러 해결법이 인터넷에 자세히 나와있지 않기 때문입니다. (설치하는데 랩실 선배님의 도움을 받았음에도 일주일을 소모함)

 

추후 EtherCAT Master을 Linux에 설치하는 사람에게 유용하게 사용되길 바랍니다.

 

.1 Getting the Software

우선 Ethercat 파일을 다운받아야 합니다. 여기서 1.5.2버전 말고 git에 올라와있는 최신 버전을 받았습니다. 1.5.2 버전을 받아서 빌드할 경우에는 make modules 단계에서 이와 같은 에러가 뜨기 때문입니다.

/home/lastdefiance/ethercat-1.5.2/examples/mini/mini.c: In function ‘init_mini_module’:
/home/lastdefiance/ethercat-1.5.2/examples/mini/mini.c:495:5: error: implicit declaration of function 
‘init_timer’; did you mean ‘init_timers’? [-Werror=implicit-function-declaration]
  495 |     init_timer(&timer);
      |     ^~~~~~~~~~
      |     init_timers
/home/lastdefiance/ethercat-1.5.2/examples/mini/mini.c:496:20: error: assignment to 
‘void (*)(struct timer_list *)’ from incompatible pointer type ‘void (*)(long unsigned int)’ 
[-Werror=incompatible-pointer-types]
  496 |     timer.function = cyclic_task;
      |                    ^
cc1: some warnings being treated as errors
make[4]: *** [scripts/Makefile.build:288: /home/lastdefiance/ethercat-1.5.2/examples/mini/mini.o] Error 1
make[3]: *** [scripts/Makefile.build:519: /home/lastdefiance/ethercat-1.5.2/examples/mini] Error 2
make[2]: *** [scripts/Makefile.build:519: /home/lastdefiance/ethercat-1.5.2/examples] Error 2
make[1]: *** [Makefile:1849: /home/lastdefiance/ethercat-1.5.2] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-5.11.0-46-generic'

여기서 init_timer를 init_timers라고 바꾸는 것을 권유하는 점을 포착하여 google에 검색하였더니 지금 우분투 커널이 상위 버전 (2.X가 아닌 4.X 이상을 사용할때 나타나는 버전 에러, 5.11.0-46 을 사용중이다.) 이라 호환되지 않는다고 합니다. 커널 버전을 낮출 수도 있으나, 그냥 현재 커널 버전을 유지하기 위해서 최신 버전을 받아서 사용했습니다.

 

https://gitlab.com/etherlab.org/ethercat

 

EtherLab / EtherCAT

IgH EtherCAT Master for Linux

gitlab.com

위에 있는 git 주소에서 tar.bz2 형식으로 ethercat master 파일을 받고, 이후에는 instruction이 담겨져 있는 ethercat-1.5.2 pdf 파일대로 설치를 진행하면 된다.

 

9.2 Building the Software

$ tar xjf ethercat-master.tar.bz2
$ cd ethercat-master/

우선 unzip후 폴더 내부로 이동합니다.

 

$ ./bootstrap
$ ./configure
$ make
$ make modules

이후 software build를 시작합니다.

 

./bootstrap 진행시

configure.ac:62: error: possibly undefined macro: AC_PROG_LIBTOOL
      If this token and others are legitimate, please use m4_pattern_allow.
      See the Autoconf documentation.
autoreconf: /usr/bin/autoconf failed with exit status: 1

다음과 같은 에러가 뜬다면 LIBTOOL 라이브러리가 설치되지 않아서 일어나는 에러이기 때문에 libtool을 설치해줍니다.

$ sudo apt-get install libtool

 

./configure 진행시

checking for kernel for 8139too driver... configure: error: kernel 5.11 not available for 8139too driver!

다음과 같은 에러가 뜬다면 리눅스 커널 버전과 8139too가 호환되지 않는다는 것입니다. 이때는 ./configure 대신 이 코드를 사용합니다.

./configure --enable-generic --disable-e100 --disable-8139too --disable-e1000 --disable-r8169

generic 버전으로 configure을 진행한다는 말인 것 같습니다.

 

9.3 Building the Interface Documentation

이후 make modules까지 error코드나 말 없이 잘 진행되었다면 interface documation을 build합니다.

$ make doc

 

이때 doxygen이 설치되어있지 않아 doxygen: command not found라는 에러가 뜬다면

sudo apt-get install doxygen

을 통해 doxygen을 설치한 후 다시 코드를 실행시킵니다.

 

9.4 Installing the Software

$ make install
$ make modules_install

이제 install을 통해 설치를 진행합니다.

/usr/bin/install: cannot create regular file '/usr/local/include/ecrt.h': Permission denied
/usr/bin/install: cannot create regular file '/usr/local/include/ectty.h': Permission denied

코드실행시 이와 같은 Permission denied와 같은 에러가 발생할 경우 sudo를 앞에 붙여서 관리자 권한으로 진행하면 됩니다.(sudo make install 등)

 

make modules_install 진행시

make -C "/usr/src/linux-headers-5.11.0-46-generic" M="/home/lastdefiance/ethercat-master" INSTALL_MOD_DIR="ethercat" modules_install
make[1]: Entering directory '/usr/src/linux-headers-5.11.0-46-generic'
  INSTALL /home/lastdefiance/ethercat-master/devices/ec_generic.ko
At main.c:160:
- SSL error:02001002:system library:fopen:No such file or directory: ../crypto/bio/bss_file.c:69
- SSL error:2006D080:BIO routines:BIO_new_file:no such file: ../crypto/bio/bss_file.c:76
sign-file: certs/signing_key.pem: No such file or directory
  INSTALL /home/lastdefiance/ethercat-master/examples/mini/ec_mini.ko
At main.c:160:
- SSL error:02001002:system library:fopen:No such file or directory: ../crypto/bio/bss_file.c:69
- SSL error:2006D080:BIO routines:BIO_new_file:no such file: ../crypto/bio/bss_file.c:76
sign-file: certs/signing_key.pem: No such file or directory
  INSTALL /home/lastdefiance/ethercat-master/master/ec_master.ko
At main.c:160:
- SSL error:02001002:system library:fopen:No such file or directory: ../crypto/bio/bss_file.c:69
- SSL error:2006D080:BIO routines:BIO_new_file:no such file: ../crypto/bio/bss_file.c:76
sign-file: certs/signing_key.pem: No such file or directory
  DEPMOD  5.11.0-46-generic
Warning: modules_install: missing 'System.map' file. Skipping depmod.
make[1]: Leaving directory '/usr/src/linux-headers-5.11.0-46-generic'

다음과 같은 에러가 뜬다면 모듈 서명 확인이 필요하다는 뜻입니다.

sign-file: certs/signing_key.pem: No such file or directory
  DEPMOD  5.11.0-46-generic

이 에러 코드에 주목해서 해결해야 합니다.

 

이 에러의 경우, https://github.com/andikleen/simple-pt/issues/8 여기에서는 bastoica라는 사람이 4.4.0.20 이상에서는 서명되지 않은 커널 모듈을 로드하지 못하도록 하게 되어있기 때문에, 키를 이용하여 서명하거나, 모듈 서명 확인을 비활성화하거나, 보안 부팅을 완전히 끄는 3가지 해결책이 있다고 한다.

 

저는 키를 이용해서 서명하는 방법을 선택하였고 해결책은

https://superuser.com/questions/1214116/no-openssl-sign-file-signing-key-pem-leads-to-error-while-loading-kernel-modules/1322832#1322832

 

No OpenSSL sign-file signing_key.pem leads to error while loading kernel modules

I am having problems while loading/installing a kernel module. The kernel module builds successfully, but whenever I try to make module_install, the kernel seems to fail to load. It leaves the below

superuser.com

이곳에 key를 생성하는 방법이 나와있습니다. key를 생성하는 방법은 간단합니다.

cd /usr/src/linux/certs
nano x509.genkey

명령어를 통해 /usr/src/linux/certs로 들어가 vi, nano 등 상관없이 x509.genkey파일을 생성합니다.

[ req ]
default_bits = 4096
distinguished_name = req_distinguished_name
prompt = no
string_mask = utf8only
x509_extensions = myexts

[ req_distinguished_name ]
CN = Modules

[ myexts ]
basicConstraints=critical,CA:FALSE
keyUsage=digitalSignature
subjectKeyIdentifier=hash
authorityKeyIdentifier=keyid

이후 생성된 파일에 위 내용을 붙여넣기 한 후 저장한 다음

openssl req -new -nodes -utf8 -sha512 -days 36500 -batch -x509 -config x509.genkey -outform DER -out signing_key.x509 -keyout signing_key.pem

위의 명령어를 실행하여 signing_key.pem을 생성합니다

 

이후에는 정상적으로 진행되거나 아래처럼 에러코드가 변하게 됩니다.

make -C "/usr/src/linux-headers-5.11.0-46-generic" M="/home/lastdefiance/ethercat-master" INSTALL_MOD_DIR="ethercat" modules_install
make[1]: Entering directory '/usr/src/linux-headers-5.11.0-46-generic'
  INSTALL /home/lastdefiance/ethercat-master/devices/ec_generic.ko
  INSTALL /home/lastdefiance/ethercat-master/examples/mini/ec_mini.ko
  INSTALL /home/lastdefiance/ethercat-master/master/ec_master.ko
  DEPMOD  5.11.0-46-generic
Warning: modules_install: missing 'System.map' file. Skipping depmod.
make[1]: Leaving directory '/usr/src/linux-headers-5.11.0-46-generic'

Skipping depmod라는 warning 코드가 발생할 경우

sudo depmod -a

를 통해 depmod 명령어를 실행해주면 됩니다.

 

Skipping depmod라는 warning 코드가 발생하지 않을 경우

그대로 다음 단계를 진행하면 된다.

 

cd 를 통해 root로 돌아가 다음과 같은 명령어를 입력해 설치를 마무리합니다.

sudo cp /usr/local/etc/sysconfig/ethercat /etc/sysconfig/ethercat
sudo ln -s /usr/local/etc/init.d/ethercat /etc/init.d/ethercat

여기까지 완료하였다면 ethercat 설치가 완료되었다.

 

9.X Start the Software

root에서 다음과 같은 명령어를 입력합니다.

sudo /etc/init.d/ethercat start

여기서 이런 문구가 뜰 경우 설치가 완료된 것입니다!

Starting EtherCAT master 1.5.2  done

sudo /etc/init.d/ethercat start 진행시

Starting EtherCAT master 1.5.2 modprobe: ERROR: could not insert 'ec_master': Invalid argument
 failed

필자의 경우 이때 이런 에러가 발생했습니다.

 

여기서는 modprobe에 주목해야 한다. modprobe: ERROR으로 modprobe란 모듈 유틸리티 프로그램이라고 한다. ec_master라는 모듈이 적재되지 않은거 같으니 insmod 명령어로 ec_master이라는 모듈을 로드해서 적재해야 할 것 같습니다. 

cd /lib/modules/5.11.0-46-generic/ethercat/master

 

(여기서 커널 버전에 따라 5.11.0-46-generic이 아닌 다른 폴더 이름이 뜰 수 있다.)

로 이동한 이후,

sudo insmod ec_master.ko

로 master 폴더에 저장된 ec_master.ko 모듈을 적재해준 후 다시 start하면 해결됩니다.

 

이 일련의 과정을 따라했다면, EtherCAT master 설치가 완료되었습니다!