Difference between revisions of "debootstrap disk image"

From Noah.org
Jump to navigationJump to search
m
m
Line 1: Line 1:
 
[[Category:Engineering]]
 
[[Category:Engineering]]
  
This creates a minimal Debian Sid root filesystem with added networking and OpenSSH Server. The root password is set to '''password'''.
+
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'''.
 
   
 
   
 
<pre>
 
<pre>
Line 16: Line 16:
 
LOOP=/mnt/loop
 
LOOP=/mnt/loop
  
dd if=/dev/zero of=${DISK_NAME} bs=1048576 count=$((1+${DISK_SIZE}/1048576)) of=${DISK_NAME}
+
# 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}
 
mkfs -F -t ext4 ${DISK_NAME}
 
mkdir -p ${LOOP}
 
mkdir -p ${LOOP}
 
mount -o loop ${DISK_NAME} ${LOOP}
 
mount -o loop ${DISK_NAME} ${LOOP}
debootstrap sid ${LOOP} http://ftp.us.debian.org/debian/
+
debootstrap --include=openssh-server,vim sid ${LOOP} http://ftp.us.debian.org/debian/
sed -i -e 's/tty1/hvc0/g' ${LOOP}/etc/inittab
+
#  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
 
echo root:password | chroot ${LOOP} chpasswd
 
cp /etc/hosts ${LOOP}/etc/hosts
 
cp /etc/hosts ${LOOP}/etc/hosts
Line 34: Line 41:
 
auto eth0
 
auto eth0
 
iface eth0 inet static
 
iface eth0 inet static
  address ${IP}
+
    address ${IP}
  netmask ${NM}
+
    netmask ${NM}
  gateway ${GW}
+
    gateway ${GW}
 
EOF_INTERFACES
 
EOF_INTERFACES
 
cat > ${LOOP}/etc/resolv.conf <<EOF_RESOLV_CONF
 
cat > ${LOOP}/etc/resolv.conf <<EOF_RESOLV_CONF
Line 48: Line 55:
 
chmod 600 ${LOOP}/root/.ssh/authorized_keys
 
chmod 600 ${LOOP}/root/.ssh/authorized_keys
 
chown 0:0 ${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
 
chroot ${LOOP} apt-get install -q -y --allow-unauthenticated openssh-server
 
</pre>
 
</pre>

Revision as of 09:38, 15 May 2014


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