Ticket #22999: make_qemu_image

File make_qemu_image, 5.4 KB (added by tmonteil, 4 years ago)
Line 
1#!/bin/bash
2
3
4#*****************************************************************************
5#   Copyright (C) 2015 Thierry Monteil <sage-builder!lma.metelu.net>
6#
7#  Distributed under the terms of the GNU General Public License (GPL)
8#  as published by the Free Software Foundation; either version 3 of
9#  the License, or (at your option) any later version.
10#                  http://www.gnu.org/licenses/
11#
12#*****************************************************************************
13
14
15# This script builds qemu qcow2 generic images for various releases of Debian
16# and Ubuntu, without any user interaction.
17#
18# Usage: make_qemu_image <distro> <release> <arch> <size> <image_file_path> <hostname_prefix>
19#   - <distro>          : debian, ubuntu.
20#   - <release>         : stable, wheezy, testing, jessie, precise, trusty, 14.04, ...
21#   - <arch>            : 32, 64.
22#   - <size>            : 800M, 3G, ...
23#   - <image_file_path> : ./qemu_images/myimage.qcow2, /tmp/debian.qcow2, ...
24#   - <hostname_prefix> : anonymous, my-name, warning-no-underscore
25
26
27
28### VARIABLES YOU MAY WANT TO CHANGE
29
30DEBIAN_MIRROR='http://http.debian.net/debian/'
31UBUNTU_MIRROR='http://archive.ubuntu.com/ubuntu/'
32
33
34
35### ROBUSTNESS OF THE SCRIPT
36
37set -ex
38
39fail () {
40    echo "${1}"
41    exit 1
42}
43
44cleanup () {
45    sync
46    sleep 5
47    service udev restart
48    [ "${TARGET_MOUNT_POINT}" ] && umount ${TARGET_MOUNT_POINT}
49    [ "${TARGET_DISK}" ] && qemu-nbd -d ${TARGET_DISK}
50    [ "${TARGET_MOUNT_POINT}" ] && rm -rf ${TARGET_MOUNT_POINT}
51}
52
53trap cleanup EXIT
54
55
56
57### SETTING VARIABLES ACCORDING TO PROVIDED ARGUMENTS
58
59[ "$(whoami)" = "root" ] || fail "this script should be run as root"
60
61DISTRO=${1}
62RELEASE=${2}
63ARCH=${3}
64SIZE=${4}
65IMAGE_FILE_PATH=${5}
66HOSTNAME_PREFIX=${6}
67
68
69HOSTNAME=${HOSTNAME_PREFIX}-${DISTRO}-${RELEASE}-${ARCH}
70
71
72case ${DISTRO} in
73    (debian)
74        MIRROR="${DEBIAN_MIRROR}" ;
75        if [ ${RELEASE} = 'sid' ] || [ ${RELEASE} = 'unstable' ] ; then
76            SECURITY_APT=""
77        else
78            SECURITY_APT="deb http://security.debian.org/ ${RELEASE}/updates main" ;
79        fi
80        case ${ARCH} in
81            (32)
82                LINUX_IMAGE='linux-image-486' ; # possibly 686-pae
83                DEBOOTSTRAP_ARCH='i386' ;
84                QEMU_ARCH='i386' ;
85                ;;
86            (64)
87                LINUX_IMAGE='linux-image-amd64' ;
88                DEBOOTSTRAP_ARCH='amd64' ;
89                QEMU_ARCH='x86_64' ;
90                ;;
91        esac ;;
92    (ubuntu)
93        MIRROR="${UBUNTU_MIRROR}" ;
94        SECURITY_APT="deb ${MIRROR} ${RELEASE}-security main" ;
95        LINUX_IMAGE='linux-image-generic' ;
96        case ${ARCH} in
97            (32)
98                DEBOOTSTRAP_ARCH='i386' ;
99                QEMU_ARCH='i386' ;
100                ;;
101            (64)
102                DEBOOTSTRAP_ARCH='amd64' ;
103                QEMU_ARCH='x86_64' ;
104                ;;
105        esac ;;
106esac
107
108
109
110### INITIALIZE AND MOUNT TARGET IMAGE
111
112qemu-img create -f qcow2 ${IMAGE_FILE_PATH} ${SIZE}
113
114TARGET_MOUNT_POINT="qemu_${$}"
115rm -rf ${TARGET_MOUNT_POINT} || true
116mkdir ${TARGET_MOUNT_POINT}
117unset TARGET_DISK
118
119modprobe nbd max_part=16
120for i in /dev/nbd*
121do
122    if qemu-nbd -c ${i} ${IMAGE_FILE_PATH}
123    then
124        TARGET_DISK=${i}
125        break
126    fi
127done
128
129[ "${TARGET_DISK}" ] || fail "no nbd device available"
130
131echo '0,,83,*' | sfdisk ${TARGET_DISK} -q -D -uM
132
133mkfs.ext4 -q ${TARGET_DISK}p1
134
135mount ${TARGET_DISK}p1 ${TARGET_MOUNT_POINT}
136
137
138
139### INSTALL AND CONFIGURE TARGET IMAGE
140
141# base install
142debootstrap --arch ${DEBOOTSTRAP_ARCH} --include=less,vim,sudo,openssh-server,rsync,${LINUX_IMAGE} ${RELEASE} $TARGET_MOUNT_POINT ${MIRROR} || fail "cannot install ${RELEASE} into ${TARGET_DISK}"
143
144# mount points
145cat << EOF > ${TARGET_MOUNT_POINT}/etc/fstab
146/dev/sda1 /                   ext4    errors=remount-ro 0       1
147EOF
148
149# hostname
150echo ${HOSTNAME} > ${TARGET_MOUNT_POINT}/etc/hostname
151
152# hosts
153cat << EOF > ${TARGET_MOUNT_POINT}/etc/hosts
154127.0.0.1       localhost
155127.0.1.1       ${HOSTNAME}
156
157# The following lines are desirable for IPv6 capable hosts
158::1     localhost ip6-localhost ip6-loopback
159ff02::1 ip6-allnodes
160ff02::2 ip6-allrouters
161EOF
162
163# network
164cat << EOF > ${TARGET_MOUNT_POINT}/etc/network/interfaces
165auto lo
166iface lo inet loopback
167
168auto eth0
169iface eth0 inet dhcp
170EOF
171
172
173# apt : security updates + extlinux belongs to universe in Ubuntu
174echo ${SECURITY_APT} >> ${TARGET_MOUNT_POINT}/etc/apt/sources.list
175sed -i 's/\(ubuntu.* main$\)/\1 universe/g' ${TARGET_MOUNT_POINT}/etc/apt/sources.list
176
177
178# connect as root via ssh
179sed -i 's/^PermitRootLogin.*/PermitRootLogin yes/g' ${TARGET_MOUNT_POINT}/etc/ssh/sshd_config
180
181if [ -f id_rsa.pub ] ; then
182    mkdir -p ${TARGET_MOUNT_POINT}/root/.ssh
183    cp id_rsa.pub ${TARGET_MOUNT_POINT}/root/.ssh/authorized_keys
184else
185    # log-in without password if no public key available.
186    chroot $TARGET_MOUNT_POINT passwd -d root
187    sed -i 's/^PermitEmptyPasswords.*/PermitEmptyPasswords yes/g' ${TARGET_MOUNT_POINT}/etc/ssh/sshd_config
188    sed -i 's/^\(UsePAM.*\)/# \1/g' ${TARGET_MOUNT_POINT}/etc/ssh/sshd_config
189fi
190
191
192# extlinux
193dd if=/usr/lib/EXTLINUX/mbr.bin of=${TARGET_DISK} conv=notrunc bs=440 count=1 ||dd if=/usr/lib/extlinux/mbr.bin of=${TARGET_DISK} conv=notrunc bs=440 count=1
194mkdir -p ${TARGET_MOUNT_POINT}/boot/extlinux
195cp extlinux_update.sh ${TARGET_MOUNT_POINT}/boot/extlinux
196chmod +x ${TARGET_MOUNT_POINT}/boot/extlinux/extlinux_update.sh
197(cd ${TARGET_MOUNT_POINT}/boot/extlinux ; ./extlinux_update.sh)
198
199
200### END
201
202exit 0
203