summaryrefslogtreecommitdiffstats
path: root/bin/sbuild-qemu-create-modscript
blob: 0f139b50253858e87b41f491b9c0f5c86fc369bf (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
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-or-later
#
# Copyright © 2020      Christian Kastner <ckk@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see
# <http://www.gnu.org/licenses/>.
#
#######################################################################


set -e
umask 0022


VMROOT="$1"
if [ -z "$VMROOT" ]
then
	echo "$0 expects the mounted root of the VM as first argument." >&2
	exit 1
elif ! mountpoint -q "$VMROOT"
then
	echo "$VMROOT is not a mountpoint." >&2
	exit 1
fi


echo "### Customizing base image ###"

if [ -n "$SQC_SKEL" ]
then
	echo "Copying contents of $SQC_SKEL"
	if [ ! -d "$SQC_SKEL" ]
	then
		echo "$SQC_SKEL is not a directory." >&2
		exit 1
	fi
	cp -pr "$SQC_SKEL/." "$VMROOT/root"
fi

if [ -n "$SQC_AUTH_KEYS" ]
then
	echo "Copying $SQC_AUTH_KEYS to /root/.ssh/"
	if [ ! -f "$SQC_AUTH_KEYS" ]
	then
		echo "$SQC_AUTH_KEYS is not a regular file." >&2
		exit 1
	fi

	TARGET_KEYS="$VMROOT/root/.ssh/authorized_keys"
	if [ ! -d "$VMROOT/root/.ssh" ]
	then
		mkdir --mode=0700 "$VMROOT/root/.ssh"
	fi
	cp "$SQC_AUTH_KEYS" "$VMROOT/root/.ssh/authorized_keys"
	chroot "$VMROOT" chmod 0600 /root/.ssh/authorized_keys
	chroot "$VMROOT" chown root:root /root/.ssh/authorized_keys
	chroot "$VMROOT" apt-get install --quiet --assume-yes openssh-server
fi

if [ -n "$SQC_INSTALL_PACKAGES" ]
then
	echo "Installing additional packages"
	chroot "$VMROOT" apt-get install --quiet --assume-yes $SQC_INSTALL_PACKAGES
fi

if [ -n "$SQC_EXTRA_DEBS" ]
then
	echo "Installing extra .debs"
	VMTMP=`mktemp -d -p "$VMROOT"`
	cp -t "$VMTMP" $SQC_EXTRA_DEBS
	chroot "$VMROOT" dpkg --recursive -i `basename "$VMTMP"`
	chroot "$VMROOT" apt-get update
	rm -rf "$VMTMP"
fi

# Mount point for a shared folder, if the VM is launched with one
echo "Adding 9p to initramfs"
echo -e "9p\n9pnet\n9pnet_virtio" >> "$VMROOT/etc/initramfs-tools/modules"
chroot "$VMROOT" update-initramfs -u
echo "Adding shared folder to fstab"
mkdir -m 755 "$VMROOT/shared"
echo "sbuild-qemu /shared 9p trans=virtio,version=9p2000.L,auto,nofail 0 0" >> "$VMROOT/etc/fstab"

echo "Updating GRUB menu"
echo "GRUB_TIMEOUT=1" >> "$VMROOT/etc/default/grub"
chroot "$VMROOT" update-grub

# Enable automatically setting terminal rows/columns if the host passes us the
# params using -fw_cfg
echo "Creating script in /etc/profile.d/ to set terminal geometry to host"
cat > "$VMROOT/etc/profile.d/sbuild-qemu-terminal-settings.sh" <<"EOF"
#!/bin/sh
# Set VM tty rows/columns to host rows/columns
#
# This only works if the guest kernel was compiled with CONFIG_FW_CFG_FSYS, and
# the host rows/columns were passed on through QEMU using -fw_cfg. Regular
# users will also need permission to read this file (see the udev rule).

ROWSFILE="/sys/firmware/qemu_fw_cfg/by_name/opt/sbuild-qemu/tty-rows/raw"
COLSFILE="/sys/firmware/qemu_fw_cfg/by_name/opt/sbuild-qemu/tty-cols/raw"

DEB_HOST_ARCH="`dpkg-architecture -qDEB_HOST_ARCH`"
if [ "$DEB_HOST_ARCH" = "armhf" ] || [ "$DEB_HOST_ARCH" = "arm64" ]
then
	TTY=/dev/ttyAMA0
else
	TTY=/dev/ttyS0
fi

if [ -f "$ROWSFILE" ]
then
    stty -F "$TTY" rows `cat "$ROWSFILE"`
fi

if [ -f "$COLSFILE" ]
then
    stty -F "$TTY" cols `cat "$COLSFILE"`
fi
EOF

# Makes the image significantly smaller
chroot "$VMROOT" apt-get --option Dir::Etc::SourceList=/dev/null --option Dir::Etc::SourceParts=/dev/null update
chroot "$VMROOT" apt-get  clean

echo "### Customization of base image complete. ###"