Skip to content

Instantly share code, notes, and snippets.

@chertov
Last active April 28, 2020 09:45
Show Gist options
  • Save chertov/8d95f65af635540c9e235ce861bf5460 to your computer and use it in GitHub Desktop.
Save chertov/8d95f65af635540c9e235ce861bf5460 to your computer and use it in GitHub Desktop.
hikernels
#/bin/sh
set -e
# https://mirrors.edge.kernel.org/pub/linux/kernel/v4.x/linux-4.14.tar.xz
# https://mirrors.edge.kernel.org/pub/linux/kernel/v3.x/linux-3.0.8.tar.xz
# https://mirrors.edge.kernel.org/pub/linux/kernel/v3.x/linux-3.18.20.tar.xz
# https://mirrors.edge.kernel.org/pub/linux/kernel/v3.x/linux-3.4.35.tar.xz
KERNELS=./init_data/
if [ ! -d "$KERNELS" ]; then mkdir $KERNELS; fi
init_kernel () {
local kernel_version=$1
local kernel_group=v${kernel_version:0:1}.x
if [ -d ./linux-$kernel_version ]; then return; fi
# download original linux kernel
if [ ! -f ./$KERNELS/linux-$kernel_version.tar.xz ]; then
curl -o ./$KERNELS/linux-$kernel_version.tar.xz https://mirrors.edge.kernel.org/pub/linux/kernel/$kernel_group/linux-$kernel_version.tar.xz
fi
# untar, init git repo and commit original source code
( tar -xpJf ./$KERNELS/linux-$kernel_version.tar.xz && cd linux-$kernel_version && git init && git add . && git commit -m 'init sources' )
}
apply_patches () {
local kernel_version=$1
if [ -d ./linux-$kernel_version ]; then return; fi
init_kernel $kernel_version
# apply all patches
for i in ./patches-$kernel_version/*.patch; do patch -s -p0 < $i; done
# commit all untracked files
# ( cd linux-$kernel_version && git add $(git ls-files -o --exclude-standard) && git commit -m 'commit only untracked files' )
# commit changes
( cd linux-$kernel_version && git add . && git commit -m 'hisilicon changes' )
}
#init the latest lts kernel
init_kernel 4.14
# init and apply patches
apply_patches 3.0.8
apply_patches 3.18.20
apply_patches 3.4.35
#/bin/sh
set -e
# Clean the exited containers, the untagged images (or old images)
( docker rm $( docker ps -a | grep Exit | cut -d ' ' -f 1) || exit 0 )
( docker rmi $(docker images | tail -n +2 | awk '$1 == "<none>" {print $'3'}') || exit 0 )
current_path=$(realpath $(dirname "$0"))
source $current_path/include.sh
make_patch 3.4.35 linux-3.4.5 linux-3.4.y
build_kernel 3.4.35
#/bin/sh
set -e
# Clean the exited containers, the untagged images (or old images)
( docker rm $( docker ps -a | grep Exit | cut -d ' ' -f 1) || exit 0 )
( docker rmi $(docker images | tail -n +2 | awk '$1 == "<none>" {print $'3'}') || exit 0 )
current_path=$(realpath $(dirname "$0"))
source $current_path/include.sh
make_patch 4.14 linux-4.14 linux-4.14
# build_kernel () {
# local kernel_version=$1
# local build_imagename=kernel_$kernel_version
# docker build -t $build_imagename -f $(pwd)/kernel_$kernel_version.Dockerfile $(pwd)/data_$kernel_version/
# docker run -it -v $(pwd)/output/:/output/ $build_imagename
# }
# build_kernel 4.14
#/bin/sh
set -e
current_path=$(realpath $(dirname "$0"))
KERNELS=init_data
download_linux_kernel () {
local kernel_version=$1
# download original linux kernel
# https://mirrors.edge.kernel.org/pub/linux/kernel/v4.x/linux-4.14.tar.xz
# https://mirrors.edge.kernel.org/pub/linux/kernel/v3.x/linux-3.0.8.tar.xz
# https://mirrors.edge.kernel.org/pub/linux/kernel/v3.x/linux-3.18.20.tar.xz
# https://mirrors.edge.kernel.org/pub/linux/kernel/v3.x/linux-3.4.35.tar.xz
local tar_path=$current_path/$KERNELS/linux-$kernel_version.tar.xz
if [ ! -f $tar_path ]; then
local kernel_group=v${kernel_version:0:1}.x
local kernel_url=https://mirrors.edge.kernel.org/pub/linux/kernel/$kernel_group/linux-$kernel_version.tar.xz
printf " Can't find linux kernel source archive: $tar_path\n"
printf " Try to download: $kernel_url\n"
curl -o $tar_path $kernel_url
fi
}
unpack_linux_kernel () {
local kernel_version=$1
if [ -d $current_path/linux-$kernel_version ]; then
printf "Skip 1-3 steps: Linux kernel founded in $current_path/linux-$kernel_version\n"
return;
fi
printf "Step 1: get linux kernel $kernel_version source archive\n"
local tar_path=$current_path/$KERNELS/linux-$kernel_version.tar.xz
if [ ! -f $tar_path ]; then
download_linux_kernel $kernel_version
fi
printf " Archive path: $tar_path\n"
printf " Ok!\n"
printf "Step 2: unpack linux kernel $kernel_version source archive\n"
# untar
tar -xpJf $tar_path
printf " Ok!\n"
printf "Step 3: init git repo in linux-$kernel_version, make first commit with original source code\n"
# init git repo and commit original source code
( cd $current_path/linux-$kernel_version && git init && git add . && git commit -m 'init sources' && git tag original_src )
printf " Ok!\n"
}
prepare_linux_kernel () {
local kernel_version=$1
if [ -d $current_path/linux-$kernel_version ]; then return; fi
unpack_linux_kernel $kernel_version
printf "Step 4: apply ztf patches\n"
# apply all patches
for i in $current_path/$KERNELS/zft_patches/patches-$kernel_version/*.patch; do patch -s -p0 < $i; done
printf " Ok!\n"
# commit all untracked files
# ( cd linux-$kernel_version && git add $(git ls-files -o --exclude-standard) && git commit -m 'commit only untracked files' )
printf "Step 5: commit hisilicon changes\n"
# commit changes
( cd linux-$kernel_version && git add . && git commit -m 'hisilicon changes' && git tag hisilicon )
printf " Ok!\n"
}
# Example: make_patch 3.4.35 linux-3.4.5 linux-3.4.y
make_patch () {
local kernel_version=$1
local src_prefix=$2
local dst_prefix=$3
local hikernels_path=$(realpath $(dirname "$0"))
if [ ! -d "$hikernels_path/linux-$kernel_version" ]; then prepare_linux_kernel $kernel_version; fi
local patches_path=$hikernels_path/data_$kernel_version/patches
if [ ! -d "$patches_path" ]; then mkdir -p $patches_path; fi
rm -rf $patches_path/*
echo patches_path $patches_path
(
cd $hikernels_path/linux-$kernel_version
# git diff original_src hisilicon --src-prefix=$src_prefix/ --dst-prefix=$dst_prefix/ > $patches_path/003-hisilicon.patch
git diff hisilicon HEAD --src-prefix=$src_prefix/ --dst-prefix=$dst_prefix/ > $patches_path/707-commited.patch
git diff --cached --src-prefix=$src_prefix/ --dst-prefix=$dst_prefix/ > $patches_path/708-cached.patch
git diff --src-prefix=$src_prefix/ --dst-prefix=$dst_prefix/ > $patches_path/709-index.patch
)
}
# Example: build_kernel 3.4.35
build_kernel () {
local kernel_version=$1
local build_imagename=kernel_$kernel_version
docker build -t $build_imagename -f $current_path/kernel_$kernel_version.Dockerfile $current_path/data_$kernel_version/
docker run -it -v $current_path/output/:/output/ $build_imagename
}
FROM kernels_src
ARG MAKE_ARGS=-j7
WORKDIR /src/linux-3.4.y/
RUN cp arch/arm/configs/hi3518ev200_full_defconfig .config \
&& sed -i "s/CONFIG_INITRAMFS_SOURCE=\"\"/CONFIG_INITRAMFS_SOURCE=\"\/src\/rootfs\.cpio\.gz\"/g" .config \
&& printf "\nCONFIG_INITRAMFS_ROOT_UID=0\n\
CONFIG_INITRAMFS_ROOT_GID=0\n" >> .config
RUN make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- ${MAKE_ARGS} oldconfig \
&& make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- ${MAKE_ARGS} uImage modules dtbs
COPY ./patches /src/patches
# ARG MAKE_ARGS="-j1 V=s"
RUN ( cd /src/ && for i in ./patches/*.patch; do patch -s -p0 < $i; done ) \
&& make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- ${MAKE_ARGS} uImage modules dtbs
FROM kernels_src
ARG MAKE_ARGS=-j7
WORKDIR /src/linux-4.14/
COPY ./patches /src/patches
RUN ( cd /src/ && for i in ./patches/*.patch; do patch -s -p0 < $i; done )
RUN cp arch/arm/configs/hi3518ev200_full_defconfig .config
ARG MAKE_ARGS="-j1 V=s"
RUN make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- ${MAKE_ARGS} oldconfig \
&& make ARCH=arm CROSS_COMPILE=/usr/bin/arm-linux-gnueabi- ${MAKE_ARGS} uImage modules dtbs
FROM debian:stretch
RUN DEBIAN_FRONTEND=noninteractive apt-get update && apt-get -y install \
build-essential gawk git ncurses-dev python gcc-arm-linux-gnueabi \
subversion unzip zlib1g-dev libssl-dev wget cpio bc time u-boot-tools
WORKDIR /src/
COPY ./linux-*.tar.xz ./
# RUN wget https://mirrors.edge.kernel.org/pub/linux/kernel/v3.x/linux-3.0.8.tar.xz \
# && wget https://mirrors.edge.kernel.org/pub/linux/kernel/v3.x/linux-3.18.20.tar.xz \
# && wget https://mirrors.edge.kernel.org/pub/linux/kernel/v3.x/linux-3.4.35.tar.xz
RUN mkdir -p linux-3.0.y \
&& tar -xf linux-3.0.8.tar.xz -C ./linux-3.0.y --strip-components=1 \
\
&& mkdir -p linux-3.18.y \
&& tar -xf linux-3.18.20.tar.xz -C ./linux-3.18.y --strip-components=1 \
\
&& mkdir -p linux-3.4.y \
&& tar -xf linux-3.4.35.tar.xz -C ./linux-3.4.y --strip-components=1 \
\
&& mkdir -p linux-4.14 \
&& tar -xf linux-4.14.tar.xz -C ./linux-4.14 --strip-components=1 \
\
&& rm linux-*.tar.xz
COPY ./zft_patches ./zft_patches
# RUN for i in ./zft_patches/patches-3.0.8/*.patch; do patch -s -p0 < $i; done
RUN for i in ./zft_patches/patches-3.18.20/*.patch; do patch -s -p0 < $i; done
RUN for i in ./zft_patches/patches-3.4.35/*.patch; do patch -s -p0 < $i; done
COPY ./rootfs.cpio.gz ./
RUN echo "#!/bin/bash\n \
set -e\n \
rm -rf /output/*\n \
cp .config /output\n \
cp arch/arm/boot/uImage /output\n \
" >> copy.sh && chmod 777 copy.sh
ENTRYPOINT ["/src/copy.sh"]
CMD []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment