2019-11-02 19:34:54 +03:00
|
|
|
#!/bin/sh
|
2019-06-04 15:16:30 +03:00
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
die() {
|
2019-11-02 19:34:54 +03:00
|
|
|
echo "die: $*"
|
2019-06-04 15:16:30 +03:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2019-11-02 19:34:54 +03:00
|
|
|
if [ "$(id -u)" != 0 ]; then
|
2019-06-04 15:16:30 +03:00
|
|
|
die "this script needs to run as root"
|
|
|
|
fi
|
2020-01-02 05:06:14 +03:00
|
|
|
if [ "$(uname -s)" = "Darwin" ]; then
|
2019-12-25 22:47:43 +03:00
|
|
|
export PATH="/usr/local/opt/e2fsprogs/bin:$PATH"
|
|
|
|
export PATH="/usr/local/opt/e2fsprogs/sbin:$PATH"
|
|
|
|
fi
|
2019-06-04 15:16:30 +03:00
|
|
|
echo "setting up disk image..."
|
2019-11-26 14:54:15 +03:00
|
|
|
qemu-img create _disk_image "${DISK_SIZE:-600}"m || die "couldn't create disk image"
|
2020-01-15 23:45:39 +03:00
|
|
|
chown "$SUDO_UID":"$SUDO_GID" _disk_image || die "couldn't adjust permissions on disk image"
|
2019-06-04 15:16:30 +03:00
|
|
|
echo "done"
|
|
|
|
|
2019-11-02 19:34:54 +03:00
|
|
|
printf "creating new filesystem... "
|
2020-01-02 05:06:14 +03:00
|
|
|
if [ "$(uname -s)" = "OpenBSD" ]; then
|
|
|
|
VND=`vnconfig _disk_image`
|
|
|
|
(echo "e 0"; echo 83; echo n; echo 0; echo "*"; echo "quit") | fdisk -e $VND
|
|
|
|
mkfs.ext2 -I 128 -F /dev/${VND}i || die "couldn't create filesystem"
|
|
|
|
else
|
2020-01-22 23:58:43 +03:00
|
|
|
if [ -x /sbin/mke2fs ]; then
|
|
|
|
/sbin/mke2fs -q -I 128 _disk_image || die "couldn't create filesystem"
|
|
|
|
else
|
|
|
|
mke2fs -q -I 128 _disk_image || die "couldn't create filesystem"
|
|
|
|
fi
|
2020-01-02 05:06:14 +03:00
|
|
|
fi
|
2019-06-04 15:16:30 +03:00
|
|
|
echo "done"
|
|
|
|
|
2019-11-02 19:34:54 +03:00
|
|
|
printf "mounting filesystem... "
|
2019-06-04 15:16:30 +03:00
|
|
|
mkdir -p mnt
|
2020-01-02 05:06:14 +03:00
|
|
|
if [ "$(uname -s)" = "Darwin" ]; then
|
2019-12-25 22:47:43 +03:00
|
|
|
fuse-ext2 _disk_image mnt -o rw+,allow_other,uid=501,gid=20 || die "couldn't mount filesystem"
|
2020-01-02 05:06:14 +03:00
|
|
|
elif [ "$(uname -s)" = "OpenBSD" ]; then
|
|
|
|
mount -t ext2fs /dev/${VND}i mnt/ || die "couldn't mount filesystem"
|
2019-12-25 22:47:43 +03:00
|
|
|
else
|
|
|
|
mount _disk_image mnt/ || die "couldn't mount filesystem"
|
|
|
|
fi
|
2019-06-04 15:16:30 +03:00
|
|
|
echo "done"
|
|
|
|
|
|
|
|
cleanup() {
|
|
|
|
if [ -d mnt ]; then
|
2019-11-02 19:34:54 +03:00
|
|
|
printf "unmounting filesystem... "
|
2019-06-04 15:16:30 +03:00
|
|
|
umount mnt || ( sleep 1 && sync && umount mnt )
|
|
|
|
rm -rf mnt
|
2020-01-02 05:06:14 +03:00
|
|
|
if [ "$(uname -s)" = "OpenBSD" ]; then
|
|
|
|
vnconfig -u $VND
|
|
|
|
fi
|
2019-06-04 15:16:30 +03:00
|
|
|
echo "done"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
trap cleanup EXIT
|
|
|
|
|
|
|
|
./build-root-filesystem.sh
|