debootstrap disk image

From Noah.org
Revision as of 10:39, 15 May 2014 by Root (talk | contribs)
Jump to navigationJump to search


This creates a minimal Debian Sid root filesystem with added networking and OpenSSH Server. The root password is set to password. This also adds your public SSH RSA key to the new environment's /root/.ssh/authorized_keys file.

#!/bin/bash

DISK_NAME=$1
DISK_SIZE=$2
HOSTNAME=$3
DOMAIN=$4
IP=$5
NM=$6
GW=$7
NS=$8
LOOP=/mnt/loop

# Allocate the disk image using '''fallocate''' if possible; otherwise, using '''dd'''.
if type fallocate 2>/dev/null 1>/dev/null; then
    fallocate -l ${DISK_SIZE} ${DISK_NAME}
else
    # FIXME syntax doesn't agree with fallocate style. This will not accept the K,M,G suffixes that fallocate will allow.
    dd if=/dev/zero of=${DISK_NAME} bs=1048576 count=$((1+${DISK_SIZE}/1048576)) of=${DISK_NAME}
fi
mkfs -F -t ext4 ${DISK_NAME}
mkdir -p ${LOOP}
mount -o loop ${DISK_NAME} ${LOOP}
debootstrap --include=openssh-server,vim sid ${LOOP} http://ftp.us.debian.org/debian/
#  FIXME: This sets the console to use the Xen virtual console, which only applies to Xen.
### sed -i -e 's/tty1/hvc0/g' ${LOOP}/etc/inittab
echo root:password | chroot ${LOOP} chpasswd
cp /etc/hosts ${LOOP}/etc/hosts
cat > ${LOOP}/etc/network/interfaces <<EOF_INTERFACES
# interfaces(5) file used by ifup(8) and ifdown(8)
# Include files from /etc/network/interfaces.d:
source-directory /etc/network/interfaces.d

auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
    address ${IP}
    netmask ${NM}
    gateway ${GW}
EOF_INTERFACES
cat > ${LOOP}/etc/resolv.conf <<EOF_RESOLV_CONF
search ${HOSTNAME}.${DOMAIN}
nameserver ${NS}
EOF_RESOLV_CONF
mkdir ${LOOP}/root/.ssh
chmod 700 ${LOOP}/root/.ssh
chown 0:0 ${LOOP}/root/.ssh
cat ~/.ssh/id_rsa.pub >> ${LOOP}/root/.ssh/authorized_keys
chmod 600 ${LOOP}/root/.ssh/authorized_keys
chown 0:0 ${LOOP}/root/.ssh/authorized_keys
# Install packages. This could have been doing through debootrstrap's "--include" option.
chroot ${LOOP} apt-get install -q -y --allow-unauthenticated openssh-server