OSDN Git Service

Merge branch 'dev' into alteriso-3-mainline
[alterlinux/alterlinux.git] / system / initcpio / hooks / archiso
1 #!/bin/ash
2
3 # args: source, newroot, mountpoint
4 _mnt_dmsnapshot() {
5     local img="${1}"
6     local newroot="${2}"
7     local mnt="${3}"
8     local img_fullname="${img##*/}";
9     local img_name="${img_fullname%%.*}"
10     local dm_snap_name="${dm_snap_prefix}_${img_name}"
11     local ro_dev ro_dev_size rw_dev
12
13     ro_dev="$(losetup --find --show --read-only -- "${img}")"
14     echo "${ro_dev}" >> /run/archiso/used_block_devices
15     ro_dev_size="$(blockdev --getsz -- "${ro_dev}")"
16
17     if [ "${cow_persistent}" = "P" ]; then
18         if [ -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" ]; then
19             msg ":: Found '/run/archiso/cowspace/${cow_directory}/${img_name}.cow', using as persistent."
20         else
21             msg ":: Creating '/run/archiso/cowspace/${cow_directory}/${img_name}.cow' as persistent."
22             truncate -s "${cow_spacesize}" "/run/archiso/cowspace/${cow_directory}/${img_name}.cow"
23         fi
24     else
25         if [ -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow" ]; then
26             msg ":: Found '/run/archiso/cowspace/${cow_directory}/${img_name}.cow' but non-persistent requested, removing."
27             rm -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow"
28         fi
29         msg ":: Creating '/run/archiso/cowspace/${cow_directory}/${img_name}.cow' as non-persistent."
30         truncate -s "${cow_spacesize}" "/run/archiso/cowspace/${cow_directory}/${img_name}.cow"
31     fi
32
33     rw_dev="$(losetup --find --show "/run/archiso/cowspace/${cow_directory}/${img_name}.cow")"
34     echo "${rw_dev}" >> /run/archiso/used_block_devices
35
36     dmsetup create "${dm_snap_name}" --table \
37         "0 ${ro_dev_size} snapshot ${ro_dev} ${rw_dev} ${cow_persistent} ${cow_chunksize}"
38
39     if [ "${cow_persistent}" != "P" ]; then
40         rm -f "/run/archiso/cowspace/${cow_directory}/${img_name}.cow"
41     fi
42
43     _mnt_dev "/dev/mapper/${dm_snap_name}" "${newroot}${mnt}" "-w" "defaults"
44     readlink -f "/dev/mapper/${dm_snap_name}" >> /run/archiso/used_block_devices
45 }
46
47 # args: source, newroot, mountpoint
48 _mnt_overlayfs() {
49     local src="${1}"
50     local newroot="${2}"
51     local mnt="${3}"
52     mkdir -p "/run/archiso/cowspace/${cow_directory}/upperdir" "/run/archiso/cowspace/${cow_directory}/workdir"
53     mount -t overlay -o \
54     "lowerdir=${src},upperdir=/run/archiso/cowspace/${cow_directory}/upperdir,workdir=/run/archiso/cowspace/${cow_directory}/workdir" \
55     airootfs "${newroot}${mnt}"
56 }
57
58
59 # args: /path/to/image_file, mountpoint
60 _mnt_sfs() {
61     local img="${1}"
62     local mnt="${2}"
63     local img_fullname="${img##*/}"
64     local sfs_dev
65
66     # shellcheck disable=SC2154
67     # defined via initcpio's parse_cmdline()
68     if [ "${copytoram}" = "y" ]; then
69         msg -n ":: Copying squashfs image to RAM..."
70         if ! cp -- "${img}" "/run/archiso/copytoram/${img_fullname}" ; then
71             echo "ERROR: while copy '${img}' to '/run/archiso/copytoram/${img_fullname}'"
72             launch_interactive_shell
73         fi
74         img="/run/archiso/copytoram/${img_fullname}"
75         msg "done."
76     fi
77     sfs_dev="$(losetup --find --show --read-only -- "${img}")"
78     echo "${sfs_dev}" >> /run/archiso/used_block_devices
79     _mnt_dev "${sfs_dev}" "${mnt}" "-r" "defaults"
80 }
81
82 # args: device, mountpoint, flags, opts
83 _mnt_dev() {
84     local dev="${1}"
85     local mnt="${2}"
86     local flg="${3}"
87     local opts="${4}"
88
89     mkdir -p "${mnt}"
90
91     msg ":: Mounting '${dev}' to '${mnt}'"
92
93     while ! poll_device "${dev}" 30; do
94         echo "ERROR: '${dev}' device did not show up after 30 seconds..."
95         echo "   Falling back to interactive prompt"
96         echo "   You can try to fix the problem manually, log out when you are finished"
97         launch_interactive_shell
98     done
99
100     if mount -o "${opts}" "${flg}" "${dev}" "${mnt}"; then
101         msg ":: Device '${dev}' mounted successfully."
102     else
103         echo "ERROR; Failed to mount '${dev}'"
104         echo "   Falling back to interactive prompt"
105         echo "   You can try to fix the problem manually, log out when you are finished"
106         launch_interactive_shell
107     fi
108 }
109
110 _verify_checksum() {
111     local _status
112     cd "/run/archiso/bootmnt/${archisobasedir}/${arch}" || exit 1
113     sha512sum -c airootfs.sha512 > /tmp/checksum.log 2>&1
114     _status=$?
115     cd -- "${OLDPWD}" || exit 1
116     return "${_status}"
117 }
118
119 _verify_signature() {
120     local _status
121     cd "/run/archiso/bootmnt/${archisobasedir}/${arch}" || exit 1
122     gpg --homedir /gpg --status-fd 1 --verify airootfs.sfs.sig 2>/dev/null | grep -qE '^\[GNUPG:\] GOODSIG'
123     _status=$?
124     cd -- "${OLDPWD}" || exit 1
125     return ${_status}
126 }
127
128 run_hook() {
129     [ -z "${arch}" ] && arch="$(uname -m)"
130     [ -z "${copytoram_size}" ] && copytoram_size="75%"
131     [ -z "${archisobasedir}" ] && archisobasedir="arch"
132     [ -z "${dm_snap_prefix}" ] && dm_snap_prefix="arch"
133     # shellcheck disable=SC2154
134     # defined via initcpio's parse_cmdline()
135     [ -z "${archisodevice}" ] && archisodevice="/dev/disk/by-label/${archisolabel}"
136     [ -z "${cow_spacesize}" ] && cow_spacesize="256M"
137     # shellcheck disable=SC2154
138     # defined via initcpio's parse_cmdline()
139     if [ -n "${cow_label}" ]; then
140         cow_device="/dev/disk/by-label/${cow_label}"
141         [ -z "${cow_persistent}" ] && cow_persistent="P"
142     elif [ -n "${cow_device}" ]; then
143         [ -z "${cow_persistent}" ] && cow_persistent="P"
144     else
145         cow_persistent="N"
146     fi
147
148     [ -z "${cow_flags}" ] && cow_flags="defaults"
149     [ -z "${cow_directory}" ] && cow_directory="persistent_${archisolabel}/${arch}"
150     [ -z "${cow_chunksize}" ] && cow_chunksize="8"
151
152     # set mount handler for archiso
153     export mount_handler="archiso_mount_handler"
154 }
155
156 # This function is called normally from init script, but it can be called
157 # as chain from other mount handlers.
158 # args: /path/to/newroot
159 archiso_mount_handler() {
160     local newroot="${1}"
161
162     if ! mountpoint -q "/run/archiso/bootmnt"; then
163         _mnt_dev "${archisodevice}" "/run/archiso/bootmnt" "-r" "defaults"
164         if [ "${copytoram}" != "y" ]; then
165             readlink -f "${archisodevice}" >> /run/archiso/used_block_devices
166         fi
167     fi
168
169     # shellcheck disable=SC2154
170     # defined via initcpio's parse_cmdline()
171     if [ "${checksum}" = "y" ]; then
172         if [ -f "/run/archiso/bootmnt/${archisobasedir}/${arch}/airootfs.sha512" ]; then
173             msg -n ":: Self-test requested, please wait..."
174             if _verify_checksum; then
175                 msg "done. Checksum is OK, continue booting."
176             else
177                 echo "ERROR: one or more files are corrupted"
178                 echo "see /tmp/checksum.log for details"
179                 launch_interactive_shell
180             fi
181         else
182             echo "ERROR: checksum=y option specified but ${archisobasedir}/${arch}/airootfs.sha512 not found"
183             launch_interactive_shell
184         fi
185     fi
186
187     # shellcheck disable=SC2154
188     # defined via initcpio's parse_cmdline()
189     if [ "${verify}" = "y" ]; then
190         if [ -f "/run/archiso/bootmnt/${archisobasedir}/${arch}/airootfs.sfs.sig" ]; then
191             msg -n ":: Signature verification requested, please wait..."
192             if _verify_signature; then
193                 msg "done. Signature is OK, continue booting."
194             else
195                 echo "ERROR: one or more files are corrupted"
196                 launch_interactive_shell
197             fi
198         else
199             echo "ERROR: verify=y option specified but ${archisobasedir}/${arch}/airootfs.sfs.sig not found"
200             launch_interactive_shell
201         fi
202     fi
203
204     if [ "${copytoram}" = "y" ]; then
205         msg ":: Mounting /run/archiso/copytoram (tmpfs) filesystem, size=${copytoram_size}"
206         mkdir -p /run/archiso/copytoram
207         mount -t tmpfs -o "size=${copytoram_size}",mode=0755 copytoram /run/archiso/copytoram
208     fi
209
210     if [ -n "${cow_device}" ]; then
211         _mnt_dev "${cow_device}" "/run/archiso/cowspace" "-r" "${cow_flags}"
212         readlink -f "${cow_device}" >> /run/archiso/used_block_devices
213         mount -o remount,rw "/run/archiso/cowspace"
214     else
215         msg ":: Mounting /run/archiso/cowspace (tmpfs) filesystem, size=${cow_spacesize}..."
216         mkdir -p /run/archiso/cowspace
217         mount -t tmpfs -o "size=${cow_spacesize}",mode=0755 cowspace /run/archiso/cowspace
218     fi
219     mkdir -p "/run/archiso/cowspace/${cow_directory}"
220     chmod 0700 "/run/archiso/cowspace/${cow_directory}"
221
222     _mnt_sfs "/run/archiso/bootmnt/${archisobasedir}/${arch}/airootfs.sfs" "/run/archiso/sfs/airootfs"
223     if [ -f "/run/archiso/sfs/airootfs/airootfs.img" ]; then
224         _mnt_dmsnapshot "/run/archiso/sfs/airootfs/airootfs.img" "${newroot}" "/"
225     else
226         _mnt_overlayfs "/run/archiso/sfs/airootfs" "${newroot}" "/"
227     fi
228
229     if [ "${copytoram}" = "y" ]; then
230         umount -d /run/archiso/bootmnt
231     fi
232 }
233
234 # vim: set ft=sh: