Skip to content

Commit

Permalink
Add support for local SSDs
Browse files Browse the repository at this point in the history
Add a systemd service which, on boot, partitions local SSD and
installs Rustup and Cargo on it for NayDuck to use.  Since the disk is
quite a bit larger than what we need, use part of it for swap which
should help with Docker tests sometimes failing.
  • Loading branch information
mina86 committed Aug 1, 2022
1 parent d0958d3 commit 0b2d8da
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 9 deletions.
6 changes: 3 additions & 3 deletions automation/setup-host.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ set -x

apt-get -y update
apt-get -y upgrade
apt-get -y install git python3-pip libpq-dev lld libclang-dev
apt-get -y install fdisk git python3-pip libpq-dev lld libclang-dev

grep -q ^nayduck: /etc/passwd ||
adduser --disabled-login --gecos NayDuck nayduck
Expand Down Expand Up @@ -89,8 +89,8 @@ else
fi

case $type in
frontend) services=ui ;;
worker) services='worker fuzzer' ;;
frontend) services=ui ;;
worker) services='local-ssd worker fuzzer' ;;
*) services=$type
esac
for service in $services; do
Expand Down
2 changes: 1 addition & 1 deletion systemd/nayduck-fuzzer.service
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[Unit]
Description=NayDuck Fuzzer
After=network.target
After=network.target local-ssd.service

[Service]
Restart=always
Expand Down
8 changes: 8 additions & 0 deletions systemd/nayduck-local-ssd.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/home/nayduck/nayduck
ExecStart=/bin/sh systemd/setup-local-ssd.sh

[Install]
WantedBy=multi-user.target
2 changes: 1 addition & 1 deletion systemd/nayduck-worker.service
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[Unit]
Description=NayDuck Worker
After=network.target nayduck-fuzzer.service
After=network.target nayduck-fuzzer.service local-ssd.service

[Service]
Restart=always
Expand Down
57 changes: 57 additions & 0 deletions systemd/setup-local-ssd.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/bin/sh

set -eux

dev=/dev/nvme0n1
dir=/datadrive

# Sanity checks
if ! [ -e "$dev" ]; then
echo "$dev: device does not exist; nothing to do"
exit 0
fi

if ! [ -e "${dev}p1" ]; then
# Partition the disk:
# - p1 → data partition, will become /datadrive
# - p2 → swap partition
sfdisk "$dev" <<EOF
size=360G
type=swap
write
EOF

# Data on the device is essentially lost on reboot and we’re treating it as
# ephemeral. There’s no point in keeping journal.
mkfs.ext4 -E discard -O ^has_journal "${dev}p1"
mkdir -p "$dir"
mount "${dev}p1" "$dir" -o discard,noatime,nobarrier
chown nayduck:nayduck "$dir"
else
mount "${dev}p1" "$dir" -o discard,noatime,nobarrier
fi

# Enable swap on the second partition
mkswap "${dev}p2"
swapon "${dev}p2"

if ! [ -f "$dir/README" ]; then
cat >$dir/README <<EOF
Data in this directory is ephemeral and may disappear on reboots.
Don’t keep any data which cannot be recreated here.
EOF
chmod 644 "$dir/README"
fi

if ! [ -d "$dir/home" ]; then
# Install Rustup and Cargo in /datadrive/home for NayDuck to use. Those always
# take a lot of space so best keep them on our massive local SSD.
mkdir -p "$dir/home"
chown nayduck:nayduck "$dir/home"
sudo -u nayduck CARGO_HOME="$dir/home/cargo" RUSTUP_HOME="$dir/home/rustup" '
set -eux
curl https://sh.rustup.rs -sSf | sh -s -- -y
"$CARGO_HOME/bin/rustup" target add wasm32-unknown-unknown
"$CARGO_HOME/bin/cargo" install cargo-fuzz
'
fi
15 changes: 11 additions & 4 deletions workers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,21 +278,28 @@ def int_to_ip(addr: int) -> str:

def setup_environ() -> None:
"""Configures environment variables for workers and builders."""
# Look for Cargo.
path = WORKDIR / 'home/cargo'
if (path / 'bin/cargo').is_file():
os.environb[b'CARGO_HOME'] = os.fsencode(path)
os.environb[b'RUSTUP_HOME'] = os.fsencode(WORKDIR / 'home/rustup')
cargo_bin = path / 'bin'
else:
cargo_bin = pathlib.Path.home() / '.cargo/bin'

# Add Cargo to PATH and remove various unnecessary directories
pathsep = os.fsencode(os.pathsep)
paths = os.environb.get(b'PATH', os.fsencode(os.defpath)).split(pathsep)
paths = [
path for path in paths
if path.startswith(b'/') and not path.endswith(b'/sbin')
]

cargo_bin = pathlib.Path.home() / '.cargo/bin'
if cargo_bin.exists():
paths.insert(0, os.fsencode(cargo_bin))

os.environb[b'PATH'] = pathsep.join(paths)

# Configure Cargo builds
# Configure Cargo builds to make compilation faster. We don’t need
# super-optimised builds thus disabling LTO and using lld.
os.environb[b'CARGO_PROFILE_RELEASE_LTO'] = b'false'
os.environb[b'CARGO_PROFILE_DEV_DEBUG'] = b'0'
os.environb[b'CARGO_PROFILE_TEST_DEBUG'] = b'0'
Expand Down

0 comments on commit 0b2d8da

Please sign in to comment.