OSDN Git Service

Merge remote-tracking branch 'origin/dev' into alteriso-3-check-mkalteriso
[alterlinux/alterlinux.git] / system / initcpio / hooks / archiso
1 #!/bin/ash
2 #
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 # args: source, newroot, mountpoint
6 _mnt_dmsnapshot() {
7     local img="${1}"
8     local newroot="${2}"
9     local mnt="${3}"
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
14
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}")"
18
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."
22         else
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"
25         fi
26     else
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"
30         fi
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"
33     fi
34
35     rw_dev="$(losetup --find --show "/run/archiso/cowspace/${cow_directory}/${img_name}.cow")"
36     echo "${rw_dev}" >> /run/archiso/used_block_devices
37
38     dmsetup create "${dm_snap_name}" --table \
39         "0 ${ro_dev_size} snapshot ${ro_dev} ${rw_dev} ${cow_persistent} ${cow_chunksize}"
40
41     if [ "${cow_persistent}" != "P" ]; then
42         rm -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow"
43     fi
44
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
47 }
48
49 # args: source, newroot, mountpoint
50 _mnt_overlayfs() {
51     local src="${1}"
52     local newroot="${2}"
53     local mnt="${3}"
54     mkdir -p "/run/archiso/cowspace/${cow_directory}/upperdir" "/run/archiso/cowspace/${cow_directory}/workdir"
55     mount -t overlay -o \
56     "lowerdir=${src},upperdir=/run/archiso/cowspace/${cow_directory}/upperdir,workdir=/run/archiso/cowspace/${cow_directory}/workdir" \
57     airootfs "${newroot}${mnt}"
58 }
59
60
61 # args: /path/to/image_file, mountpoint
62 _mnt_sfs() {
63     local img="${1}"
64     local mnt="${2}"
65     local img_fullname="${img##*/}"
66     local sfs_dev
67
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
75         fi
76         img="/run/archiso/copytoram/${img_fullname}"
77         msg "done."
78     fi
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"
82 }
83
84 # args: device, mountpoint, flags, opts
85 _mnt_dev() {
86     local dev="${1}"
87     local mnt="${2}"
88     local flg="${3}"
89     local opts="${4}"
90
91     mkdir -p "${mnt}"
92
93     msg ":: Mounting '${dev}' to '${mnt}'"
94
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
100     done
101
102     if mount -o "${opts}" "${flg}" "${dev}" "${mnt}"; then
103         msg ":: Device '${dev}' mounted successfully."
104     else
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
109     fi
110 }
111
112 _verify_checksum() {
113     local _status
114     cd "/run/archiso/bootmnt/${archisobasedir}/${arch}" || exit 1
115     sha512sum -c airootfs.sha512 > /tmp/checksum.log 2>&1
116     _status=$?
117     cd -- "${OLDPWD}" || exit 1
118     return "${_status}"
119 }
120
121 _verify_signature() {
122     local _status
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'
125     _status=$?
126     cd -- "${OLDPWD}" || exit 1
127     return ${_status}
128 }
129
130 run_hook() {
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"
146     else
147         cow_persistent="N"
148     fi
149
150     [ -z "${cow_flags}" ] && cow_flags="defaults"
151     [ -z "${cow_directory}" ] && cow_directory="persistent_${archisolabel}/${arch}"
152     [ -z "${cow_chunksize}" ] && cow_chunksize="8"
153
154     # set mount handler for archiso
155     export mount_handler="archiso_mount_handler"
156 }
157
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() {
162     local newroot="${1}"
163
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
168         fi
169     fi
170
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."
178             else
179                 echo "ERROR: one or more files are corrupted"
180                 echo "see /tmp/checksum.log for details"
181                 launch_interactive_shell
182             fi
183         else
184             echo "ERROR: checksum=y option specified but ${archisobasedir}/${arch}/airootfs.sha512 not found"
185             launch_interactive_shell
186         fi
187     fi
188
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."
196             else
197                 echo "ERROR: one or more files are corrupted"
198                 launch_interactive_shell
199             fi
200         else
201             echo "ERROR: verify=y option specified but ${archisobasedir}/${arch}/airootfs.sfs.sig not found"
202             launch_interactive_shell
203         fi
204     fi
205
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
210     fi
211
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"
216     else
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
220     fi
221     mkdir -p "/run/archiso/cowspace/${cow_directory}"
222     chmod 0700 "/run/archiso/cowspace/${cow_directory}"
223
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}" "/"
227     else
228         _mnt_overlayfs "/run/archiso/sfs/airootfs" "${newroot}" "/"
229     fi
230
231     if [ "${copytoram}" = "y" ]; then
232         umount -d /run/archiso/bootmnt
233     fi
234 }
235
236 # vim: set ft=sh: