Skip to content

Commit

Permalink
make separate home partition optional
Browse files Browse the repository at this point in the history
  • Loading branch information
brettaufheber committed Dec 31, 2023
1 parent 71c3e4e commit 14e83af
Showing 1 changed file with 63 additions and 28 deletions.
91 changes: 63 additions & 28 deletions ubuntu-installer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ function main {
SHOW_HELP=false
SHELL_LOGIN=false
USE_EFI=false
USE_SEPARATE_HOME=false

#define long options
LONG_OPTIONS='help,login,efi'
LONG_OPTIONS='help,login,efi,separate-home'
LONG_OPTIONS="$LONG_OPTIONS"',username:,hostname:,codename:,bundles:,dev-root:,dev-home:,dev-boot:'
LONG_OPTIONS="$LONG_OPTIONS"',mirror:,locales:,time-zone:,user-gecos:,password:'
LONG_OPTIONS="$LONG_OPTIONS"',keyboard-model:,keyboard-layout:,keyboard-variant:,keyboard-options:'

# parse arguments
OPTIONS_PARSED=$(
getopt \
--options 'hleu:n:c:b:x:y:z:' \
--options 'hlesu:n:c:b:x:y:z:' \
--longoptions "$LONG_OPTIONS" \
--name "$SELF_NAME" \
-- "$@"
Expand All @@ -49,6 +50,10 @@ function main {
USE_EFI=true
shift 1
;;
-s | --separate-home)
USE_SEPARATE_HOME=true
shift 1
;;
-u | --username)
USERNAME_NEW="$2"
shift 2
Expand Down Expand Up @@ -335,11 +340,16 @@ function verify_mounting_root {

function verify_mounting_home {

# the block device file for "/home" must exist
if [[ -z "${DEV_HOME:-}" ]] || [[ ! -b "${DEV_HOME:-}" ]]; then
# only perform checks if a separate home partition is used
if "$USE_SEPARATE_HOME"; then

echo "$SELF_NAME: require device file for /home" >&2
exit 1
# the block device file for "/home" must exist
if [[ -z "${DEV_HOME:-}" ]] || [[ ! -b "${DEV_HOME:-}" ]]; then

echo "$SELF_NAME: require device file for /home" >&2
exit 1

fi

fi
}
Expand Down Expand Up @@ -873,7 +883,7 @@ function task_install_system {
# format $DEV_ROOT
mkfs.ext4 "$DEV_ROOT"

# mount "/" and "/home"
# mount root partition
mounting_step_1

# execute debootstrap
Expand Down Expand Up @@ -1346,9 +1356,8 @@ function configure_fstab {
# set path for output file
FILE="$CHROOT/etc/fstab"

# get UUID of system and home partition
# get UUID of system partition
UUID_ROOT="$(blkid -s UUID -o value "$DEV_ROOT")"
UUID_HOME="$(blkid -s UUID -o value "$DEV_HOME")"

if "$USE_EFI"; then

Expand All @@ -1368,12 +1377,30 @@ function configure_fstab {

fi

if "$USE_SEPARATE_HOME"; then

# get UUID of home partition
UUID_HOME="$(blkid -s UUID -o value "$DEV_HOME")"

# allows to write home specific entry to fstab file
FILE_HOME="$FILE"

else

# an UUID is not needed in this case
UUID_HOME=""

# discard home specific entry
FILE_HOME="/dev/null"

fi

# edit /etc/fstab
echo '# /etc/fstab' > "$FILE"
echo '# <file system> <mount point> <type> <options> <dump> <pass>' >> "$FILE"
echo "UUID=$UUID_ROOT / ext4 defaults,errors=remount-ro 0 1" >> "$FILE"
echo "UUID=$UUID_UEFI /boot/efi vfat defaults 0 2" >> "$FILE_UEFI"
echo "UUID=$UUID_HOME /home ext4 defaults 0 2" >> "$FILE"
echo "UUID=$UUID_HOME /home ext4 defaults 0 2" >> "$FILE_HOME"
echo "proc /proc proc defaults 0 0" >> "$FILE"
echo "sys /sys sysfs defaults 0 0" >> "$FILE"
echo "tmpfs /tmp tmpfs defaults,size=40% 0 0" >> "$FILE"
Expand Down Expand Up @@ -1672,26 +1699,10 @@ function mounting_step_1 {

# set path to mounting point
CHROOT="/mnt/ubuntu-$(cat '/proc/sys/kernel/random/uuid')"
CHHOME="$CHROOT/home"

# mount $DEV_ROOT
mkdir -p "$CHROOT"
mount "$DEV_ROOT" "$CHROOT"

# mount $DEV_HOME
if mount | grep -q "$DEV_HOME"; then

HOME_PATH="$(df "$DEV_HOME" | grep -oE '(/[[:alnum:]]+)+$' | head -1)"

mkdir -p "$CHHOME"
mount -o bind "$HOME_PATH" "$CHHOME"

else

mkdir -p "$CHHOME"
mount "$DEV_HOME" "$CHHOME"

fi
}

function unmounting_step_1 {
Expand All @@ -1703,8 +1714,7 @@ function unmounting_step_1 {
stop_chroot_processes
sync

# unmount home directory and directory root
umount "$CHHOME"
# unmount directory root
umount "$CHROOT"
rmdir "$CHROOT"

Expand Down Expand Up @@ -1748,6 +1758,25 @@ function mounting_step_2 {
fi

fi

if "$USE_SEPARATE_HOME"; then

# mount $DEV_HOME
if mount | grep -q "$DEV_HOME"; then

HOME_PATH="$(df "$DEV_HOME" | grep -oE '(/[[:alnum:]]+)+$' | head -1)"

mkdir -p "$CHROOT/home"
mount -o bind "$HOME_PATH" "$CHROOT/home"

else

mkdir -p "$CHROOT/home"
mount "$DEV_HOME" "$CHROOT/home"

fi

fi
}

function unmounting_step_2 {
Expand All @@ -1773,6 +1802,12 @@ function unmounting_step_2 {

fi

if "$USE_SEPARATE_HOME"; then

umount "$CHROOT/home"

fi

fi
}

Expand Down

0 comments on commit 14e83af

Please sign in to comment.