Skip to content

Commit 5a54372

Browse files
committedSep 7, 2022
Initial section material for 2022.
0 parents  commit 5a54372

File tree

7 files changed

+653
-0
lines changed

7 files changed

+653
-0
lines changed
 

‎.gitignore

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#*#
2+
*.aux
3+
*.backup
4+
*.core
5+
*.dSYM
6+
*.dvi
7+
*.gcda
8+
*.gch
9+
*.log
10+
*.noopt
11+
*.o
12+
*.optimized
13+
*.pch
14+
*.unsafe
15+
*.xcodeproj
16+
*~
17+
.DS_Store
18+
.cproject
19+
.deps
20+
.gitcheckout
21+
.goutputstream-*
22+
.hg
23+
.project
24+
.svn
25+
.vscode
26+
a.out
27+
core*
28+
cmake-build-*
29+
cscope.out
30+
pset.tgz
31+
pset[1-9].tgz
32+
pset[1-9]grade.tgz
33+
strace.out
34+
tags
35+
tags.*
36+
typescript
37+
vgcore*

‎README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CS 61 Sections
2+
==============
3+
4+
This repository contains section material for Harvard’s CS 61 class,
5+
Systems Programming and Machine Organization.
6+
7+
For more information, see the course site:
8+
https://cs61.seas.harvard.edu/
9+
10+
Follow the section discussion using the pages listed under “Sections”
11+
at https://cs61.seas.harvard.edu/

