Development/Free Topic

Ubuntu GUI 관련 트러블슈팅. Gym, Xserver, pyglet, TeamViewer

IMCOMKING 2016. 7. 26. 20:48

OpenAI Gym을 원격서버 IPython에서 렌더링하기

# 주의 #
아래의 방법으로 하면, 결국 되기는 되는데, CUDA 드라이버가 뭔가에 의해서(아마 범블비) 지워져버려서 GPU컴퓨팅이 안된다....... 그래서 결론은 이 글 가장 아래부분의 TeamViewer를 사용하는 것을 추천한다.

-----

원격 서버에서 IPython을 통해 GYM의 렌더링 결과를 gif로 보여주기를 시도해보았다. 결국 다음 링크의 예제를 재현하는 것이 목표였으나 pyglet과 xvfb 설정에서 상당히 많은 어려움이 있었다.


재현할 대상 : http://nbviewer.jupyter.org/github/patrickmineault/xcorr-notebooks/blob/master/Render%20OpenAI%20gym%20as%20GIF.ipynb


ubuntu 14.04 기준으로 다음의 과정을 거쳐 환경을 구축한다.


1. OpenAI gym 설치

apt-get install -y python-numpy python-dev cmake zlib1g-dev libjpeg-dev xvfb libav-tools xorg-dev python-opengl libboost-all-dev libsdl2-dev swig

sudo pip install gym[all]

https://github.com/openai/gym#installing-everything


2. IPython 3.2.1 버전 설치(최신 버전에서 해도 별문제는 없을 것 같다.)

sudo pip install "ipython[notebook]==3.2.1"


3. JSAnimiation 설치

git clone https://github.com/jakevdp/JSAnimation
cd JSAnimation
sudo python setup.py install


4. 원격 서버에서의 가상 렌더링을 위한 패키지 설치 : Xvfb , bumblebee, GLX 등
(그냥 로컬에서 IPython을 띄워서 한다면, 이 과정은 생략해도 된다.)

pyglet은 간단한 2D 게임등을 렌더링할 수 있게 해주는 라이브러리이다.
xvfb는 가상 머신에서의 버추얼 비주얼 버퍼를 렌더링하게 시뮬레이션해주는 도구이다.
GLX : OpenGL Extension to the X Window System
Bumblebee : NVIDIA 그래픽카드와 인텔 그래픽카드가 동시에 장착된 머신에서 GPU를 관리한다.
xvfb-run : Xvfb를 쓰기 편하게 만든 wrapper 이다.


export DISPLAY=:99
sudo pip install pyglet
sudo apt-get update
sudo apt-get install python-opengl

sudo apt-get install wine xvfb xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic
(폰트 설치)
sudo apt-get install xserver-xorg-video-intel libgl1-mesa-dri libgl1-mesa-glx mesa-utils
(GLX 설치)
sudo apt-get install bumblebee bumblebee-nvidia primus linux-headers-generic
(범블비 설치)
sudo reboot

- 환경 체크 : 설치가 잘 되었다면, 다음의 명령을 실행해서 상태를 확인해 볼 수 있다.

echo $DISPLAY
xdpyinfo | grep GLX
grep -i glx /var/log/Xorg*
glxinfo
python -m pyglet.info
cat  /var/log/Xorg.0.log

- X server config 재생성:
sudo X -configure



5. 실행 : xvfb-run 을 할 때 반드시 파라미터를 저렇게 넣어주지 않으면 window config가 없다는 pyglet 에러 발생하니 주의할 것.(NoSuchDisplayException)

xvfb-run -s "-screen 0 1400x900x24" /bin/bash
ipython notebook --profile=nbserver
실행된 아이파이썬에서 아래의 코드 실행

http://nbviewer.jupyter.org/github/patrickmineault/xcorr-notebooks/blob/master/Render%20OpenAI%20gym%20as%20GIF.ipynb



* 다음은 온갖 다양한 상황에서 발생한 에러의 해결 과정이다.

-  Server is already active for display 3. If this server is no longer running, remove /tmp/.X3-lock and start again.

sudo rm -rdf /tmp/.X*

이렇게 하면 돌아가던 서비스가 종료된다.


- [dix] Could not init font path element
http://askubuntu.com/questions/342376/missing-fonts-in-xvfb 참조. 폰트가 없어서 폰트를 깔아주면 된다.

sudo apt-get update
sudo apt-get install wine xvfb xfonts-100dpi xfonts-75dpi xfonts-scalable xfonts-cyrillic

# sudo apt-get -y install x-ttcidfont-conf 을 해도 된다는 코멘트가 위 링크에 달려있으나 해보진 않았다.


- pyglet.canvas.xlib.NoSuchDisplayException: Cannot connect to "None" 

https://ubuntuforums.org/showthread.php?t=1344974

export DISPLAY=:99
xvfb-run -s "-screen 0 1400x900x24" /bin/bash

두 명령어에서 지정한 번호가 반드시 일치해야함!!


-  pyglet requires an X server with GLX / glxinfo --> Error: unable to open display
이 상황은 Xvfb에서 GLX를 로딩 했음에도 불구하고, 사용하지 못하는 상황.
sudo apt-get install xserver-xorg-video-intel libgl1-mesa-dri libgl1-mesa-glx 
mesa-utils


- pyglet.window.NoSuchConfigException: No standard config is available.
xvfb-run -s "-screen 0 1400x900x24" /bin/bash 와 같이, xvfb-run을 실행할 때 파라미터를 반드시 넣어줄 것.


- xvfb-run: error: Xvfb failed to start
xvfb-run 대신 Xvfb 사용. 뭔가 실행시키는 명령어 이름이 다를 수 있나봄.


- 그이외 기타 시도했던 기록. 결국 안되었음

sudo xvfb-run /bin/bash
sudo xvfb-run -e /dev/stdout /bin/bash

sudo Xvfb :5 -screen 0 1400x900x24 & /bin/bash
sudo xvfb-run :5 -screen 0 1400x900x24 & /bin/bash 


- 다음의 사이트는 아직 시도안해봄.
http://stackexchange.com/search?q=xvfb+GLX
http://askubuntu.com/questions/472928/correct-nvidiaintel-graphics-setup-in-14-04
https://www.centos.org/forums/viewtopic.php?t=55151
https://github.com/Bumblebee-Project/Bumblebee/wiki/Troubleshooting
https://bbs.archlinux.org/viewtopic.php?id=163652


----------------------------------------

위의 방법으로 remote 서버에서의 렌더링에 성공은 하였다. 그러나 동시에 CUDA드라이버가 날아가면서 nvidia GPU가 안잡혀 GPU컴퓨팅이 안된다.. bumblebee와 서로 베타적인 관계인듯.

-->그래서 그냥 teamviewer를 이용해서 렌더링을 확인하기로 하였다.


Ubuntu 14.04에서 TeamViewer 11 사용하기

- TeamViewer 커맨드라인에서 설치하기 :
wget https://download.teamviewer.com/download/teamviewer_i386.deb
sudo dpkg -i teamviewer_i386.deb
sudo apt-get -f install

https://www.teamviewer.com/en/help/1111-How-do-I-install-TeamViewer-on-a-Linux-system-with-no-graphical-user-interface


- TeamViwer 삭제: 

sudo apt-get remove teamviewer

http://askubuntu.com/questions/565255/how-to-uninstall-teamviewer-deb-v10-0-036281-from-ubuntu-14-04/565384


- TeamViewer 커맨드라인에서 실행 및 설정하기: 

teamviewer -help : 명령어로 버전 확인
sudo service lightdm stop
sudo teamviewer setup : 그다음 내 팀뷰어 아이디로 로그인. (email로 기기 추가 인증을 해야할 수도 있음)
sudo service lightdm start
다른 컴퓨터의 팀뷰어로 해당 컴퓨터에 접속

http://www.tonisoto.com/2013/07/launching-teamviewer-remotely-throught-ssh/


- 팀뷰어에서 Ubuntu 화면 설정 변경 방법
# CUI 모드 : ctrl + alt + F1 (입력 후 팀뷰어 재접속)

ctrl + alt + (F1 ~ F7) 까지 존재함 (또는 alt + left/right 화살표도 된다는데, 난 안됨)
http://askubuntu.com/questions/157617/reverting-from-ctrl-alt-f1

# GUI 모드 : ctrl + alt + F7 (원래는 되야하는데, 팀뷰어상에서 아무리 해도 안됨. 그냥 다음의 명령어를 putty에서 입력하여 실행해서 재부팅해야함)

sudo service lightdm start
sudo init 5
sudo reboot 

http://rootblog.tistory.com/1

init 0 : 종료모드
init 1 : 단일 사용자 모드 ( 시스템 복구용 )
init 2 : 다중 사용자 텍스트 모드 ( 서버 공유 불가 )
init 3 : 다중 사용자 텍스트 모드 ( 일반적인 CLI 모드 )
init 4 : 사용안함
init 5 : 다중 사용자 그래픽 코드 ( 일반적인 GUI 모드 )
init 6 : 시스템 재부팅 모드

- Trouble Shooting

# 팀뷰어 화면에 검은색과 _ 커서 표시만 뜰 경우 :
lightdm 을 stop이 된 상태라 GUI표시가 안되는 것임. 따라서 sudo service lightdm start 을 할것

# 팀뷰어상에서 ctrl + alt + F1를 입력하여, CUI모드로는 로그인이 되는데 GUI모드로는 로그인이 안 될 때:
이것은 Teamviewer 때문에 일어나는 현상이 아니고, ubuntu 자체에서 lightdm 등의 문제로 무한 login loop가 발생하는 것이다.

sudo apt-get remove lightdm
sudo apt-get install ligthdm
sudo service lightdm restart
sudo reboot

https://forum.ubuntu-kr.org/viewtopic.php?t=22015

# sudo teamviewer setup --> The framebuffer console seems to be unavailable or has the wrong format. Please make sure that /dev/fb0 is accessible and it is configured to 32-bit depth.

