소스 검색

first_stage_install

侠义书生 3 년 전
부모
커밋
3ccbc8f7d9
1개의 변경된 파일104개의 추가작업 그리고 10개의 파일을 삭제
  1. 104 10
      debootstrap/apt_list/my_debootstrap2.sh

+ 104 - 10
debootstrap/apt_list/my_debootstrap2.sh

@@ -1,11 +1,13 @@
 #!/bin/bash
 
+set -e
+
 ########## functions ##########
 # log
 MY_LOG_FILE=/var/tmp/my_analysis.log
 : >${MY_LOG_FILE}
 
-mylog() {
+function mylog() {
     if [ $# -eq 1 ]; then
         echo "---> $1" >>${MY_LOG_FILE}
     else
@@ -15,7 +17,7 @@ mylog() {
     fi
 }
 
-mylog2() {
+function mylog2() {
     echo "---> $@" >>${MY_LOG_FILE}
 }
 
@@ -42,7 +44,7 @@ function wgetfile() {
     fi
 }
 
-without() {
+function without() {
     # usage:  without "a b c" "a d" -> "b" "c"
     (
         echo "$1" | tr ' ' '\n' | sort | uniq
@@ -52,7 +54,7 @@ without() {
 }
 
 # 从 文件1 中排除 文件2
-without2() {
+function without2() {
     (
         cat $1
         cat $2
@@ -114,6 +116,7 @@ function config_my() {
     SUITES="v101"
     COMPONENTS="main"
     ARCH="amd64"
+    TARGET="/var/tmp/rootfs"
 }
 
 function download_indices() {
@@ -137,7 +140,7 @@ function download_indices() {
     done
 }
 
-resolve_deps() {
+function resolve_deps() {
     local PKGS="$*"
     local ALLPKGS="$PKGS"
     local ALLPKGS2=""
@@ -205,7 +208,96 @@ function download_debs() {
     done | column -t >${TARGET_DIR}/deb.list
 }
 
-TARGET_DIR="/var/tmp/indices"
+function first_stage_install() {
+    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 -sf usr/"$dir" "$TARGET/$dir"
+        done
+    }
+
+    setup_merged_usr
+
+    required_deps="$(cat ${TARGET_DIR}/01-required@deps.txt2)"
+
+    for pkg in $required_deps; do
+        local filename=$(grep "^${pkg} " ${TARGET_DIR}/deb.list | awk '{print $2}')
+        # echo ${filename#*//}
+        dpkg-deb --fsys-tarfile "${TARGET_DIR}/${filename#*//}" | tar -k -xvf - -C $TARGET
+    done
+
+    # 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"
+    }
+
+    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
+    }
+
+    mkdir -p "$TARGET/var/lib/dpkg"
+    : >"$TARGET/var/lib/dpkg/status"
+    : >"$TARGET/var/lib/dpkg/available"
+
+    conditional_cp /etc/resolv.conf "$TARGET"
+    conditional_cp /etc/hostname "$TARGET"
+
+    if [ ! -e "$TARGET/etc/fstab" ]; then
+        echo '# UNCONFIGURED FSTAB FOR BASE SYSTEM' >"$TARGET/etc/fstab"
+        chown 0:0 "$TARGET/etc/fstab"
+        chmod 644 "$TARGET/etc/fstab"
+    fi
+
+    setup_devices_simple
+
+    # setup_apt_sources
+    echo "deb ${MIRRORS} ${SUITES} ${COMPONENTS}" >"$TARGET/etc/apt/sources.list"
+}
+
+TARGET_DIR="/var/tmp"
 if [ ! -d "${TARGET_DIR}" ]; then
     mkdir -p "${TARGET_DIR}"
 fi
@@ -217,8 +309,10 @@ config_my
 
 # download_indices
 
-work_out_debs
-all_debs=$(echo "${required_deps} ${important_deps}" | xargs | tr ' ' '\n' | sort | uniq)
-# echo "${all_debs}"
+# work_out_debs
+# all_debs=$(echo "${required_deps} ${important_deps}" | xargs | tr ' ' '\n' | sort | uniq)
+# # echo "${all_debs}"
+
+# download_debs "${all_debs}"
 
-download_debs "${all_debs}"
+first_stage_install