| 1 |
# |
| 2 |
# LXC specific functions |
| 3 |
# |
| 4 |
################################################################ |
| 5 |
# |
| 6 |
# Copyright (c) 1995-2014 SUSE Linux Products GmbH |
| 7 |
# |
| 8 |
# This program is free software; you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU General Public License version 2 or 3 as |
| 10 |
# published by the Free Software Foundation. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this program (see the file COPYING); if not, write to the |
| 19 |
# Free Software Foundation, Inc., |
| 20 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 21 |
# |
| 22 |
################################################################ |
| 23 |
|
| 24 |
lxc_get_id() { |
| 25 |
LXCID="obsbuild:${BUILD_ROOT##*/}" |
| 26 |
if which lxc-config >/dev/null 2>&1; then |
| 27 |
LXC_TYPE=standalone |
| 28 |
elif which virsh >/dev/null 2>&1; then |
| 29 |
LXC_TYPE=libvirt |
| 30 |
else |
| 31 |
LXC_TYPE=unknown |
| 32 |
fi |
| 33 |
} |
| 34 |
|
| 35 |
vm_verify_options_lxc() { |
| 36 |
VM_ROOT= |
| 37 |
VM_SWAP= |
| 38 |
} |
| 39 |
|
| 40 |
lxcsh() { |
| 41 |
virsh -c lxc:/// "$@" |
| 42 |
} |
| 43 |
|
| 44 |
vm_startup_lxc() { |
| 45 |
lxc_get_id |
| 46 |
LXCCONF="$BUILD_ROOT/.build.lxc.conf" |
| 47 |
buildroot_rm .build.lxc.conf |
| 48 |
vm_startup_lxc_$LXC_TYPE |
| 49 |
BUILDSTATUS="$?" |
| 50 |
test "$BUILDSTATUS" != 255 || BUILDSTATUS=3 |
| 51 |
cleanup_and_exit "$BUILDSTATUS" |
| 52 |
} |
| 53 |
|
| 54 |
vm_startup_lxc_standalone() { |
| 55 |
LXCDIR="`lxc-config lxc.lxcpath`/$LXCID" |
| 56 |
LXCROOTFS="$LXCDIR/rootfs" |
| 57 |
LXCHOOK="$LXCDIR/pre-mount.hook" |
| 58 |
LXCVERSION="$(lxc-create --version)" |
| 59 |
cat $BUILD_DIR/lxc.conf > "$LXCCONF" |
| 60 |
cat >> "$LXCCONF" <<-EOF |
| 61 |
lxc.rootfs = $LXCROOTFS |
| 62 |
lxc.hook.pre-mount = $LXCHOOK |
| 63 |
EOF |
| 64 |
case "$LXCVERSION" in |
| 65 |
3.*) |
| 66 |
sed -i \ |
| 67 |
-e 's/lxc\.aa_profile = /lxc.apparmor.profile = /' \ |
| 68 |
-e 's/lxc\.console = /lxc.console.path = /' \ |
| 69 |
-e 's/lxc\.rootfs = /lxc.rootfs.path = dir:/' \ |
| 70 |
"$LXCCONF" |
| 71 |
;; |
| 72 |
esac |
| 73 |
# XXX: do this always instead of leaking the hosts' one? |
| 74 |
buildroot_rm /etc/mtab |
| 75 |
echo "rootfs / rootfs rw 0 0" > "$BUILD_ROOT/etc/mtab" |
| 76 |
lxc-destroy -n "$LXCID" >/dev/null 2>&1 || true |
| 77 |
mkdir -p "$LXCROOTFS" |
| 78 |
cat > "$LXCHOOK" <<-EOF |
| 79 |
#!/bin/sh |
| 80 |
mount --bind "$BUILD_ROOT" "$LXCROOTFS" |
| 81 |
EOF |
| 82 |
chmod a+x "$LXCHOOK" |
| 83 |
case "$LXCVERSION" in |
| 84 |
1.0.8|1.1.*|[23].*) |
| 85 |
lxc-create -n "$LXCID" -f "$LXCCONF" -t none || cleanup_and_exit 1 |
| 86 |
lxc-start -n "$LXCID" -F "$vm_init_script" |
| 87 |
;; |
| 88 |
1.0.*) |
| 89 |
lxc-create -n "$LXCID" -f "$LXCCONF" || cleanup_and_exit 1 |
| 90 |
lxc-start -n "$LXCID" "$vm_init_script" |
| 91 |
;; |
| 92 |
*) |
| 93 |
echo "Unsupported lxc version $LXCVERSION!" >&2 |
| 94 |
cleanup_and_exit 1 |
| 95 |
;; |
| 96 |
esac |
| 97 |
if ! [ -r "$BUILD_ROOT/.build/_exitcode" -a ! -L "$BUILD_ROOT/.build/_exitcode" ]; then |
| 98 |
echo "'$BUILD_ROOT/.build/_exitcode' not found or symlink" |
| 99 |
return 3 |
| 100 |
fi |
| 101 |
exitcode=$(cat $BUILD_ROOT/.build/_exitcode) |
| 102 |
return "$exitcode" |
| 103 |
} |
| 104 |
|
| 105 |
vm_startup_lxc_libvirt() { |
| 106 |
local lxc_arch |
| 107 |
# x86 i686 x86_64 amd64 |
| 108 |
case $BUILD_ARCH in |
| 109 |
i586:*) lxc_arch=i686 ;; |
| 110 |
x86_64:*) lxc_arch=x86_64 ;; |
| 111 |
*) lxc_arch=${BUILD_ARCH/:*} ;; |
| 112 |
esac |
| 113 |
|
| 114 |
lxcsh destroy "$LXCID" >/dev/null 2>&1 || true |
| 115 |
cat <<-EOF > "$LXCCONF" |
| 116 |
<domain type='lxc'> |
| 117 |
<name>$LXCID</name> |
| 118 |
<memory unit='MiB'>${VM_MEMSIZE:-512}</memory> |
| 119 |
<os> |
| 120 |
<type arch='$lxc_arch'>exe</type> |
| 121 |
<init>$vm_init_script</init> |
| 122 |
</os> |
| 123 |
<vcpu>1</vcpu> |
| 124 |
<clock offset='utc'/> |
| 125 |
<on_poweroff>destroy</on_poweroff> |
| 126 |
<on_reboot>restart</on_reboot> |
| 127 |
<on_crash>destroy</on_crash> |
| 128 |
<devices> |
| 129 |
<emulator>/usr/lib64/libvirt/libvirt_lxc</emulator> |
| 130 |
<filesystem type='mount'> |
| 131 |
<source dir='$BUILD_ROOT'/> |
| 132 |
<target dir='/'/> |
| 133 |
</filesystem> |
| 134 |
<!-- SLES11 and OpenSUSE 13.1 fails if cannot change /sys owner --> |
| 135 |
<!-- BTW, ro mode can be overlapped with mount -o remount,rw --> |
| 136 |
<filesystem type='mount'> |
| 137 |
<source dir='/sys'/> |
| 138 |
<target dir='/sys'/> |
| 139 |
</filesystem> |
| 140 |
<console type='pty'/> |
| 141 |
</devices> |
| 142 |
<features> |
| 143 |
<privnet/> |
| 144 |
<!-- SLES11 fails if cannot create nodes (mknode) --> |
| 145 |
<capabilities policy='default'> |
| 146 |
<mknod state='on'/> |
| 147 |
</capabilities> |
| 148 |
</features> |
| 149 |
</domain> |
| 150 |
EOF |
| 151 |
# XXX: do this always instead of leaking the hosts' one? |
| 152 |
buildroot_rm /etc/mtab |
| 153 |
echo "rootfs / rootfs rw 0 0" > $BUILD_ROOT/etc/mtab |
| 154 |
# could LOGFILE be used instead? |
| 155 |
lxcsh create --console $LXCCONF | sed -ure 's/\x0d//g;:redo /.\x08/{s/.\x08//; b redo}' |
| 156 |
exitcode="${PIPESTATUS[0]}" |
| 157 |
if [ "$exitcode" -gt 0 ]; then |
| 158 |
return $exitcode # libvirt errors |
| 159 |
fi |
| 160 |
if ! [ -r "$BUILD_ROOT/.build/_exitcode" -a ! -L "$BUILD_ROOT/.build/_exitcode" ]; then |
| 161 |
echo "'$BUILD_ROOT/.build/_exitcode' not found or symlink" |
| 162 |
return 3 |
| 163 |
fi |
| 164 |
exitcode=$(cat $BUILD_ROOT/.build/_exitcode) |
| 165 |
return "$exitcode" |
| 166 |
} |
| 167 |
|
| 168 |
vm_kill_lxc() { |
| 169 |
lxc_get_id |
| 170 |
vm_kill_lxc_$LXC_TYPE |
| 171 |
} |
| 172 |
|
| 173 |
vm_kill_lxc_standalone() { |
| 174 |
lxc-stop -n "$LXCID" || true |
| 175 |
} |
| 176 |
|
| 177 |
vm_kill_lxc_libvirt() { |
| 178 |
lxcsh destroy "$LXCID" || true |
| 179 |
} |
| 180 |
|
| 181 |
vm_fixup_lxc() { |
| 182 |
: |
| 183 |
} |
| 184 |
|
| 185 |
vm_attach_root_lxc() { |
| 186 |
: |
| 187 |
} |
| 188 |
|
| 189 |
vm_attach_swap_lxc() { |
| 190 |
: |
| 191 |
} |
| 192 |
|
| 193 |
vm_detach_root_lxc() { |
| 194 |
: |
| 195 |
} |
| 196 |
|
| 197 |
vm_detach_swap_lxc() { |
| 198 |
: |
| 199 |
} |
| 200 |
|
| 201 |
vm_sysrq_lxc() { |
| 202 |
: |
| 203 |
} |
| 204 |
|
| 205 |
vm_wipe_lxc() { |
| 206 |
: |
| 207 |
} |
| 208 |
|
| 209 |
vm_cleanup_lxc() { |
| 210 |
if test $$ -ne 1 && test $$ -ne 2 ; then |
| 211 |
lxc_get_id |
| 212 |
vm_cleanup_lxc_$LXC_TYPE |
| 213 |
fi |
| 214 |
} |
| 215 |
|
| 216 |
vm_cleanup_lxc_standalone() { |
| 217 |
lxc-destroy -n "$LXCID" |
| 218 |
} |
| 219 |
|
| 220 |
vm_cleanup_lxc_libvirt() { |
| 221 |
lxcsh destroy "$LXCID" >/dev/null 2>&1 || true |
| 222 |
} |
| 223 |
|