3 # SPDX-License-Identifier: GPL-3.0-or-later
5 # args: source, newroot, mountpoint
10 local img_fullname="${img##*/}";
11 local img_name="${img_fullname%%.*}"
12 local dm_snap_name="${dm_snap_prefix}_${img_name}"
13 local ro_dev ro_dev_size rw_dev
15 ro_dev="$(losetup --find --show --read-only -- "${img}")"
16 echo "${ro_dev}" >> /run/archiso/used_block_devices
17 ro_dev_size="$(blockdev --getsz "${ro_dev}")"
19 if [ "${cow_persistent}" = "P" ]; then
20 if [ -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" ]; then
21 msg ":: Found '/run/archiso/cowspace/${cow_directory}/${img_name}.cow', using as persistent."
23 msg ":: Creating '/run/archiso/cowspace/${cow_directory}/${img_name}.cow' as persistent."
24 truncate -s "${cow_spacesize}" "/run/archiso/cowspace/${cow_directory}/${img_name}.cow"
27 if [ -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" ]; then
28 msg ":: Found '/run/archiso/cowspace/${cow_directory}/${img_name}.cow' but non-persistent requested, removing."
29 rm -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow"
31 msg ":: Creating '/run/archiso/cowspace/${cow_directory}/${img_name}.cow' as non-persistent."
32 truncate -s "${cow_spacesize}" "/run/archiso/cowspace/${cow_directory}/${img_name}.cow"
35 rw_dev="$(losetup --find --show "/run/archiso/cowspace/${cow_directory}/${img_name}.cow")"
36 echo "${rw_dev}" >> /run/archiso/used_block_devices
38 dmsetup create "${dm_snap_name}" --table \
39 "0 ${ro_dev_size} snapshot ${ro_dev} ${rw_dev} ${cow_persistent} ${cow_chunksize}"
41 if [ "${cow_persistent}" != "P" ]; then
42 rm -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow"
45 _mnt_dev "/dev/mapper/${dm_snap_name}" "${newroot}${mnt}" "-w" "defaults"
46 readlink -f "/dev/mapper/${dm_snap_name}" >> /run/archiso/used_block_devices
49 # args: source, newroot, mountpoint
54 mkdir -p "/run/archiso/cowspace/${cow_directory}/upperdir" "/run/archiso/cowspace/${cow_directory}/workdir"
56 "lowerdir=${src},upperdir=/run/archiso/cowspace/${cow_directory}/upperdir,workdir=/run/archiso/cowspace/${cow_directory}/workdir" \
57 airootfs "${newroot}${mnt}"
61 # args: /path/to/image_file, mountpoint
65 local img_fullname="${img##*/}"
68 # shellcheck disable=SC2154
69 # defined via initcpio's parse_cmdline()
70 if [ "${copytoram}" = "y" ]; then
71 msg -n ":: Copying squashfs image to RAM..."
72 if ! cp -- "${img}" "/run/archiso/copytoram/${img_fullname}" ; then
73 echo "ERROR: while copy '${img}' to '/run/archiso/copytoram/${img_fullname}'"
74 launch_interactive_shell
76 img="/run/archiso/copytoram/${img_fullname}"
79 sfs_dev="$(losetup --find --show --read-only -- "${img}")"
80 echo "${sfs_dev}" >> /run/archiso/used_block_devices
81 _mnt_dev "${sfs_dev}" "${mnt}" "-r" "defaults"
84 # args: device, mountpoint, flags, opts
93 msg ":: Mounting '${dev}' to '${mnt}'"
95 while ! poll_device "${dev}" 30; do
96 echo "ERROR: '${dev}' device did not show up after 30 seconds..."
97 echo " Falling back to interactive prompt"
98 echo " You can try to fix the problem manually, log out when you are finished"
99 launch_interactive_shell
102 if mount -o "${opts}" "${flg}" "${dev}" "${mnt}"; then
103 msg ":: Device '${dev}' mounted successfully."
105 echo "ERROR; Failed to mount '${dev}'"
106 echo " Falling back to interactive prompt"
107 echo " You can try to fix the problem manually, log out when you are finished"
108 launch_interactive_shell
114 cd "/run/archiso/bootmnt/${archisobasedir}/${arch}" || exit 1
115 sha512sum -c airootfs.sha512 > /tmp/checksum.log 2>&1
117 cd -- "${OLDPWD}" || exit 1
121 _verify_signature() {
123 cd "/run/archiso/bootmnt/${archisobasedir}/${arch}" || exit 1
124 gpg --homedir /gpg --status-fd 1 --verify airootfs.sfs.sig 2>/dev/null | grep -qE '^\[GNUPG:\] GOODSIG'
126 cd -- "${OLDPWD}" || exit 1
131 [ -z "${arch}" ] && arch="$(uname -m)"
132 [ -z "${copytoram_size}" ] && copytoram_size="75%"
133 [ -z "${archisobasedir}" ] && archisobasedir="arch"
134 [ -z "${dm_snap_prefix}" ] && dm_snap_prefix="arch"
135 # shellcheck disable=SC2154
136 # defined via initcpio's parse_cmdline()
137 [ -z "${archisodevice}" ] && archisodevice="/dev/disk/by-label/${archisolabel}"
138 [ -z "${cow_spacesize}" ] && cow_spacesize="256M"
139 # shellcheck disable=SC2154
140 # defined via initcpio's parse_cmdline()
141 if [ -n "${cow_label}" ]; then
142 cow_device="/dev/disk/by-label/${cow_label}"
143 [ -z "${cow_persistent}" ] && cow_persistent="P"
144 elif [ -n "${cow_device}" ]; then
145 [ -z "${cow_persistent}" ] && cow_persistent="P"
150 [ -z "${cow_flags}" ] && cow_flags="defaults"
151 [ -z "${cow_directory}" ] && cow_directory="persistent_${archisolabel}/${arch}"
152 [ -z "${cow_chunksize}" ] && cow_chunksize="8"
154 # set mount handler for archiso
155 export mount_handler="archiso_mount_handler"
158 # This function is called normally from init script, but it can be called
159 # as chain from other mount handlers.
160 # args: /path/to/newroot
161 archiso_mount_handler() {
164 if ! mountpoint -q "/run/archiso/bootmnt"; then
165 _mnt_dev "${archisodevice}" "/run/archiso/bootmnt" "-r" "defaults"
166 if [ "${copytoram}" != "y" ]; then
167 readlink -f "${archisodevice}" >> /run/archiso/used_block_devices
171 # shellcheck disable=SC2154
172 # defined via initcpio's parse_cmdline()
173 if [ "${checksum}" = "y" ]; then
174 if [ -f "/run/archiso/bootmnt/${archisobasedir}/${arch}/airootfs.sha512" ]; then
175 msg -n ":: Self-test requested, please wait..."
176 if _verify_checksum; then
177 msg "done. Checksum is OK, continue booting."
179 echo "ERROR: one or more files are corrupted"
180 echo "see /tmp/checksum.log for details"
181 launch_interactive_shell
184 echo "ERROR: checksum=y option specified but ${archisobasedir}/${arch}/airootfs.sha512 not found"
185 launch_interactive_shell
189 # shellcheck disable=SC2154
190 # defined via initcpio's parse_cmdline()
191 if [ "${verify}" = "y" ]; then
192 if [ -f "/run/archiso/bootmnt/${archisobasedir}/${arch}/airootfs.sfs.sig" ]; then
193 msg -n ":: Signature verification requested, please wait..."
194 if _verify_signature; then
195 msg "done. Signature is OK, continue booting."
197 echo "ERROR: one or more files are corrupted"
198 launch_interactive_shell
201 echo "ERROR: verify=y option specified but ${archisobasedir}/${arch}/airootfs.sfs.sig not found"
202 launch_interactive_shell
206 if [ "${copytoram}" = "y" ]; then
207 msg ":: Mounting /run/archiso/copytoram (tmpfs) filesystem, size=${copytoram_size}"
208 mkdir -p /run/archiso/copytoram
209 mount -t tmpfs -o "size=${copytoram_size}",mode=0755 copytoram /run/archiso/copytoram
212 if [ -n "${cow_device}" ]; then
213 _mnt_dev "${cow_device}" "/run/archiso/cowspace" "-r" "${cow_flags}"
214 readlink -f "${cow_device}" >> /run/archiso/used_block_devices
215 mount -o remount,rw "/run/archiso/cowspace"
217 msg ":: Mounting /run/archiso/cowspace (tmpfs) filesystem, size=${cow_spacesize}..."
218 mkdir -p /run/archiso/cowspace
219 mount -t tmpfs -o "size=${cow_spacesize}",mode=0755 cowspace /run/archiso/cowspace
221 mkdir -p "/run/archiso/cowspace/${cow_directory}"
222 chmod 0700 "/run/archiso/cowspace/${cow_directory}"
224 _mnt_sfs "/run/archiso/bootmnt/${archisobasedir}/${arch}/airootfs.sfs" "/run/archiso/sfs/airootfs"
225 if [ -f "/run/archiso/sfs/airootfs/airootfs.img" ]; then
226 _mnt_dmsnapshot "/run/archiso/sfs/airootfs/airootfs.img" "${newroot}" "/"
228 _mnt_overlayfs "/run/archiso/sfs/airootfs" "${newroot}" "/"
231 if [ "${copytoram}" = "y" ]; then
232 umount -d /run/archiso/bootmnt