nfs-debian/install.sh

97 lines
2.3 KiB
Bash
Executable File

#!/bin/bash
# Copy NFS-booted Debian to the local drive and setup boot
set -e
DEV=$1
if [ ! -b $DEV ]; then
echo "USAGE: $0 /dev/sdXXX (sdXXX must be a block device)"
exit 1
fi
PART=${DEV}1
mkdir -p /root/target
# Unmount old chroot-mounts if any
for i in boot/efi dev/pts dev proc sys; do
if grep -q " /root/target/$i " /proc/mounts; then
umount /root/target/$i
fi
done
if grep -q " /root/target " /proc/mounts; then
umount /root/target
fi
# Check if $PART already contains a filesystem
if mount $PART /root/target &>/dev/null; then
umount /root/target
if [ "$WIPE" = "1" ]; then
echo "Device $PART already contains a filesystem. WIPE=1 specified. Wiping"
wipefs -a $DEV
else
echo "Device $PART already contains a filesystem. Wipe it first"
exit 1
fi
fi
# Make 2 partitions: rootfs and EFI System
SIZE=`blockdev --getsz $DEV`
ROOT_SIZE=$((SIZE-2048-1048576))
EFISYSTEM_START=$((SIZE-1048576))
sfdisk $DEV <<EOF
label: gpt
${DEV}1 : start=2048, size=$ROOT_SIZE, type=0FC63DAF-8483-4772-8E79-3D69D8477DE4
${DEV}2 : start=$EFISYSTEM_START, type=C12A7328-F81F-11D2-BA4B-00A0C93EC93B
EOF
mkfs.ext4 -F $PART
mkfs.vfat -F32 ${DEV}2
UUID=`blkid -o value -s UUID $PART`
EFIUUID=`blkid -o value -s PARTUUID ${DEV}2`
# Mount new filesystems
mount $PART /root/target
mkdir -p /root/target/boot/efi
mount ${DEV}2 /root/target/boot/efi
# Copy Debian to the new partition
cp -av /overlay/lower/* /root/target
# Remove overlayroot from initramfs settings
rm /root/target/etc/initramfs-tools/hooks/hooks-overlay
rm /root/target/etc/initramfs-tools/scripts/init-bottom/init-bottom-overlay
>/root/target/etc/initramfs-tools/modules
cat >/root/target/etc/initramfs-tools/initramfs.conf <<EOF
MODULES=most
BUSYBOX=auto
KEYMAP=n
COMPRESS=gzip
DEVICE=
EOF
# Remove ssh-keygen -A
(echo '#!/bin/bash'; echo 'exit 0') > /root/target/etc/rc.local
# Set fstab
cat >/root/target/etc/fstab <<EOF
UUID=$UUID / ext4 rw,relatime 0 1
PARTUUID=$EFIUUID /boot/efi vfat defaults 0 2
EOF
# Install GRUB
for i in dev dev/pts proc sys; do mount --bind /$i /root/target/$i; done
chroot /root/target <<EOF
ssh-keygen -A
update-initramfs -u -k all
update-grub
grub-install --target=x86_64-efi $DEV
EOF
for i in dev/pts dev proc sys; do umount /root/target/$i; done
# Unmount filesystems
umount /root/target/boot/efi
umount /root/target