forked from microsoft/WSL-DistroLauncher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_rootfs.sh
168 lines (135 loc) · 4.24 KB
/
get_rootfs.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash
set -e
if ! [ $(id -u) = 0 ]; then
echo "The script need to be run as root." >&2
exit 1
fi
if [ $SUDO_USER ]; then
real_user=$SUDO_USER
else
real_user=$(whoami)
fi
ARCH="x86_64"
VERSION="2020.08.01"
IMAGE="archlinux-bootstrap-$VERSION-$ARCH.tar.gz"
MIRROR="http://mirror.rackspace.com/archlinux/iso/$VERSION/$IMAGE"
WSL=2
FAKEROOT="https://aur.archlinux.org/cgit/aur.git/snapshot/fakeroot-tcp.tar.gz"
DAEMONIZE="https://aur.archlinux.org/cgit/aur.git/snapshot/daemonize.tar.gz"
DJINN="https://github.com/zacharyrs/djinn/releases/download/v0.2.1/djinn"
# Get the current directory
BUILDDIR=$(cd `dirname $0` && pwd -P)
# Prepare fakeroot-tcp as the normal (non-root) user
if [ $WSL == 1 ]; then
sudo -u $real_user sh <<EOF
set -e
# Get somewhere in tmp to work (as non-root)
cd \$(mktemp -d)
# Note you'll need the dependencies for this...
curl -LO "${FAKEROOT}"
tar xzf "fakeroot-tcp.tar.gz"
cd fakeroot-tcp
makepkg
cp "\$(pwd)/\$(ls | grep pkg.)" "$BUILDDIR/fakeroot.pkg.gz"
EOF
else
# Prepare daemonize as the normal (non-root) user
sudo -u $real_user sh <<EOF
set -e
# Get somewhere in tmp to work (as non-root)
WORKDIR=\$(mktemp -d)
cd \$WORKDIR
# Note you'll need the dependencies for this...
curl -LO "$DAEMONIZE"
tar xzf "daemonize.tar.gz"
cd daemonize
makepkg
cp "\$(pwd)/\$(ls | grep pkg.)" "$BUILDDIR/daemonize.pkg.gz"
rm -rf \$WORKIR
EOF
fi
# Get a new working directory (as root)
WORKDIR=$(mktemp -d)
cd $WORKDIR
# Get the base image
curl -LO $MIRROR
tar xzf $IMAGE
# Stop us breaking things!
mount --bind root.$ARCH root.$ARCH
cd root.$ARCH
# Set up internet and pacman configs...
cp -r $BUILDDIR/linux_files/etc/* etc/
sed -i -e 's/#en_US.UTF-8/en_US.UTF-8/' etc/locale.gen
echo "LANG=en_US.UTF-8" > etc/locale.conf
echo "LANGUAGE=en_US.UTF-8" >> etc/locale.conf
echo "LC_ALL=en_US.UTF-8" >> etc/locale.conf
if [ $WSL == 1 ]; then
# Copy over the fakeroot package
cp "$BUILDDIR/fakeroot.pkg.gz" root/
else
# Copy over the daemonize package
cp "$BUILDDIR/daemonize.pkg.gz" root/
fi
# ...Initialise pacman
arch-chroot . pacman-key --init
arch-chroot . pacman-key --populate archlinux
# Fix localization
arch-chroot . pacman -Sy --noconfirm sed gzip
arch-chroot . locale-gen
# Update the install, and add stuff!
arch-chroot . pacman -Syu --noconfirm
arch-chroot . pacman -S --noconfirm base base-devel sudo ccache clang pigz pbzip2 git wget
# Now remove and disable the linux kernel install
if pacman -Qi linux-firmware; then
arch-chroot . pacman -Rsc --noconfirm linux-firmware
fi
sed -i -e 's/#IgnorePkg =/IgnorePkg =/' etc/pacman.conf
rm etc/pacman.d/hooks/90-linux.hook
if [ $WSL == 1 ]; then
yes | arch-chroot . pacman -U /root/fakeroot.pkg.gz
else
arch-chroot . pacman -S --noconfirm polkit
yes | arch-chroot . pacman -U /root/daemonize.pkg.gz
curl -Lo usr/local/bin/djinn $DJINN
arch-chroot . chmod u+s /usr/local/bin/djinn
arch-chroot . chmod a+rx /usr/local/bin/
mkdir -p usr/local/lib/systemd/system-environment-generators/
cat > usr/local/lib/systemd/system-environment-generators/10-djinn.sh << EOF
#!/bin/sh
if [ -e /run/djinn.env ]
then
cat /run/djinn.env
fi
EOF
arch-chroot . chmod a+rx /usr/local/lib/systemd/system-environment-generators/10-djinn.sh
fi
# wsl recommendations
arch-chroot . systemctl disable getty@tty1
# And make the final touches!
rm etc/resolv.conf
cat > etc/resolv.conf << EOF
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
EOF
sed -i -e "s/^# %wheel ALL=.*NOPASSWD:.*$/%wheel ALL=(ALL) NOPASSWD: ALL/" etc/sudoers
# Copy in user overrides
[ -d "$BUILDDIR/linux_overrides/etc/" ] && cp -r $BUILDDIR/linux_overrides/etc/* etc/
# Run the user-overrides script if it exists...
if [ -f "$BUILDDIR/linux_overrides/runme.sh" ]; then
cp -r $BUILDDIR/linux_overrides/runme.sh root/
chmod +x root/runme.sh
arch-chroot . /root/runme.sh
fi
# Finish install
yes | arch-chroot . pacman -Scc
# Now to tidy up...
umount -l .
/bin/rm -rf tmp/*
/bin/rm -rf root/*
/bin/rm -rf var/cache/pacman/pkg/*
# tar the rootfs, and drop it in the build directory
tar --ignore-failed-read -czf $BUILDDIR/install.tar.gz *
chown $real_user:$real_user $BUILDDIR/install.tar.gz
# delete the working directory
rm -rf $WORKDIR