‎common/rules.mk

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
# compiler flags
2+
CFLAGS := -std=gnu11 -Wall -Wextra -Wshadow -g $(DEFS) $(CFLAGS)
3+
CXXFLAGS := -std=gnu++2a -Wall -Wextra -Wshadow -g $(DEFS) $(CXXFLAGS)
4+
5+
O ?= -O3
6+
ifeq ($(filter 0 1 2 3 s z g fast,$(O)),$(strip $(O)))
7+
override O := -O$(O)
8+
endif
9+
10+
PTHREAD ?= 0
11+
ifeq ($(PTHREAD),1)
12+
CFLAGS += -pthread
13+
CXXFLAGS += -pthread
14+
WANT_TSAN ?= 1
15+
endif
16+
17+
PIE ?= 1
18+
ifeq ($(PIE),0)
19+
LDFLAGS += -no-pie
20+
endif
21+
22+
# compiler variant
23+
ifeq ($(COMPILER),clang)
24+
ifeq ($(origin CC),default)
25+
ifeq ($(shell if clang --version | grep -e 'LLVM\|clang' >/dev/null; then echo 1; else echo 0; fi),1)
26+
CC = clang
27+
endif
28+
endif
29+
ifeq ($(origin CXX),default)
30+
ifeq ($(shell if clang++ --version | grep -e 'LLVM\|clang' >/dev/null; then echo 1; else echo 0; fi),1)
31+
CXX = clang++
32+
endif
33+
endif
34+
endif
35+
ifeq ($(COMPILER),gcc)
36+
ifeq ($(origin CC),default)
37+
ifeq ($(shell if gcc --version 2>&1 | grep -e 'Free Software' >/dev/null; then echo 1; else echo 0; fi),1)
38+
CC = gcc
39+
endif
40+
endif
41+
ifeq ($(origin CXX),default)
42+
ifeq ($(shell if g++ --version 2>&1 | grep -e 'Free Software' >/dev/null; then echo 1; else echo 0; fi),1)
43+
CXX = g++
44+
endif
45+
endif
46+
endif
47+
48+
ISCLANG := $(shell if $(CC) --version | grep -e 'LLVM\|clang' >/dev/null; then echo 1; else echo 0; fi)
49+
ifeq ($(ISCLANG),1)
50+
BADCXXFLAGS ?= -fno-if-conversion -fno-if-conversion2
51+
endif
52+
53+
ifeq ($(NEED_CXX_GCC),1)
54+
GXX_ISCLANG := $(shell if g++ --version | grep -e 'LLVM\|clang' >/dev/null; then echo 1; else echo 0; fi)
55+
ifeq ($(GXX_ISCLANG),1)
56+
ifeq ($(shell if g++-12 --version 2>/dev/null | grep -e 'Free Software' >/dev/null; then echo 1; else echo 0; fi),1)
57+
CXX_GCC = g++-12
58+
else ifeq ($(shell if g++-11 --version 2>/dev/null | grep -e 'Free Software' >/dev/null; then echo 1; else echo 0; fi),1)
59+
CXX_GCC = g++-11
60+
else ifeq ($(shell if g++-10 --version 2>/dev/null | grep -e 'Free Software' >/dev/null; then echo 1; else echo 0; fi),1)
61+
CXX_GCC = g++-10
62+
else
63+
CXX_GCC = false
64+
endif
65+
else
66+
CXX_GCC = g++
67+
endif
68+
endif
69+
70+
# sanitizer arguments
71+
ifndef SAN
72+
SAN := $(SANITIZE)
73+
endif
74+
ifeq ($(SAN),1)
75+
ifndef ASAN
76+
ASAN := $(if $(strip $(shell $(CC) -v 2>&1 | grep 'build=aarch.*target=x86')),,1)
77+
endif
78+
endif
79+
ifndef TSAN
80+
ifeq ($(WANT_TSAN),1)
81+
TSAN := $(SAN)
82+
endif
83+
endif
84+
85+
check_for_sanitizer = $(if $(strip $(shell $(CC) -fsanitize=$(1) -x c -E /dev/null 2>&1 | grep sanitize=)),$(info ** WARNING: The `$(CC)` compiler does not support `-fsanitize=$(1)`.),1)
86+
ifeq ($(TSAN),1)
87+
ifeq ($(call check_for_sanitizer,thread),1)
88+
CFLAGS += -fsanitize=thread
89+
CXXFLAGS += -fsanitize=thread
90+
endif
91+
else
92+
ifeq ($(or $(ASAN),$(LSAN),$(LEAKSAN)),1)
93+
ifeq ($(call check_for_sanitizer,address),1)
94+
CFLAGS += -fsanitize=address
95+
CXXFLAGS += -fsanitize=address
96+
endif
97+
endif
98+
ifeq ($(or $(LSAN),$(LEAKSAN)),1)
99+
ifeq ($(call check_for_sanitizer,leak),1)
100+
CFLAGS += -fsanitize=leak
101+
CXXFLAGS += -fsanitize=leak
102+
endif
103+
endif
104+
endif
105+
ifeq ($(or $(UBSAN),$(SAN)),1)
106+
ifeq ($(call check_for_sanitizer,undefined),1)
107+
CFLAGS += -fsanitize=undefined -fno-sanitize-recover=undefined
108+
CXXFLAGS += -fsanitize=undefined -fno-sanitize-recover=undefined
109+
endif
110+
endif
111+
112+
# profiling
113+
ifeq ($(or $(PROFILE),$(PG)),1)
114+
CFLAGS += -pg
115+
CXXFLAGS += -pg
116+
endif
117+
118+
# these rules ensure dependencies are created
119+
DEPCFLAGS = -MD -MF $(DEPSDIR)/$(patsubst %.o,%,$(@F)).d -MP
120+
DEPSDIR := .deps
121+
BUILDSTAMP := $(DEPSDIR)/rebuildstamp
122+
DEPFILES := $(wildcard $(DEPSDIR)/*.d)
123+
ifneq ($(DEPFILES),)
124+
include $(DEPFILES)
125+
endif
126+
127+
# when the C compiler or optimization flags change, rebuild all objects
128+
ifneq ($(strip $(DEP_CC)),$(strip $(CC) $(CPPFLAGS) $(CFLAGS) $(O)))
129+
DEP_CC := $(shell mkdir -p $(DEPSDIR); echo >$(BUILDSTAMP); echo "DEP_CC:=$(CC) $(CPPFLAGS) $(CFLAGS) $(O)" >$(DEPSDIR)/_cc.d)
130+
endif
131+
ifneq ($(strip $(DEP_CXX)),$(strip $(CXX) $(CPPFLAGS) $(CXXFLAGS) $(O) $(LDFLAGS)))
132+
DEP_CXX := $(shell mkdir -p $(DEPSDIR); echo >$(BUILDSTAMP); echo "DEP_CXX:=$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(O) $(LDFLAGS)" >$(DEPSDIR)/_cxx.d)
133+
endif
134+
135+
136+
V = 0
137+
ifeq ($(V),1)
138+
run = $(1) $(3)
139+
xrun = /bin/echo "$(1) $(3)" && $(1) $(3)
140+
else
141+
run = @$(if $(2),/bin/echo " $(2) $(3)" &&,) $(1) $(3)
142+
xrun = $(if $(2),/bin/echo " $(2) $(3)" &&,) $(1) $(3)
143+
endif
144+
runquiet = @$(1) $(3)
145+
146+
CXX_LINK_PREREQUISITES = $(CXX) $(CXXFLAGS) $(LDFLAGS) $(O) -o $@ $^
147+
148+
CLEANASM = 1
149+
ifeq ($(CLEANASM),1)
150+
cleanasm = perl -ni -e '$$badsection = !!/\.note\.gnu/ if /^\s+\.section/; print if !/^(?:\# BB|\s+\.cfi|\s+\.p2align|\s+\.loc|\.LF[BE]|\.LVL|\s+\# =>This|\s+\# kill)/ && !$$badsection' $(1)
151+
else
152+
cleanasm = :
153+
endif
154+
155+
DEFAULT_ASM_CXXFLAGS ?= $(O)
156+
flagged_compile = @ARGS=$$(grep '^//!' $< | sed 's/.*!//$(patsubst %,;s/ % */ /,$(BADCXXFLAGS));s/^ | $$//'); \
157+
if test -z "$$ARGS"; then ARGS="$(DEFAULT_ASM_CXXFLAGS)"; fi; \
158+
$(call xrun,$(CXX) $(3) $$ARGS -o $(2) $(1),COMPILE $$ARGS $(1) -o $(2))
159+
flagged_compile_S = $(call flagged_compile,$(1),$(2),$(filter-out -g,$(3) -S)) && { $(call cleanasm,$(2)); }
160+
flagged_compile_c = $(call flagged_compile,$(1),$(2),$(3) -c)
161+
162+
163+
PERCENT := %
164+
165+
# cancel implicit rules we don't want
166+
%: %.c
167+
%.o: %.c
168+
%: %.cc
169+
%.o: %.cc
170+
%: %.o
171+
%.o: %.s
172+
173+
$(BUILDSTAMP):
174+
@mkdir -p $(@D)
175+
@echo >$@
176+
177+
always:
178+
@:
179+
180+
clean-hook:
181+
@:
182+
183+
.PHONY: always clean-hook
184+
.PRECIOUS: %.o

