From 4ce1ba8f6d8a2cfe4e8f6bf60b9e714c818f1087 Mon Sep 17 00:00:00 2001
From: WangRunji <wangrunji0408@163.com>
Date: Mon, 2 Apr 2018 15:28:32 +0800
Subject: [PATCH] Rewrite README. Add docker.

---
 .gitignore           |  1 +
 Makefile             | 27 ++++++++++++++++++++++++++-
 README.md            | 17 ++++++++++-------
 docker/.bash_aliases |  1 +
 docker/Dockerfile    | 40 ++++++++++++++++++++++++++++++++++++++++
 docker/README.md     | 18 ++++++++++++++++++
 docker/entrypoint.sh | 19 +++++++++++++++++++
 src/lib.rs           |  1 +
 8 files changed, 116 insertions(+), 8 deletions(-)
 create mode 100644 docker/.bash_aliases
 create mode 100644 docker/Dockerfile
 create mode 100644 docker/README.md
 create mode 100755 docker/entrypoint.sh

diff --git a/.gitignore b/.gitignore
index 6eeec87..168da4f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
 build
 target
+Cargo.lock
\ No newline at end of file
diff --git a/Makefile b/Makefile
index cda8749..5e1b348 100644
--- a/Makefile
+++ b/Makefile
@@ -34,9 +34,34 @@ $(kernel): kernel $(rust_os) $(assembly_object_files) $(linker_script)
 		$(assembly_object_files) $(rust_os)
 
 kernel:
-	@xargo build --target $(target)
+	@RUST_TARGET_PATH=$(shell pwd) xargo build --target $(target)
 
 # compile assembly files
 build/arch/$(arch)/%.o: src/arch/$(arch)/%.asm
 	@mkdir -p $(shell dirname $@)
 	@nasm -felf64 $< -o $@
