Browse Source

关于debootstrap

侠义书生 3 years ago
parent
commit
8155dc498d

+ 6 - 0
README.md

@@ -32,3 +32,9 @@
 
 - 说明:解析debian/control安装依赖包
 
+# 同步麒麟源到本地
+
+```
+apt install apt-mirror
+apt-mirror etc/apt/mirrors/mirror2-launchpad.kylin.list
+```

+ 4 - 4
config-ip.sh

@@ -2,10 +2,10 @@
 set -e
 
 function usage() {
-  echo -e "USAGE:\n\
-    $(basename $0) --help   # 使用帮助\n\
-    $(basename $0) work     # 联想工作机IP\n\
-    $(basename $0) pxe      # pxe IP\n\
+  echo -e "USAGE:
+    $(basename $0) --help   # 使用帮助
+    $(basename $0) work     # 联想工作机IP
+    $(basename $0) pxe      # pxe IP
     $(basename $0) dhcp     # 自动分配"
 
   exit 1

+ 5 - 5
config-nmcli.sh

@@ -3,11 +3,11 @@
 set -e
 
 function usage() {
-  echo -e "USAGE:\n\
-    $(basename $0) --help   # 使用帮助\n\
-    $(basename $0) wifi     # 连接wifi\n\
-    $(basename $0) pxe      # pxe IP\n\
-    $(basename $0) work1    # 工作 IP 1\n\
+  echo -e "USAGE:
+    $(basename $0) --help   # 使用帮助
+    $(basename $0) wifi     # 连接wifi
+    $(basename $0) pxe      # pxe IP
+    $(basename $0) work1    # 工作 IP 1
     $(basename $0) work2    # 工作 IP 2
     "
 

+ 38 - 2
debootstrap/cmd_debootstrap.sh

@@ -3,7 +3,7 @@ function docs() {
     # 源码: https://salsa.debian.org/installer-team/debootstrap
 
     # 查看支持的版本代号
-    ls /usr/share/debootstrap/scripts/
+    ls -l /usr/share/debootstrap/scripts/
 }
 
 #################### 本地源 ####################
@@ -76,8 +76,10 @@ function kylin_launchpad() {
     # --cache-dir
     sudo debootstrap --no-check-gpg --cache-dir=/data/temp/kylin-debs --components=main,restricted,universe,multiverse v101 /data/temp/kylin-rootfs ${MIRROR_URL}
 
-    # --foreign
+    # --foreign(第一阶段)
     sudo debootstrap --no-check-gpg --foreign --components=main,restricted,universe,multiverse v101 /data/temp/kylin-rootfs ${MIRROR_URL}
+    # --second-stage(第二阶段)
+    sudo chroot /data/temp/kylin-rootfs /debootstrap/debootstrap --second-stage
     # --variant=minbase
     sudo debootstrap --no-check-gpg --variant=minbase --foreign --components=main,restricted,universe,multiverse v101 /data/temp/kylin-rootfs ${MIRROR_URL}
     # --cache-dir
@@ -130,3 +132,37 @@ function rpi() {
     # armhf
     # E: Couldn't download http://archive.launchpad.dev/kylin/dists/v101/restricted/binary-armhf/Packages
 }
+
+# 两种解 deb 的方式
+function extract_deb() {
+    # 方法1
+    for i in $(ls /data/temp/kylin-rootfs/var/cache/apt/archives/*.deb); do
+        echo $i
+        dpkg-deb --fsys-tarfile $i | tar -k -xf -
+    done
+
+    # 方法2
+    for i in $(ls /data/temp/kylin-rootfs/var/cache/apt/archives/*.deb); do
+        echo $i
+        dpkg-deb -x $i .
+    done
+
+    # 解压deb到 my_rootfs
+    for i in $(ls *.deb); do
+        echo $i
+        dpkg-deb -x "${i}" /data/temp/my_rootfs
+    done
+}
+
+# 将 rootfs 目录中的内容打包成tar包,並导入到Docker镜像列表中
+function docker() {
+    # 方法1
+    sudo tar cvf kylin-rootfs.tar -C kylin-rootfs .
+    docker import kylin-rootfs.tar kylin
+
+    # 方法2
+    sudo tar c -C kylin-rootfs . | docker import - kylin
+
+    # 运行
+    docker run -it --rm kylin /bin/bash
+}

+ 158 - 156
debootstrap/my_debootstrap.sh

@@ -7,84 +7,86 @@ smallyes() {
     while echo "$YES" 2>/dev/null; do :; done
 }
 
+########## functions for first stage ##########
+conditional_cp() {
+    if [ ! -e "$2/$1" ]; then
+        if [ -L "$1" ] && [ -e "$1" ]; then
+            cat "$1" >"$2/$1"
+        elif [ -e "$1" ]; then
+            cp "$1" "$2/$1"
+        fi
+    fi
+}
+
+setup_merged_usr() {
+    link_dir="bin sbin lib lib32 lib64 libx32"
+    local dir
+    for dir in $link_dir; do
+        mkdir -p "$TARGET/usr/$dir"
+        ln -s usr/"$dir" "$TARGET/$dir"
+    done
+}
+
+extract_dpkg_deb_data() {
+    DEB_LIST1=/data/temp/01-kylin-rootfs-first-and-second/1.txt
+    # DEB_DIR=/data/temp/kylin-rootfs
+    if [ -f "${DEB_LIST1}" ]; then
+        for i in $(cat ${DEB_LIST1}); do
+            # i="${DEB_DIR}${i}"
+            i="$(dirname ${DEB_LIST1})${i}"
+            echo $i
+            dpkg-deb --fsys-tarfile $i | tar -k -xf -
+        done
+    else
+        echo "${DEB_LIST1} is not exist!!"
+    fi
+}
+
+# Create a device node if it does not exist. By default, the mode is 666.
+mknod_if_needed() {
+    local device type major minor mode
+    device="$1"
+    type="$2"
+    major="$3"
+    minor="$4"
+    mode="${5:-666}"
+
+    if [ ! -e "$device" ]; then
+        mknod -m "$mode" "$device" "$type" "$major" "$minor"
+    fi
+}
+
+setup_devices_simple() {
+    # The list of devices that can be created in a container comes from
+    # src/core/cgroup.c in the systemd source tree.
+    mknod_if_needed "$TARGET/dev/null" c 1 3
+    mknod_if_needed "$TARGET/dev/zero" c 1 5
+    mknod_if_needed "$TARGET/dev/full" c 1 7
+    mknod_if_needed "$TARGET/dev/random" c 1 8
+    mknod_if_needed "$TARGET/dev/urandom" c 1 9
+    mknod_if_needed "$TARGET/dev/tty" c 5 0
+    if [ ! "$CONTAINER" = "systemd-nspawn" ]; then
+        mknod_if_needed "$TARGET/dev/console" c 5 1
+    fi
+    # To avoid pre-exist directory causes error, specify "-p" option
+    mkdir -p "$TARGET/dev/pts/" "$TARGET/dev/shm/"
+    # Inside a container, we might not be allowed to create /dev/ptmx.
+    # If not, do the next best thing.
+    if ! mknod_if_needed "$TARGET/dev/ptmx" c 5 2; then
+        warning MKNOD "Could not create /dev/ptmx, falling back to symlink. This chroot will require /dev/pts mounted with ptmxmode=666"
+        ln -sf pts/ptmx "$TARGET/dev/ptmx"
+    fi
+    ln -sf /proc/self/fd "$TARGET/dev/fd"
+    ln -sf /proc/self/fd/0 "$TARGET/dev/stdin"
+    ln -sf /proc/self/fd/1 "$TARGET/dev/stdout"
+    ln -sf /proc/self/fd/2 "$TARGET/dev/stderr"
+}
+########## functions for first stage ##########
+
 first_stage_setup() {
     TARGET=$(pwd)
     echo "TARGET=${TARGET}"
 
-    setup_merged_usr() {
-        link_dir="bin sbin lib lib32 lib64 libx32"
-        local dir
-        for dir in $link_dir; do
-            mkdir -p "$TARGET/usr/$dir"
-            ln -s usr/"$dir" "$TARGET/$dir"
-        done
-    }
-
-    conditional_cp() {
-        if [ ! -e "$2/$1" ]; then
-            if [ -L "$1" ] && [ -e "$1" ]; then
-                cat "$1" >"$2/$1"
-            elif [ -e "$1" ]; then
-                cp "$1" "$2/$1"
-            fi
-        fi
-    }
-
-    extract_dpkg_deb_data() {
-        DEB_LIST1=/data/temp/01-kylin-rootfs-first-and-second/1.txt
-        # DEB_DIR=/data/temp/kylin-rootfs
-        if [ -f "${DEB_LIST1}" ]; then
-            for i in $(cat ${DEB_LIST1}); do
-                # i="${DEB_DIR}${i}"
-                i="$(dirname ${DEB_LIST1})${i}"
-                echo $i
-                dpkg-deb --fsys-tarfile $i | tar -k -xf -
-            done
-        else
-            echo "${DEB_LIST1} is not exist!!"
-        fi
-    }
-
-    # Create a device node if it does not exist. By default, the mode is 666.
-    mknod_if_needed() {
-        local device type major minor mode
-        device="$1"
-        type="$2"
-        major="$3"
-        minor="$4"
-        mode="${5:-666}"
-
-        if [ ! -e "$device" ]; then
-            mknod -m "$mode" "$device" "$type" "$major" "$minor"
-        fi
-    }
-
-    setup_devices_simple() {
-        # The list of devices that can be created in a container comes from
-        # src/core/cgroup.c in the systemd source tree.
-        mknod_if_needed "$TARGET/dev/null" c 1 3
-        mknod_if_needed "$TARGET/dev/zero" c 1 5
-        mknod_if_needed "$TARGET/dev/full" c 1 7
-        mknod_if_needed "$TARGET/dev/random" c 1 8
-        mknod_if_needed "$TARGET/dev/urandom" c 1 9
-        mknod_if_needed "$TARGET/dev/tty" c 5 0
-        if [ ! "$CONTAINER" = "systemd-nspawn" ]; then
-            mknod_if_needed "$TARGET/dev/console" c 5 1
-        fi
-        # To avoid pre-exist directory causes error, specify "-p" option
-        mkdir -p "$TARGET/dev/pts/" "$TARGET/dev/shm/"
-        # Inside a container, we might not be allowed to create /dev/ptmx.
-        # If not, do the next best thing.
-        if ! mknod_if_needed "$TARGET/dev/ptmx" c 5 2; then
-            warning MKNOD "Could not create /dev/ptmx, falling back to symlink. This chroot will require /dev/pts mounted with ptmxmode=666"
-            ln -sf pts/ptmx "$TARGET/dev/ptmx"
-        fi
-        ln -sf /proc/self/fd "$TARGET/dev/fd"
-        ln -sf /proc/self/fd/0 "$TARGET/dev/stdin"
-        ln -sf /proc/self/fd/1 "$TARGET/dev/stdout"
-        ln -sf /proc/self/fd/2 "$TARGET/dev/stderr"
-    }
-
     setup_merged_usr
 
     extract_dpkg_deb_data
@@ -108,78 +110,97 @@ first_stage_setup() {
     echo "deb http://172.29.220.242/kylin v101 main restricted universe multiverse" >"$TARGET/etc/apt/sources.list"
 }
 
-second_stage() {
-    TARGET="/"
-
-    in_target_failmsg() {
-        local code msg arg
-        code="$1"
-        msg="$2"
-        arg="$3"
-        shift
-        shift
-        shift
-        if ! PATH=/sbin:/usr/sbin:/bin:/usr/bin eval "$CHROOT_CMD \"\$@\""; then
-            echo "$code" "$msg" "$arg" >>"$TARGET/debootstrap/debootstrap.log"
-            # Try to point user at actual failing package.
-            msg="See %s for details"
-            if [ -e "$TARGET/debootstrap/debootstrap.log" ]; then
-                arg="$TARGET/debootstrap/debootstrap.log"
-                local pkg="$(grep '^dpkg: error processing ' "$TARGET/debootstrap/debootstrap.log" | head -n 1 | sed 's/\(error processing \)\(package \|archive \)/\1/' | cut -d ' ' -f 4)"
-                if [ -n "$pkg" ]; then
-                    msg="$msg (possibly the package $pkg is at fault)"
-                fi
-            else
-                arg="the log"
+########## functions for second stage ##########
+
+in_target_failmsg() {
+    local code msg arg
+    code="$1"
+    msg="$2"
+    arg="$3"
+    shift
+    shift
+    shift
+    if ! PATH=/sbin:/usr/sbin:/bin:/usr/bin eval "$CHROOT_CMD \"\$@\""; then
+        echo "$code" "$msg" "$arg" >>"$TARGET/debootstrap/debootstrap.log"
+        # Try to point user at actual failing package.
+        msg="See %s for details"
+        if [ -e "$TARGET/debootstrap/debootstrap.log" ]; then
+            arg="$TARGET/debootstrap/debootstrap.log"
+            local pkg="$(grep '^dpkg: error processing ' "$TARGET/debootstrap/debootstrap.log" | head -n 1 | sed 's/\(error processing \)\(package \|archive \)/\1/' | cut -d ' ' -f 4)"
+            if [ -n "$pkg" ]; then
+                msg="$msg (possibly the package $pkg is at fault)"
             fi
-            echo "$code" "$msg" "$arg" >>"$TARGET/debootstrap/debootstrap.log"
-            return 1
+        else
+            arg="the log"
         fi
-        return 0
-    }
-
-    in_target() {
-        in_target_failmsg IN_TARGET_FAIL "Failure trying to run: %s" "$CHROOT_CMD $*" "$@"
-    }
-
-    debfor() {
-        (
-            while read pkg path; do
-                for p in "$@"; do
-                    [ "$p" = "$pkg" ] || continue
-                    echo "$path"
-                    mylog "====== $path"
-                done
-            done <"$TARGET/debootstrap/debpaths"
-        )
-    }
-
-    x_feign_install() {
-        local pkg="$1"
-        local deb="$(debfor $pkg)"
-        mylog "deb=${deb}"
-        local ver="$(in_target dpkg-deb -f "$deb" Version)"
-        mylog "ver=${ver}"
-
-        mkdir -p "$TARGET/var/lib/dpkg/info"
-
-        echo "Package: $pkg
+        echo "$code" "$msg" "$arg" >>"$TARGET/debootstrap/debootstrap.log"
+        return 1
+    fi
+    return 0
+}
+
+in_target() {
+    in_target_failmsg IN_TARGET_FAIL "Failure trying to run: %s" "$CHROOT_CMD $*" "$@"
+}
+
+debfor() {
+    (
+        while read pkg path; do
+            for p in "$@"; do
+                [ "$p" = "$pkg" ] || continue
+                echo "$path"
+                mylog "====== $path"
+            done
+        done <"$TARGET/debootstrap/debpaths"
+    )
+}
+
+x_feign_install() {
+    local pkg="$1"
+    local deb="$(debfor $pkg)"
+    mylog "deb=${deb}"
+    local ver="$(in_target dpkg-deb -f "$deb" Version)"
+    mylog "ver=${ver}"
+
+    mkdir -p "$TARGET/var/lib/dpkg/info"
+
+    echo "Package: $pkg
 Version: $ver
 Maintainer: unknown
 Status: install ok installed" >>"$TARGET/var/lib/dpkg/status"
 
-        touch "$TARGET/var/lib/dpkg/info/${pkg}.list"
-    }
+    touch "$TARGET/var/lib/dpkg/info/${pkg}.list"
+}
 
-    x_feign_install dpkg
+x_core_install() {
+    smallyes '' | in_target dpkg --force-depends --install $(debfor "$@")
+}
 
-    # x_core_install() {
-    #     dpkg --force-depends --install $(debfor "$@")
-    # }
+setup_dselect_method() {
+    case "$1" in
+    apt)
+        mkdir -p "$TARGET/var/lib/dpkg"
+        echo "apt apt" >"$TARGET/var/lib/dpkg/cmethopt"
+        chmod 644 "$TARGET/var/lib/dpkg/cmethopt"
+        ;;
+    *)
+        error 1 UNKNOWNDSELECT "unknown dselect method"
+        ;;
+    esac
+}
 
-    x_core_install() {
-        smallyes '' | in_target dpkg --force-depends --install $(debfor "$@")
-    }
+setup_available() {
+    for p in "$@"; do
+        sed -n "/^Package: ${p}$/,/^$/p" /var/lib/apt/lists/*_Packages
+    done >"$TARGET/var/lib/dpkg/available"
+}
+
+########## functions for second stage ##########
+
+second_stage() {
+    TARGET="/"
+
+    x_feign_install dpkg
 
     DEBIAN_FRONTEND=noninteractive
     DEBCONF_NONINTERACTIVE_SEEN=true
@@ -221,28 +242,9 @@ Status: install ok installed" >>"$TARGET/var/lib/dpkg/status"
     echo "Warning: Fake start-stop-daemon called, doing nothing" >"$TARGET/sbin/start-stop-daemon"
     chmod 755 "$TARGET/sbin/start-stop-daemon"
 
-    setup_dselect_method() {
-        case "$1" in
-        apt)
-            mkdir -p "$TARGET/var/lib/dpkg"
-            echo "apt apt" >"$TARGET/var/lib/dpkg/cmethopt"
-            chmod 644 "$TARGET/var/lib/dpkg/cmethopt"
-            ;;
-        *)
-            error 1 UNKNOWNDSELECT "unknown dselect method"
-            ;;
-        esac
-    }
-
     setup_dselect_method apt
 
     dpkg --status-fd 8 --configure --pending --force-configure-any --force-depends 8>&1 1>&7 || echo EXITCODE $?
 
-    setup_available() {
-        for p in "$@"; do
-            sed -n "/^Package: ${p}$/,/^$/p" /var/lib/apt/lists/*_Packages
-        done >"$TARGET/var/lib/dpkg/available"
-    }
-
     setup_available $required $base
 }

+ 64 - 0
debootstrap/test.sh

@@ -0,0 +1,64 @@
+function test() {
+    # I: usage: [OPTION]... <suite> <target> [<mirror> [<script>]]
+    OPTIONS="--no-check-gpg --print-debs --keep-debootstrap-dir --variant=minbase --components=main,restricted,universe,multiverse"
+    OPTIONS="--no-check-gpg --components=main,restricted,universe,multiverse"
+    SUITE="v101"
+    TARGET="/data/temp/kylin-rootfs"
+    MIRROR="http://172.29.220.242/kylin/"
+    sudo debootstrap ${OPTIONS} ${SUITE} ${TARGET} ${MIRROR}
+}
+
+# 分段执行arm
+function test_arm1() {
+    # 安装依赖包
+    sudo apt-get install qemu-user-static
+    sudo apt-get install binfmt-support debootstrap qemu
+
+    sudo mkdir /opt/ubuntu-arm-64
+    sudo debootstrap --arch=arm64 --foreign trusty /opt/ubuntu-arm-64 http://ports.ubuntu.com/
+    sudo cp /usr/bin/qemu-aarch64-static /opt/ubuntu-arm-64/usr/bin/
+
+    # 为第二阶段准备,挂载设备节点
+    function ch_mount() {
+        sudo mount -t proc proc proc/
+        sudo mount -t sysfs sys sys/
+        sudo mount -o bind /dev dev/
+        sudo mount -t devpts pts dev/pts/
+    }
+
+    # --second-stage
+    sudo chroot /opt/ubuntu-arm-64 /debootstrap/debootstrap --second-stage
+
+    # 做完第二阶段,卸载设备节点
+    function ch_umount() {
+        sudo umount proc/
+        sudo umount sys/
+        sudo umount dev/
+        sudo umount dev/pts/
+    }
+}
+
+## 分段执行 实验研究
+function test_kylin() {
+    cd /data/temp/
+
+    # 拷贝第1阶段到kylin-rootfs
+    # sudo cp -rf 03-kylin-rootfs-first_stage_install/ kylin-rootfs
+    sudo cp -rf 03-kylin-rootfs-first_stage_install-minimal kylin-rootfs
+
+    # 拷贝第2阶段测试脚本
+    sudo cp /data/git/gogs/kylin/debootstrap/my_debootstrap.sh /data/temp/kylin-rootfs/debootstrap/
+
+    sudo chroot kylin-rootfs
+    source /debootstrap/my_debootstrap.sh
+
+    # sudo mkdir -p kylin-rootfs/data
+    # sudo mount --bind shell_test/ kylin-rootfs/data/
+    # cd kylin-rootfs
+    # ch_mount
+    # cd ..
+
+    # sudo chroot kylin-rootfs
+    # source /data/my_debootstrap.sh
+    # ch_umount
+}

+ 1 - 1
etc/apt/mirrors/mirror1-kylin.list

@@ -14,6 +14,6 @@ set run_postmirror  0
 
 deb [arch=amd64] http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1 main restricted universe multiverse
 deb [arch=i386]  http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1 main restricted universe multiverse
-# deb [arch=arm64] http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1 main restricted universe multiverse
+deb [arch=arm64] http://archive.kylinos.cn/kylin/KYLIN-ALL 10.1 main restricted universe multiverse
 
 clean http://archive.kylinos.cn/kylin/KYLIN-ALL

+ 1 - 1
etc/apt/mirrors/mirror1-partner.list

@@ -14,6 +14,6 @@ set run_postmirror  0
 
 deb [arch=amd64] http://archive.kylinos.cn/kylin/partner 10.1 main
 deb [arch=i386]  http://archive.kylinos.cn/kylin/partner 10.1 main
-# deb [arch=arm64] http://archive.kylinos.cn/kylin/partner 10.1 main
+deb [arch=arm64] http://archive.kylinos.cn/kylin/partner 10.1 main
 
 clean http://archive.kylinos.cn/kylin/partner

+ 3 - 3
rebuild-deb.sh

@@ -4,9 +4,9 @@ set -e
 # 解压、修改、重新打包deb包
 
 function usage() {
-    echo -e "USAGE:\n\
-    $(basename $0) --help       # 使用帮助\n\
-    $(basename $0) -x file.deb  # 解压\n\
+    echo -e "USAGE:
+    $(basename $0) --help       # 使用帮助
+    $(basename $0) -x file.deb  # 解压
     $(basename $0) -b dir       # 打包"
 
     exit 1

+ 8 - 0
yocto/rpi4.sh

@@ -0,0 +1,8 @@
+for i in $(awk '{print $2}' /data/temp/rpi4.list); do
+    # echo $i
+    if grep -rn -q "^Package: $i$" /data/temp/lists/172.29.220.242*; then
+        echo "has === $i"
+    else
+        echo "no *** $i"
+    fi
+done