Environment Setup
First, you need a PC or virtual machine with Ubuntu installed; version 20.04 is recommended. The username cannot contain Chinese characters.
We have prepared a docker environment. You only need to follow the steps below to create a complete compilation environment for compiling OpenHarmony 5.0.
The docker image creation method is as follows:
1 Install Docker
- Reference: section 2.2 of Learn Docker installation and usage in 10 minutes - CSDN Blog
2 Use Our Pre-Built Docker Image
This document introduces two methods.Method 1: use our pre-built docker image. Method 2: build your own docker image.
Advantages: hassle-free, no network required. Disadvantages: there may be file-permission issues.
Note
docker image path:ShimetaPi OpenHarmony Materials>M4-R1>05-Development Materials>01-OpenHarmory Development Materials>openharmony5_env.tar
Download the file and copy it to any directory, then run the following command to load the docker image.
docker load -i openharmony5_env.tar
docker images # 查看镜像是否加载成功3 Build Your Own Docker Image
3.1 Create the Dockerfile
3.1.1 Purpose
A dockerfile is a text file that contains a series of instructions used to build a Docker image.
3.1.2 Creation Method
(1) cd ~ (2) mkdir proj (3) cd ./proj (4) vim dockerfile (5) Paste the following text in, then type ":q" to save.
// dockerfile
FROM ubuntu:18.04
ARG TARGETPLATFORM
ARG DEBIAN_FRONTEND=noninteractive
ARG userid
ARG groupid
ARG username
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
RUN cp -a /etc/apt/sources.list /etc/apt/sources.list.bak
RUN sed -i 's@http://.*ubuntu.com@http://repo.huaweicloud.com@g' /etc/apt/sources.list
RUN dpkg --add-architecture i386 && \
apt-get update && \
apt-get install -y locales && \
localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
ENV LANG en_US.UTF-8
RUN apt-get install --no-install-recommends --no-install-suggests --yes \
binutils git git-lfs gnupg flex bison gperf build-essential zip \
curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev \
x11proto-core-dev libx11-dev lib32z1-dev ccache libgl1-mesa-dev libxml2-utils \
xsltproc unzip m4 bc gnutls-bin python3.8 python3-pip ruby openjdk-8-jdk \
python3-distutils dosfstools mtools libssl-dev libelf-dev sudo vim openssh-client wget libfl-dev liblz4-tool scons mtd-utils u-boot-tools default-jdk cpio genext2fs gcc-arm-none-eabi && \
apt-get clean && \
rm -rf /var/lib/apt/* /var/cache/apt/* /tmp/* /var/tmp/*
RUN pip3 install setuptools kconfiglib -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip3 install scons ecdsa pycryptodome -i https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip3 install --upgrade --ignore-installed six
RUN rm -f /usr/bin/python
RUN ln -s /usr/bin/python3.8 python
RUN groupadd -g $groupid $username \
&& useradd -m -u $userid -g $groupid $username \
&& echo "$username:123456" | chpasswd \
&& echo $username >/root/username
RUN sed -i -e '/\%sudo/ c \%sudo ALL=(ALL) NOPASSWD: ALL' /etc/sudoers
RUN usermod -a -G sudo $username
RUN echo "root:123456" | chpasswd
ENV HOME=/home/$username
ENV USER=$username
WORKDIR $HOME
ENV HOME=/home/$username
ENV USER=$username
ENV WORKFOLDER=/home/$username/proj
ENV GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
ENV PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home'
RUN mkdir -p $WORKFOLDER
ENTRYPOINT chroot --userspec=$(cat /root/username):$(cat /root/username) / /bin/bash -c "cd $WORKFOLDER && exec /bin/bash -i"Dockerfile Detailed Description
For those who want to learn Docker, read the following text:
In line 14, the apt command source is changed to Huawei Cloud to alleviate the slow download from the original image source. In line 23, this mainly installs some dependency packages needed for compilation. You can modify this later to add the packages you want. In line 39, a group is created using the passed-in groupid; it is recommended not to modify it. In line 40, a user is created using the passed-in userid; it is recommended not to modify it. In line 41, the user password is modified to 123456. You can modify the user's default password here to your own commonly-used password. Through the above configuration, when we enter the container, the container's default user, its groupid, userid, and user password are determined by the above code. In line 48, the root password is explicitly set to 123456. You can modify the root password here to your own commonly-used password.
3.2 Create build-container
3.2.1 Purpose
This script is used to: create the required image from the dockerfile in 2.1.
3.2.2 Creation Method
(1) cd ~/proj (2) vim build-container (3) Paste the following text in, then type ":q" to save.
#!/bin/bash
USER_ID=1000
GROUP_ID=1000
USERNAME=shimeta
docker build --build-arg userid=$USER_ID --build-arg groupid=$GROUP_ID --build-arg username=$USERNAME --tag sc3568ha2:latest .Tips
Lines 3, 4, and 5 pass the custom user's group ID, user ID, and user name to the dockerfile. In the dockerfile created in 2.1, a new user will be created based on these three passed-in parameters. The " . " at the end of line 8 is used to specify the location of the dockerfile we created in the previous step. Since we place the dockerfile, build-container, and entrycontainer files all in the same path, we use " . " here to indicate that the dockerfile is in the current directory. The "sc3568ha2:latest" in line 8 specifies the name and version number of the created docker image. You can modify it yourself; the format is "image_name:version".
(4) chmod +x ./build-container
3.3 Create the Image
# 先将当前用户加入docker组
sudo usermod -aG docker $USER
# 应用变更
newgrp docker
# 运行以下命令测试是否无需 sudo 即可使用 Docker:
docker images
bash ./build-container3.3.1 Problem 1
Image creation fails with the following related text — consider that a network problem caused the docker image pull to fail. Error response from daemon: Get "https://registry-1.docker.io/v2/": context deadline exceeded (Client.Timeout exceeded while awaiting headers)
This can be resolved by configuring the image source. Reference solution:
# 编辑daemon.json
vim /etc/docker/daemon.json
# 将下述第5至36行的文本粘贴到daemon.json
{
"registry-mirrors" : ["https://docker.registry.cyou",
"https://docker-cf.registry.cyou",
"https://dockercf.jsdelivr.fyi",
"https://docker.jsdelivr.fyi",
"https://dockertest.jsdelivr.fyi",
"https://mirror.aliyuncs.com",
"https://dockerproxy.com",
"https://mirror.baidubce.com",
"https://docker.m.daocloud.io",
"https://docker.nju.edu.cn",
"https://docker.mirrors.sjtug.sjtu.edu.cn",
"https://docker.mirrors.ustc.edu.cn",
"https://mirror.iscas.ac.cn",
"https://docker.rainbond.cc",
"https://do.nark.eu.org",
"https://dc.j8.work",
"https://dockerproxy.com",
"https://gst6rzl9.mirror.aliyuncs.com",
"https://registry.docker-cn.com",
"http://hub-mirror.c.163.com",
"http://mirrors.ustc.edu.cn/",
"https://mirrors.tuna.tsinghua.edu.cn/",
"http://mirrors.sohu.com/"
],
"insecure-registries" : [
"registry.docker-cn.com",
"docker.mirrors.ustc.edu.cn"
],
"debug": true,
"experimental": false
}
# 重新加载daemon.json文件,使修改生效
sudo systemctl daemon-reload
# 重新启动docker服务
sudo systemctl restart docker
# 查看是否配置成功
docker info4 Enter the Container
4.1 entrycontainer
4.1.1 Purpose
This script is used to: create a container from the image in 1 or the image generated in 2, and enter that container.
4.1.2 Creation Method
(1) cd ~/proj (2) vim entrycontainer (3) Paste the following text in, then type ":q" to save.
#!/bin/bash
set -e
HOME_DIR="/home/shimeta/proj"
BUILD_DIR="/home/xxx/proj"
docker run --privileged --volume ${BUILD_DIR}:${HOME_DIR}:rw --volume /tmp:/tmp \
--workdir=${HOME_DIR} \
--env TERM=xterm-256color --env SHELL=/bin/bash \
--rm --init --tty --interactive \
--hostname docker_OP \
sc3568ha2:latestTips
One convenience of using docker is that you can map a host folder into the container. After entering the container, it is as if we are using a new computer while still being able to access specific host folders. With this feature, later, before entering the container, we first place the project files under the path specified by BUILD_DIR; then, after entering the container, we go to the HOME_DIR path and we can see our project files at HOME_DIR inside the container. The "--volume ${BUILD_DIR}:${HOME_DIR}:rw" in line 9 is the statement that implements this mapping. It maps the host folder BUILD_DIR to the container's HOME_DIR path. After we enter the container, we can access all the files under the host folder BUILD_DIR from the HOME_DIR path. The path in line 6 is usually also "/home/xxx/proj", consistent with the in-container path, to avoid confusion. Here, xxx is the name of the user you use, and needs to be modified manually.
4.2 Execute the Following Commands
cd ~/proj
chmod +x ./entrycontainer
bash ./entrycontainer5 Operations After Entering the Container
After entering the container, you also need to execute the following commands:
# 1、配置默认命令行解释器为bash
ls -l /bin/sh #如果显示为“/bin/sh -> bash”则为正常,否则请以以下方式修改:
sudo dpkg-reconfigure dash #然后选择no
# 2、配置git邮箱,防止编译代码时报错
git config --global user.name "你的用户名"
git config --global user.email "你的邮箱@example.com"