+
+# used by docker_* targets
+docker_image ?= blog_os
+tag ?= 0.1
+docker_cargo_volume ?=  blogos-$(shell id -u)-$(shell id -g)-cargo
+docker_rustup_volume ?=  blogos-$(shell id -u)-$(shell id -g)-rustup
+docker_args ?= -e LOCAL_UID=$(shell id -u) -e LOCAL_GID=$(shell id -g) -v $(docker_cargo_volume):/usr/local/cargo -v $(docker_rustup_volume):/usr/local/rustup -v $(shell pwd):$(shell pwd) -w $(shell pwd)
+docker_clean_args ?= $(docker_cargo_volume) $(docker_rustup_volume)
+
+# docker_* targets 
+
+docker_build:
+	@docker build docker/ -t $(docker_image):$(tag)
+
+docker_iso: 
+	@docker run --rm $(docker_args) $(docker_image):$(tag) make iso
+
+docker_run: docker_iso
+	@qemu-system-x86_64 -cdrom $(iso) -s
+
+docker_interactive:
+	@docker run -it --rm $(docker_args) $(docker_image):$(tag) 
+
+docker_clean:
+	@docker volume rm $(docker_clean_args)
\ No newline at end of file
diff --git a/README.md b/README.md
index 3a6fbc9..3c6ed0b 100644
--- a/README.md
+++ b/README.md
@@ -1,16 +1,19 @@
-# Blog OS (Double Faults)
-[![Build Status](https://travis-ci.org/phil-opp/blog_os.svg?branch=post_10)](https://travis-ci.org/phil-opp/blog_os/branches)
+# RustOS for x86_64 SMP
 
-This repository contains the source code for the [Double Faults](http://os.phil-opp.com/double-faults.html) post of the [Writing an OS in Rust](http://os.phil-opp.com) series.
+A project of THU OS2018 spring.
 
-**Check out the [master branch](https://github.com/phil-opp/blog_os) for more information.**
+[Project Wiki](http://os.cs.tsinghua.edu.cn/oscourse/OS2018spring/projects/g11)
+
+The goal is to write a mini OS in Rust with multicore supporting.
+
+It will start from the post of the [Writing an OS in Rust](http://os.phil-opp.com) series. Then reimplement [xv6-x86_64](https://github.com/jserv/xv6-x86_64) in Rust style.
 
 ## Building
-You need to have `nasm`, `grub-mkrescue`, `xorriso`, `qemu`, a nightly Rust compiler, and [xargo] installed. Then you can run it using `make run`.
 
-[xargo]: https://github.com/japaric/xargo
+You need to have `nasm`, `grub-mkrescue`, `xorriso`, `qemu`, a nightly Rust compiler, and `xargo` installed. Then you can run it using `make run`.
 
-Please file an issue if you have any problems.
+A docker image is available and recommanded. Read [this](docker/README.md) for details.
 
 ## License
+
 The source code is dual-licensed under MIT or the Apache License (Version 2.0).
diff --git a/docker/.bash_aliases b/docker/.bash_aliases
new file mode 100644
index 0000000..6ddc1be
--- /dev/null
+++ b/docker/.bash_aliases
@@ -0,0 +1 @@
+PS1="\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[1;35m\]<$IMAGE_NAME>\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "
diff --git a/docker/Dockerfile b/docker/Dockerfile
new file mode 100644
index 0000000..396f214
--- /dev/null
+++ b/docker/Dockerfile
@@ -0,0 +1,40 @@
+FROM rustlang/rust:nightly
+
+ENV IMAGE_NAME=blog_os-docker
+
+RUN apt-get update && \
+    apt-get install -q -y --no-install-recommends \
+    nasm \
+    binutils \
+    grub-common \
+    xorriso \
+    grub-pc-bin && \
+    apt-get autoremove -q -y && \
+    apt-get clean -q -y && \
+    rm -rf /var/lib/apt/lists/* && \
+    cargo install xargo && \
+    rustup component add rust-src
+
+ENV GOSU_VERSION 1.10
+
+RUN set -ex; \
+	\
+	fetchDeps=' \
+		ca-certificates \
+		wget \
+	'; \
+	apt-get update; \
+	apt-get install -y --no-install-recommends $fetchDeps; \
+	rm -rf /var/lib/apt/lists/*; \
+	\
+	dpkgArch="$(dpkg --print-architecture | awk -F- '{ print $NF }')"; \
+	wget -O /usr/local/bin/gosu "https://github.com/tianon/gosu/releases/download/$GOSU_VERSION/gosu-$dpkgArch"; \
+	chmod +x /usr/local/bin/gosu; \
+# verify that the binary works
+	gosu nobody true; 
+
+COPY entrypoint.sh /usr/local/bin/
+COPY .bash_aliases /etc/skel/
+
+ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
+CMD ["/bin/bash"]
diff --git a/docker/README.md b/docker/README.md
new file mode 100644
index 0000000..3d28777
--- /dev/null
+++ b/docker/README.md
@@ -0,0 +1,18 @@
+# Building Blog OS using Docker
+Inspired by [redox].
+You just need `git`, `make`, and `docker`.
+It is beter to use a non-privileged user to run the `docker` command, which is usually achieved by adding the user to the `docker` group.
+
+## Run the container to build Blog OS
+You can build the docker image using `make docker_build` and run it using `make docker_run`.
+
+## Run the container interactively
+You can use the `make` target `docker_interactive` to get a shell in the container.
+
+## Clear the toolchain caches (Cargo & Rustup)
+To clean the docker volumes used by the toolchain, you just need to run `make docker_clean`.
+
+[redox]: https://github.com/redox-os/redox
+
+## License
+The source code is dual-licensed under MIT or the Apache License (Version 2.0). This excludes the `blog` directory.
diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh
new file mode 100755
index 0000000..f8abd58
--- /dev/null
+++ b/docker/entrypoint.sh
@@ -0,0 +1,19 @@
+#!/bin/sh
+USER_NAME=blogos
+USER_UID=${LOCAL_UID:-9001}
+USER_GID=${LOCAL_GID:-9001}
+
+groupadd --non-unique --gid $USER_GID $USER_NAME
+useradd --non-unique --create-home --uid $USER_UID --gid $USER_GID $USER_NAME
+
+export HOME=/home/$USER_NAME
+
+TESTFILE=$RUSTUP_HOME/settings.toml
+CACHED_UID=$(stat -c "%u" $TESTFILE)
+CACHED_GID=$(stat -c "%g" $TESTFILE)
+
+if [ $CACHED_UID != $USER_UID  ] || [ $USER_GID != $CACHED_GID  ]; then
+    chown $USER_UID:$USER_GID -R $CARGO_HOME $RUSTUP_HOME
+fi
+
+exec gosu $USER_NAME "$@"
diff --git a/src/lib.rs b/src/lib.rs
index 50c13a7..71611f7 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,3 +1,4 @@
+#![feature(ptr_internals)]
 #![feature(lang_items)]
 #![feature(const_fn)]
 #![feature(alloc)]