이라는 에러 메시지가 발생할 때

sudo chmod 777 /dev/fb0
ln /dev/fb0 /dev/fb

위의 명령어를 해도 안되면, 다음을 참조. 단, 절대로 sudo aptitude install v86d을 실행하면 안됨!! 믿기지 않지만 이거하면 network-manager를 없애버림...; apt-get install v86d 으로 대체할 것

https://blog.samat.org/2010/11/09/High-resolution-text-console-with-uvesafb-and-Debian/
https://gist.github.com/bdemirkir/eb1572dd2da44cfa7c27ea1bf7073c5b

network-manager가 다 지워지면서 고정 IP세팅과 DNS정보가 날아가버림. 그래서 그걸 다시 복구했더니 아이에 우분투 GUI가 작동을 안해서, 다음의 과정을 통해 전부 재설치 했더니 일단 이 문제는 해결되었음.


# GUI고치기

sudo apt-get install --reinstall ubuntu-desktop

(혹은 sudo dpkg-reconfigure ubuntu-desktop)

쿠다 재설치

sudo apt-get install --reinstall xorg


http://www.beginninglinux.com/home/troubleshoot/ubuntu-does-not-start-gui-or-login-loop


http://askubuntu.com/questions/143179/startx-doesnt-work


http://askubuntu.com/questions/320691/ubuntu-boots-on-terminal-only-how-do-i-recover-the-gui-desktop

http://askubuntu.com/questions/168736/how-to-start-gui-from-command-line



# 무한 로그인창으로 돌아오는 login loop

cat ~/.xsession-errors --> 원인확인

startx

chmod 777 .Xauthority

mv .Xauthority .Xauthority.bak

sudo service lightdm restart

sudo apt-get purge lightdm && sudo apt-get install gdm

(혹은 sudo apt-get install --reinstall lightdm)

sudo dpkg-reconfigure lightdm

재부팅


http://www.linuxslaves.com/2016/05/3-ways-fix-ubuntu-gets-stuck-login-loop.html

https://www.computersnyou.com/4947/how-to-solve-ubuntu-login-loop-quick-tip/


# An X server is currently running on the active VT (virtual console). Please shut down your X session or display manager.

To Stop X.Org Server:
sudo service lightdm stop

To Start X.Org Server:
startx
##(OR)
sudo service lightdm start

To Restart X.Org Server:
sudo service lightdm restart

http://askubuntu.com/questions/66058/how-to-shut-down-x


# There was a connectivity issue. Please check your internet connection and try again.

sudo teamviewer daemon stop
sudo teamviewer daemon start  

http://askubuntu.com/questions/676224/teamviewer-not-ready-check-your-connection

# ubuntu 로고화면만 계속 뜨는 상황: GPU드라이버를 재설치 해볼것.

# 팀뷰어 GUI로 login 시도 시, 비밀번호가 맞아도 Guest로만 로그인이 가능한 상황 :
sudo chmod 777 .Xauthority

# 팀뷰어로 접속했는데 검은화면만 나올때
sudo service lightdm start

# TeamViewer로 접속시 해상도가 낮을 때 : xconfig에서 다음부분을 수정한다.

1) 제일 쉬운 방법.
해상도 낮은 TeamViewer로 접속한 상태에서 다음 명령어를 입력한다.(원하는 해상도에 따라)

xrandr --fb 1280x1024
xrandr --fb 1920x1080

그리고나서 팀뷰어 재접속.

2) 조금 복잡한 방법. 근데 원래 됬었는데 어쩔땐 또 안됨.. 1번을 추천

sudo su
vim /etc/X11/xorg.conf 을 치고 다음 Bold 줄을 추가함

SubSection "Display"
        Depth          24
        Modes         "1280x1024"  "1024x768"   "640x480"
EndSubSection

reboot

https://wiki.ubuntu.com/X/Config/Resolution


- TeamViewer 데몬 실행, 중지, 재시작:
sudo teamviewer daemon start
sudo teamviewer daemon stop
sudo teamviewer daemon restart 




X server 관련 문제 해결

- X server 문제 해결 : 대부분 cat /var/log/Xorg.0.log  을 입력해서 로그를 꼼꼼히 잘 읽어본다음 구글에 검색하면 해결 가능


> X config 파일 초기화 하기 :
sudo su
rm /etc/X11/xorg.conf
Xorg -configure # This should create a file called /root/xorg.conf.new
vim /root/xorg.conf.new # edit as your mind
cp /root/xorg.conf.new /etc/X11/xorg.conf
reboot
startx


> 문제 : Failed to assign any connected display devices to X screen 0
X config 파일을 nvidia-xconfig 으로 생성하면 해결됨.

sudo nvidia-xconfig --allow-empty-initial-configuration # 사용가능한 display가 없어도 X를 실행하는 옵션
sudo reboot

http://askubuntu.com/questions/457850/x-server-doesnt-work-on-ubuntu-14-04-what-may-be-happening