aboutsummarylogtreecommitdiffstats
path: root/refindplus-install
blob: 56e0d641799b494b72210e2071b4674c7096bff7 (plain)
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
#!/usr/bin/env bash
set -euo pipefail

source_dir=${REFINDPLUS_SOURCE_DIR:-/usr/share/refindplus}
config_source=${REFINDPLUS_CONFIG:-${source_dir}/config.conf}
esp=''
target='refindplus'
fallback=0
register=0
label='RefindPlus'
dry_run=0

usage() {
  cat <<'EOF'
Usage: refindplus-install --esp PATH [options]

Options:
  --esp PATH       Mounted EFI system partition, such as /boot or /efi.
  --target NAME    Directory under EFI/ for normal installs. Default: refindplus.
  --fallback       Install as EFI/BOOT/BOOTX64.EFI for fallback/removable booting.
  --register       Add a UEFI boot entry with efibootmgr. Not used with --fallback.
  --label TEXT     Boot entry label for --register. Default: RefindPlus.
  --dry-run        Print actions without copying files.
  -h, --help       Show this help.

The installer copies the complete package tree: config, icons, fonts, banners,
keys, tools, and x64 filesystem drivers. An existing ESP config.conf is
preserved; the packaged config is copied as config.conf-sample instead.
EOF
}

die() {
  printf 'refindplus-install: %s\n' "$*" >&2
  exit 1
}

run() {
  printf '%s\n' "$*"
  if (( ! dry_run )); then
    "$@"
  fi
}

copy_dir_contents() {
  local from=$1
  local to=$2

  [[ -d $from ]] || return 0
  run mkdir -p "$to"
  run cp -R "${from}/." "$to/"
}

efi_loader_path() {
  local path=$1
  path=${path#/}
  path=${path//\//\\}
  printf '\\%s' "$path"
}

register_boot_entry() {
  local esp_path=$1
  local loader_path=$2
  local part_source
  local parent
  local part_num

  command -v efibootmgr >/dev/null 2>&1 || die 'efibootmgr is required for --register'
  command -v findmnt >/dev/null 2>&1 || die 'findmnt is required for --register'
  command -v lsblk >/dev/null 2>&1 || die 'lsblk is required for --register'

  part_source=$(findmnt -nro SOURCE --target "$esp_path") || die "could not identify mount source for ${esp_path}"
  parent=$(lsblk -nro PKNAME "$part_source" | head -n1)
  part_num=$(lsblk -nro PARTN "$part_source" | head -n1)

  [[ -n $parent && -n $part_num ]] || die "could not derive disk and partition from ${part_source}"
  run efibootmgr -c -d "/dev/${parent}" -p "$part_num" -L "$label" -l "$(efi_loader_path "$loader_path")"
}

while (($#)); do
  case "$1" in
    --esp)
      [[ $# -ge 2 ]] || die '--esp requires a path'
      esp=$2
      shift 2
      ;;
    --target)
      [[ $# -ge 2 ]] || die '--target requires a name'
      target=$2
      shift 2
      ;;
    --fallback)
      fallback=1
      shift
      ;;
    --register)
      register=1
      shift
      ;;
    --label)
      [[ $# -ge 2 ]] || die '--label requires text'
      label=$2
      shift 2
      ;;
    --dry-run)
      dry_run=1
      shift
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      die "unknown option: $1"
      ;;
  esac
done

[[ -n $esp ]] || die 'missing --esp PATH'
[[ -d $source_dir ]] || die "package tree not found: ${source_dir}"
[[ -d $esp ]] || die "ESP path does not exist: ${esp}"
[[ $target != *'/'* && $target != *'\\'* && -n $target ]] || die '--target must be a single directory name'

if (( fallback )); then
  dest="${esp%/}/EFI/BOOT"
  loader_name='BOOTX64.EFI'
  loader_rel='EFI/BOOT/BOOTX64.EFI'
  (( ! register )) || die '--register is not used with --fallback'
else
  dest="${esp%/}/EFI/${target}"
  loader_name='refind_x64.efi'
  loader_rel="EFI/${target}/refind_x64.efi"
fi

run mkdir -p "$dest"
run install -m0644 "${source_dir}/x64_RefindPlus_REL.efi" "${dest}/${loader_name}"
run install -m0644 "${source_dir}/x64_RefindPlus_DBG.efi" "${dest}/x64_RefindPlus_DBG.efi"

if [[ -e ${dest}/config.conf ]]; then
  run install -m0644 "${source_dir}/config.conf-sample" "${dest}/config.conf-sample"
else
  [[ -f $config_source ]] || config_source="${source_dir}/config.conf"
  run install -m0644 "$config_source" "${dest}/config.conf"
fi

run install -m0644 "${source_dir}/config.conf-sample-Dev" "${dest}/config.conf-sample-Dev"
copy_dir_contents "${source_dir}/icons" "${dest}/icons"
copy_dir_contents "${source_dir}/fonts" "${dest}/fonts"
copy_dir_contents "${source_dir}/banners" "${dest}/banners"
copy_dir_contents "${source_dir}/keys" "${dest}/keys"
copy_dir_contents "${source_dir}/drivers_x64" "${dest}/drivers_x64"
copy_dir_contents "${source_dir}/tools" "${dest}/tools"

if (( register )); then
  register_boot_entry "$esp" "$loader_rel"
fi

printf 'Installed RefindPlus to %s\n' "$dest"