‎cs61-run-docker

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#! /bin/bash
2+
3+
maindir=""
4+
destdir=cs61-sections
5+
6+
fresh=
7+
verbose=false
8+
arch="`arch`"
9+
tag=
10+
platform=
11+
while test "$#" -ne 0; do
12+
if test "$1" = "-f" -o "$1" = "--fresh"; then
13+
fresh=1
14+
shift
15+
elif test "$1" = "-V" -o "$1" = "--verbose"; then
16+
verbose=true
17+
shift
18+
elif test "$1" = "-a" -o "$1" = "--arm" -o "$1" = "--arm64"; then
19+
if test "$arch" = "arm64" -o "$arch" = "aarch64"; then
20+
platform=linux/arm64
21+
shift
22+
else
23+
echo "\`cs61-run-docker --arm\` only works on ARM64 hosts" 1>&2
24+
exit 1
25+
fi
26+
elif test "$1" = "-x" -o "$1" = "--x86-64" -o "$1" = "--x86_64" -o "$1" = "--amd64"; then
27+
platform=linux/amd64
28+
shift
29+
else
30+
armtext=
31+
if test "$arch" = "arm64" -o "$arch" = "aarch64"; then
32+
armtext=" [-a|--arm] [-x|--x86-64]"
33+
fi
34+
echo "Usage: cs61-run-docker [-f|--fresh]$armtext [-V|--verbose]" 1>&2
35+
exit 1
36+
fi
37+
done
38+
if test -z "$platform" -a \( "$arch" = "arm64" -o "$arch" = "aarch64" \); then
39+
platform=linux/arm64
40+
elif test -z "$platform"; then
41+
platform=linux/amd64
42+
fi
43+
if test -z "$tag" -a "$platform" = linux/arm64; then
44+
tag=cs61:arm64
45+
elif test -z "$tag"; then
46+
tag=cs61:latest
47+
fi
48+
49+
50+
vexec () {
51+
if $verbose; then
52+
echo "$@"
53+
fi
54+
exec "$@"
55+
}
56+
57+
if stat --format %i / >/dev/null 2>&1; then
58+
statformatarg="--format"
59+
else
60+
statformatarg="-f"
61+
fi
62+
myfileid=`stat $statformatarg %d:%i "${BASH_SOURCE[0]}" 2>/dev/null`
63+
64+
dir="`pwd`"
65+
subdir=""
66+
while test "$dir" != / -a "$dir" != ""; do
67+
thisfileid=`stat $statformatarg %d:%i "$dir"/cs61-run-docker 2>/dev/null`
68+
if test -n "$thisfileid" -a "$thisfileid" = "$myfileid"; then
69+
maindir="$dir"
70+
break
71+
fi
72+
subdir="/`basename "$dir"`$subdir"
73+
dir="`dirname "$dir"`"
74+
done
75+
76+
if test -z "$maindir" && expr "${BASH_SOURCE[0]}" : / >/dev/null 2>&1; then
77+
maindir="`dirname "${BASH_SOURCE[0]}"`"
78+
subdir=""
79+
fi
80+
81+
ssharg=
82+
sshenvarg=
83+
if test -n "$SSH_AUTH_SOCK" -a "`uname`" = Darwin; then
84+
ssharg=" --mount type=bind,src=/run/host-services/ssh-auth.sock,target=/run/host-services/ssh-auth.sock"
85+
sshenvarg=" -e SSH_AUTH_SOCK=/run/host-services/ssh-auth.sock"
86+
fi
87+
88+
if test -n "$maindir" -a -z "$fresh"; then
89+
existing_image="`docker ps -f status=running -f ancestor=$tag -f volume=/host_mnt"$maindir" --no-trunc --format "{{.CreatedAt}},{{.ID}}" | sort -r | head -n 1`"
90+
if test -n "$existing_image"; then
91+
created_at="`echo $existing_image | sed 's/,.*//'`"
92+
image="`echo $existing_image | sed 's/^.*,//'`"
93+
image12="`echo $image | head -c 12`"
94+
echo "* Using running container $image12, created $created_at" 1>&2
95+
echo "- To start a new container, exit then \`cs61-run-docker -f\`" 1>&2
96+
echo "- To kill this container, exit then \`docker kill $image12\`" 1>&2
97+
vexec docker exec -it$sshenvarg $image /bin/bash
98+
fi
99+
fi
100+
101+
netarg=
102+
if test `uname` = Darwin; then
103+
if ! netstat -n -a -p tcp | grep '\.6169[ ].*LISTEN' >/dev/null; then
104+
netarg="$netarg "'--expose=6169/tcp -p 6169:6169/tcp'
105+
fi
106+
if ! netstat -n -a -p tcp | grep '\.12949[ ].*LISTEN' >/dev/null; then
107+
netarg="$netarg "'--expose=12949/tcp -p 12949:12949/tcp'
108+
fi
109+
elif test -x /bin/netstat; then
110+
if ! netstat -n -a -p tcp | grep '\.6169[ ].*LISTEN' >/dev/null; then
111+
netarg="$netarg "'--expose=6169/tcp -p 6169:6169/tcp'
112+
fi
113+
if ! netstat -n -l -t | grep ':12949[ ]' >/dev/null; then
114+
netarg="$netarg "'--expose=12949/tcp -p 12949:12949/tcp'
115+
fi
116+
fi
117+
118+
if test -n "$maindir"; then
119+
vexec docker run -it --platform $platform --rm --privileged --cap-add=SYS_PTRACE --cap-add=NET_ADMIN --security-opt seccomp=unconfined -v "$maindir":/home/cs61-user/$destdir$ssharg -w "/home/cs61-user/$destdir$subdir" $netarg$sshenvarg $tag
120+
else
121+
vexec docker run -it --platform $platform --rm --privileged --cap-add=SYS_PTRACE --cap-add=NET_ADMIN --security-opt seccomp=unconfined $netarg$sshenvarg $tag
122+
fi

