5 # Parse input parameters
7 echo "Usage: $0 --release|-r <jessie|wheezy> [--minimal|-m] [--debootstrap-url|-u <debootstrap-mirror>]"
14 "--extra-packages"|"-e")
15 if [ -z "${2}" ] ; then
16 echo "No parameter defining the extra packages"
23 "--debootstrap-url"|"-u")
24 if [ -z "${2}" ] ; then
25 echo "No parameter defining the debootstrap URL"
37 if [ "${2}" = "wheezy" ] || [ "${2}" = "jessie" ] ; then
42 echo "Release not recognized."
51 if [ -z "${RELEASE}" ] ; then
52 echo "Release not recognized: please specify the -r parameter."
55 if [ -z "${DEB_MIRROR}" ] ; then
56 DEB_MIRROR=http://ftp.uk.debian.org/debian
58 if [ -z "${EXTRA_PACKAGES}" ] ; then
59 EXTRA_PACKAGES=bash-completion,joe,most,screen,less,vim,bzip2
62 NEEDED_PACKAGES=sudo,adduser,locales,extlinux,openssh-server,linux-image-amd64,euca2ools,file
63 if [ "${RELEASE}" = "jessie" ] ; then
64 NEEDED_PACKAGES=${NEEDED_PACKAGES},cloud-init,cloud-utils,cloud-initramfs-growroot
66 # These are needed by cloud-init and friends, and since we don't debootstrap them
67 # when creating a Wheezy image, we resolve dependencies by hand
68 NEEDED_PACKAGES=${NEEDED_PACKAGES},python,python-paramiko,python-argparse,python-cheetah,python-configobj,python-oauth,python-software-properties,python-yaml,python-boto,python-prettytable,initramfs-tools,python-requests
71 if [ ${EXTRA} = "no" ] ; then
72 PKG_LIST=${NEEDED_PACKAGES}
74 PKG_LIST=${NEEDED_PACKAGES},${EXTRA_PACKAGES}
76 if ! [ `whoami` = "root" ] ; then
77 echo "You have to be root to run this script"
80 FILE_NAME=debian-${RELEASE}-7.0.0-2-amd64
81 AMI_NAME=${FILE_NAME}.raw
82 QCOW2_NAME=${FILE_NAME}.qcow2
87 ######################################
88 ### Prepare the HDD (format, ext.) ###
89 ######################################
91 qemu-img create ${AMI_NAME} 1G
92 #dd if=/dev/null bs=1M seek=1024 of=${AMI_NAME}
94 ${PARTED} -s ${AMI_NAME} mktable msdos
95 ${PARTED} -s -a optimal ${AMI_NAME} mkpart primary ext3 1M 100%
96 ${PARTED} -s ${AMI_NAME} set 1 boot on
97 install-mbr ${AMI_NAME}
98 RESULT_KPARTX=`kpartx -av ${AMI_NAME} 2>&1`
100 if echo "${RESULT_KPARTX}" | grep "^add map" ; then
101 LOOP_DEVICE=`echo ${RESULT_KPARTX} | cut -d" " -f3`
102 echo "kpartx mounted using: ${LOOP_DEVICE}"
104 echo "It seems kpartx didn't mount the image correctly: exiting."
108 # We first use ext2, THEN convert to ext3, because that's so much faster this way.
109 mkfs.ext2 /dev/mapper/${LOOP_DEVICE}
111 # No fsck because of X days without checks
112 tune2fs -i 0 /dev/mapper/${LOOP_DEVICE}
114 MOUNT_DIR=`mktemp -d -t build-debimg.XXXXXX`
115 mount -o loop /dev/mapper/${LOOP_DEVICE} ${MOUNT_DIR}
116 debootstrap --verbose \
117 --include=${PKG_LIST} \
118 ${RELEASE} ${MOUNT_DIR} ${DEB_MIRROR}
120 ############################
121 ### Customize the distro ###
122 ############################
123 ### Customize: access to the VM ###
124 # # # # # # # # # # # # # # # # # #
125 # Setup default root password to: password
126 chroot ${MOUNT_DIR} sh -c "echo root:password | chpasswd"
128 # Otherwise, we have a huge backdoor, since the root password
129 # is always the same.
130 sed -i "s/PermitRootLogin yes/PermitRootLogin without-password/" ${MOUNT_DIR}/etc/ssh/sshd_config
132 # Add a default "debian" user which is used by cloud-init by default
133 chroot ${MOUNT_DIR} adduser --gecos Debian-cloud-init-user --disabled-password --quiet debian
135 # Adds the "debian" user to sudoers, since that is the way
136 # cloud-init grant access
137 mkdir -p ${MOUNT_DIR}/etc/sudoers.d
138 echo "debian ALL = NOPASSWD: ALL" >${MOUNT_DIR}/etc/sudoers.d/debian-cloud-init
139 chmod 0440 ${MOUNT_DIR}/etc/sudoers.d/debian-cloud-init
141 # cloud-init will put the ssh key in whatever is
142 # configured in the sshd_config. So we put the
143 # debian standard config as explicit in sshd_config.
144 sed -i -e 's|^#AuthorizedKeysFile[ \t]%h/.ssh/authorized_keys|AuthorizedKeysFile %h/.ssh/authorized_keys2|' ${MOUNT_DIR}/etc/ssh/sshd_config
146 ### Customize: misc stuff ###
147 # # # # # # # # # # # # # # #
149 echo "# /etc/fstab: static file system information.
150 proc /proc proc nodev,noexec,nosuid 0 0
151 /dev/vda1 / ext3 errors=remount-ro 0 1
152 " > ${MOUNT_DIR}/etc/fstab
153 chroot ${MOUNT_DIR} mount /proc || true
155 echo "# disable pc speaker
156 blacklist pcspkr" >${MOUNT_DIR}/etc/modprobe.d/blacklist.conf
158 echo "# Required for cinder hotplug
161 " ${MOUNT_DIR}>>/etc/modules
163 # Enable bash-completion by default
164 if [ ${EXTRA} = "yes" ] ; then
165 echo "# enable bash completion in interactive shells
166 if ! shopt -oq posix; then
167 if [ -f /usr/share/bash-completion/bash_completion ]; then
168 . /usr/share/bash-completion/bash_completion
169 elif [ -f /etc/bash_completion ]; then
170 . /etc/bash_completion
172 fi" >>${MOUNT_DIR}/etc/bash.bashrc
174 # No clear for the tty1 console
175 sed -i "s#1:2345:respawn:/sbin/getty 38400 tty1#1:2345:respawn:/sbin/getty --noclear 38400 tty1#" ${MOUNT_DIR}/etc/inittab
177 chroot ${MOUNT_DIR} apt-get install -y locales-all
180 rm -f ${MOUNT_DIR}/etc/ssh/ssh_host_*
181 rm -f ${MOUNT_DIR}/etc/udev/rules.d/70-persistent-net.rules
182 rm -f ${MOUNT_DIR}/lib/udev/write_net_rules
184 # Setup networking (eg: DHCP by default)
185 echo "# This file describes the network interfaces available on your system
186 # and how to activate them. For more information, see interfaces(5).
188 # The loopback network interface
190 iface lo inet loopback
196 # Maybe the VM has 2 NICs?
200 # Maybe the VM has 3 NICs?
203 " > ${MOUNT_DIR}/etc/network/interfaces
205 # Setup the default hostname (will be set by cloud-init
206 # at boot time anyway)
207 echo "debian.example.com" >${MOUNT_DIR}/etc/hostname
209 # This should be a correct default everywhere
210 echo "deb http://http.debian.net/debian ${RELEASE} main
211 deb-src http://http.debian.net/debian ${RELEASE} main"
213 if [ "${RELEASE}" = "wheezy" ] ; then
214 echo "deb http://http.debian.net/debian wheezy-updates main
215 deb-src http://http.debian.net/debian wheezy-updates main
216 deb http://security.debian.org/ wheezy/updates main
217 deb-src http://security.debian.org/ wheezy/updates main
218 " >>${MOUNT_DIR}/etc/apt/sources.list
220 chroot ${MOUNT_DIR} apt-get update
222 # Setup some cloud stuff
223 # Since none of cloud-init, cloud-utils and cloud-initramfs-growroot
224 # are in Wheezy, we expect that the backported .deb is in the current
225 # path. We first check for that fact.
226 if [ "${RELEASE}" = "wheezy" ] ; then
227 CHECK=`ls cloud-init_*.deb cloud-utils_*.deb cloud-initramfs-growroot_*.deb | wc -l`
228 if [ "${CHECK}" = 3 ] ; then
229 PKGS="cloud-init cloud-utils cloud-initramfs-growroot"
231 cp ${i}_*.deb ${MOUNT_DIR}
232 chroot ${MOUNT_DIR} dpkg -i ${i}_*.deb
233 rm ${MOUNT_DIR}/${i}_*.deb
236 echo "You've selected Wheezy, though there's some missing .deb files."
237 echo "Please drop cloud-init cloud-utils cloud-initramfs-growroot"
238 echo ".deb backports in your current directory."
240 kpartx -d ${AMI_NAME}
245 # For OpenStack, we would like to use Ec2 and no other API
246 echo "# to update this file, run dpkg-reconfigure cloud-init
247 datasource_list: [ Ec2 ]" >${MOUNT_DIR}/etc/cloud/cloud.cfg.d/90_dpkg.cfg
249 # Setting-up initramfs
250 chroot ${MOUNT_DIR} update-initramfs -u
252 rm ${MOUNT_DIR}/var/cache/apt/archives/*.deb
254 ###########################
255 ### Setting-up extlinux ###
256 ###########################
257 KERNEL=`chroot ${MOUNT_DIR} find boot -name 'vmlinuz-*'`
258 RAMDISK=`chroot ${MOUNT_DIR} find boot -name 'initrd.img-*'`
259 UUID=`blkid -o value -s UUID /dev/mapper/${LOOP_DEVICE}`
264 append initrd=${RAMDISK} root=/dev/vda1 ro quiet" > ${MOUNT_DIR}/boot/extlinux/extlinux.conf
265 #append initrd=${RAMDISK} root=/dev/vda1 console=tty0 console=ttyS0,115200 ro quiet" > ${MOUNT_DIR}/boot/extlinux/extlinux.conf
266 cp ${MOUNT_DIR}/boot/extlinux/extlinux.conf ${MOUNT_DIR}/extlinux.conf
267 extlinux --install ${MOUNT_DIR}
268 #chroot ${MOUNT_DIR} extlinux-update
270 ##########################
271 ### Unmount everything ###
272 ##########################
273 chroot ${MOUNT_DIR} umount /proc || true
275 # Run FSCK so that resize can work
276 tune2fs -j /dev/mapper/${LOOP_DEVICE}
277 fsck.ext3 -f /dev/mapper/${LOOP_DEVICE} || true
278 #losetup -d /dev/mapper/${LOOP_DEVICE}
279 kpartx -d ${AMI_NAME}
281 qemu-img convert -c -f raw ${AMI_NAME} -O qcow2 ${QCOW2_NAME}