‎docker/Dockerfile

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
FROM ubuntu:jammy
2+
3+
# set environment variables for tzdata
4+
ARG TZ=America/New_York
5+
ENV TZ=${TZ}
6+
7+
# include manual pages and documentation
8+
ARG DEBIAN_FRONTEND=noninteractive
9+
RUN apt-get update &&\
10+
yes | unminimize
11+
12+
# install GCC-related packages
13+
RUN apt-get -y install\
14+
binutils-doc\
15+
cpp-doc\
16+
gcc-doc\
17+
g++\
18+
g++-multilib\
19+
gdb\
20+
gdb-doc\
21+
glibc-doc\
22+
libblas-dev\
23+
liblapack-dev\
24+
liblapack-doc\
25+
libstdc++-12-doc\
26+
make\
27+
make-doc
28+
29+
# install clang-related packages
30+
RUN apt-get -y install\
31+
clang\
32+
clang-14-doc\
33+
lldb
34+
35+
# install qemu for WeensyOS (sadly, this pulls in a lot of crap)
36+
RUN apt-get -y install\
37+
qemu-system-x86
38+
39+
# install programs used for system exploration
40+
RUN apt-get -y install\
41+
blktrace\
42+
linux-tools-generic\
43+
strace\
44+
tcpdump
45+
46+
# install interactive programs (emacs, vim, nano, man, sudo, etc.)
47+
RUN apt-get -y install\
48+
bc\
49+
curl\
50+
dc\
51+
emacs-nox\
52+
git\
53+
git-doc\
54+
man\
55+
micro\
56+
nano\
57+
psmisc\
58+
sudo\
59+
vim\
60+
wget
61+
62+
# set up libraries
63+
RUN apt-get -y install\
64+
libreadline-dev\
65+
locales\
66+
wamerican
67+
68+
# install programs used for networking
69+
RUN apt-get -y install\
70+
dnsutils\
71+
inetutils-ping\
72+
iproute2\
73+
net-tools\
74+
netcat\
75+
telnet\
76+
time\
77+
traceroute
78+
79+
# set up default locale
80+
RUN locale-gen en_US.UTF-8
81+
ENV LANG en_US.UTF-8
82+
83+
# remove unneeded .deb files
84+
RUN rm -r /var/lib/apt/lists/*
85+
86+
# set up passwordless sudo for user cs61-user
87+
RUN useradd -m -s /bin/bash cs61-user && \
88+
echo "cs61-user ALL=(ALL:ALL) NOPASSWD: ALL" > /etc/sudoers.d/cs61-init
89+
90+
# create binary reporting version of dockerfile
91+
RUN (echo '#\!/bin/sh'; echo 'echo 14') > /usr/bin/cs61-docker-version; chmod ugo+rx,u+w,go-w /usr/bin/cs61-docker-version
92+
93+
# git build arguments
94+
ARG USER=CS61\ User
95+
ARG EMAIL=nobody@example.com
96+
97+
# configure your environment
98+
USER cs61-user
99+
RUN git config --global user.name "${USER}" && \
100+
git config --global user.email "${EMAIL}" && \
101+
(echo "(custom-set-variables"; echo " '(c-basic-offset 4)"; echo " '(indent-tabs-mode nil))") > ~/.emacs && \
102+
(echo "set expandtab"; echo "set shiftwidth=4"; echo "set softtabstop=4") > ~/.vimrc && \
103+
cat /dev/null > ~/.bash_profile && \
104+
echo "# 2022: avoid a Docker bug with user mapping by listing working directory" >> ~/.bash_profile && \
105+
echo "ls -al > /dev/null" >> ~/.bash_profile && \
106+
echo "# make ssh-auth.sock user-readable" >> ~/.bash_profile && \
107+
(echo "if test -f /run/host-services/ssh-auth.sock; then"; echo " sudo chown cs61-user:cs61-user /run/host-services/ssh-auth.sock"; echo "fi") >> ~/.bash_profile && \
108+
echo ". ~/.bashrc" >> ~/.bash_profile && \
109+
rm -f ~/.bash_logout && \
110+
echo "add-auto-load-safe-path ~" > ~/.gdbinit
111+
112+
WORKDIR /home/cs61-user
113+
CMD ["/bin/bash", "-l"]
114+
115+
# Initial version of this Dockerfile by Todd Morrill, CS 61 DCE student

‎docker/README.md

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
CS 61 Docker
2+
============
3+
4+
The [Docker][] container-based virtualization service lets you run a
5+
minimal CS 61 environment, including Linux, on a Mac OS X or Windows
6+
computer, without the overhead of a full virtual machine like [VMware
7+
Workstation][], [VMware Fusion][], or [VirtualBox][].
8+
9+
It should be possible to do *all* CS 61 problem sets on CS 61 Docker.
10+
11+
Advantages of Docker:
12+
13+
* Docker can start and stop virtual machines incredibly quickly.
14+
* Docker-based virtual machines are small and occupy little space on your machine.
15+
* With Docker, you can easily *edit* your code in your home environment, but
16+
*compile and run* it on a Linux host.
17+
18+
Disadvantages of Docker:
19+
20+
* Docker does not offer a graphical environment. You will need to run all CS 61
21+
programs exclusively in the terminal.
22+
* Docker technology is less user-friendly than virtual machines. You’ll have
23+
to type weird commands.
24+
* You won’t get the fun, different feeling of a graphical Linux desktop.
25+
26+
27+
## Creating CS 61 Docker
28+
29+
1. Download and install [Docker][].
30+
31+
2. Clone the [cs61-lectures repository][cs61-lectures] onto your computer.
32+
33+
3. Change into the `cs61-lectures/docker` directory.
34+
35+
4. Run this command. It will take a while—up to ten minutes.
36+
37+
```shellsession
38+
$ ./cs61-build-docker
39+
```
40+
41+
The command starts up a virtual Linux-based computer running inside your
42+
computer. It then installs a bunch of software useful for CS 61 on that
43+
environment, then takes a snapshot of the running environment. (The
44+
snapshot has a name, such as `cs61:latest` or `cs61:arm64`.) Once the
45+
snapshot is created, it’ll take just a second or so for Docker to restart
46+
it.
47+
48+
We may need to change the Docker image during the term. If we do, you’ll
49+
update your repository to get the latest Dockerfile, then re-run the
50+
`./cs61-build-docker` command from Step 4. However, later runs should be
51+
faster since they’ll take advantage of your previous work.
52+
53+
> `./cs61-build-docker` is a wrapper around `docker build`. On x86-64 hosts, it runs
54+
> `docker build -t cs61:latest -f Dockerfile --platform linux/amd64 .`
55+
{.note}
56+
57+
## Running CS 61 Docker by script
58+
59+
Our handout repositories, including `cs61-lectures` and `cs61-psets`, contain
60+
a `cs61-run-docker` script that provides good arguments and boots Docker into
61+
a view of the current directory. We will update this script throughout the
62+
term.
63+
64+
For example, here’s an example of running CS 61 Docker on a Mac OS X host. At
65+
first, `uname` (a program that prints the name of the currently running
66+
operating system) reports `Darwin`. But after `./cs61-run-docker` connects the
67+
terminal to a Linux virtual machine, `uname` reports `Linux`. At the end of
68+
the example, `exit` quits the Docker environment and returns the terminal to
69+
Mac OS X.
70+
71+
```shellsession
72+
$ cd ~/cs61-lectures
73+
$ uname
74+
Darwin
75+
$ uname -a
76+
Darwin Eddies-MacBook-Pro.local 20.6.0 Darwin Kernel Version 20.6.0: Wed Jun 23 00:26:27 PDT 2021; root:xnu-7195.141.2~5/RELEASE_ARM64_T8101 arm64
77+
$ ./cs61-run-docker
78+
cs61-user@a47f05ea5085:~/cs61-lectures$ uname
79+
Linux
80+
cs61-user@a47f05ea5085:~/cs61-lectures$ uname -a
81+
Linux a47f05ea5085 5.10.47-linuxkit #1 SMP PREEMPT Sat Jul 3 21:50:16 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
82+
cs61-user@a47f05ea5085:~/cs61-lectures$ ls
83+
common cs61-run-docker docker README.md
84+
cs61-user@a47f05ea5085:~/cs61-lectures$ exit
85+
exit
86+
$
87+
```
88+
89+
A prompt like `cs61-user@a47f05ea5085:~$` means that your terminal is
90+
connected to the VM. (The `a47f05ea5085` part is a unique identifier for this
91+
running VM.) You can execute any Linux commands you want. To escape from the
92+
VM, type Control-D or run the `exit` command.
93+
94+
The script assumes your Docker container is named `cs61:latest` or `cs61:arm64`.
95+
96+
97+
### Running CS 61 Docker by hand
98+
99+
If you don’t want to use the script, use a command like the following.
100+
101+
```shellsession
102+
$ docker run -it --platform linux/amd64 --rm -v ~/cs61-lectures:/home/cs61-user/cs61-lectures cs61:latest
103+
```
104+
105+
Explanation:
106+
107+
* `docker run` tells Docker to start a new virtual machine.
108+
* `-it` says Docker should run interactively (`-i`) using a terminal (`-t`).
109+
* `--platform linux/amd64` says Docker should emulate an x86-64-based machine.
110+
It’s necessary to specify this if you have (for example) an Apple M1-based
111+
laptop.
112+
* `--rm` says Docker should remove the virtual machine when it is done.
113+
* `-v LOCALDIR:LINUXDUR` says Docker should share a directory between your
114+
host and the Docker virtual machine. Here, I’ve asked for the host’s
115+
`~/cs61-lectures` directory to be mapped inside the virtual machine onto the
116+
`/home/cs61-user/cs61-lectures` directory, which is the virtual machine
117+
user’s `~/cs61-lectures` directory.
118+
* `cs61:latest` names the Docker image to run (namely, the one you built).
119+
120+
Here’s an example session:
121+
122+
```shellsession
123+
$ docker run -it --platform linux/amd64 --rm -v ~/cs61-lectures:/home/cs61-user/cs61-lectures cs61:latest
124+
cs61-user@a15e6c4c8dbe:~$ ls
125+
cs61-lectures
126+
cs61-user@a15e6c4c8dbe:~$ echo "Hello, world"
127+
Hello, world
128+
cs61-user@a15e6c4c8dbe:~$ cs61-docker-version
129+
14
130+
cs61-user@a15e6c4c8dbe:~$ exit
131+
exit
132+
$
133+
```
134+
135+
[Docker]: https://docker.com/
136+
[VMware Workstation]: https://www.vmware.com/products/workstation-player.html
137+
[VMware Fusion]: https://www.vmware.com/products/fusion.html
138+
[VirtualBox]: https://www.virtualbox.org/
139+
[cs61-lectures]: https://github.com/cs61/cs61-lectures/
140+
[cs61-psets]: https://github.com/cs61/cs61-f22-psets/

‎docker/cs61-build-docker

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#! /bin/bash
2+
3+
cd `dirname $0`
4+
5+
arch="`arch`"
6+
tag=
7+
platform=
8+
while test "$#" -ne 0; do
9+
if test "$1" = "-a" -o "$1" = "--arm" -o "$1" = "--arm64"; then
10+
if test "`arch`" = "arm64" -o "`arch`" = "aarch64"; then
11+
platform=linux/arm64
12+
shift
13+
else
14+
echo "\`cs61-build-docker --arm\` only works on ARM64 hosts" 1>&2
15+
exit 1
16+
fi
17+
elif test "$1" = "-x" -o "$1" = "--x86-64" -o "$1" = "--x86_64" -o "$1" = "--amd64"; then
18+
platform=linux/amd64
19+
shift
20+
else
21+
armtext=
22+
if test "`arch`" = "arm64" -o "`arch`" = "aarch64"; then
23+
armtext=" [-a|--arm] [-x|--x86-64]"
24+
fi
25+
echo "Usage: cs61-build-docker$armtext" 1>&2
26+
exit 1
27+
fi
28+
done
29+
if test -z "$platform" -a \( "$arch" = "arm64" -o "$arch" = "aarch64" \); then
30+
platform=linux/arm64
31+
elif test -z "$platform"; then
32+
platform=linux/amd64
33+
fi
34+
if test -z "$tag" -a "$platform" = linux/arm64; then
35+
tag=cs61:arm64
36+
elif test -z "$tag"; then
37+
tag=cs61:latest
38+
fi
39+
40+
if test $platform = linux/arm64; then
41+
exec docker build -t "$tag" -f Dockerfile.arm64 --platform linux/arm64 .
42+
else
43+
exec docker build -t "$tag" -f Dockerfile --platform linux/amd64 .
44+
fi

0 commit comments

Comments
 (0)
Please sign in to comment.