Skip to content

Instantly share code, notes, and snippets.

@jalsot
Forked from spectra/qemu-debian-create-image
Last active October 27, 2020 03:24
Show Gist options
  • Select an option

  • Save jalsot/a24aa543021889ad0c70 to your computer and use it in GitHub Desktop.

Select an option

Save jalsot/a24aa543021889ad0c70 to your computer and use it in GitHub Desktop.

Revisions

  1. jalsot revised this gist Oct 31, 2015. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion qemu-debian-create-image
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,12 @@

    # Configs overwritable via environment variables
    VSYSTEM=${VSYSTEM:=qemu} # Either 'qemu' or 'kvm'
    FLAVOUR=${FLAVOUR:=debian} # Either 'debian' or 'ubuntu'
    INCLUDES=${INCLUDES:="less,vim,sudo,openssh-server,acpid"}
    MIRROR=${MIRROR:="http://ftp.hu.debian.org/debian"}
    #MIRROR=${MIRROR:="http://localhost:3142/debian"}
    #MIRROR=${MIRROR:="http://hu.archive.ubuntu.com/ubuntu/"}
    #MIRROR=${MIRROR:="http://localhost:3142/ubuntu"}
    ARCH=${ARCH:=amd64}
    APT_CACHER=${APT_CACHER:=no}
    IMGSIZE=${IMGSIZE:=8G}
    @@ -63,6 +66,12 @@ if [ ! -f $FILE ]; then
    $VSYSTEM-img create -f qcow2 $FILE $IMGSIZE
    fi

    if [ $FLAVOUR == "debian" ]; then
    BOOT_PKG="linux-image-$ARCH grub-pc"
    elif [ $FLAVOUR == "ubuntu" ]; then
    BOOT_PKG="linux-image-generic grub-pc"
    fi

    echo "Looking for nbd device..."

    modprobe nbd max_part=16 || fail "failed to load nbd module into kernel"
    @@ -128,7 +137,7 @@ mount --bind /dev/ $MNT_DIR/dev || fail "cannot bind /dev"
    chroot $MNT_DIR mount -t ext4 ${DISK}p1 /boot || fail "cannot mount /boot"
    chroot $MNT_DIR mount -t proc none /proc || fail "cannot mount /proc"
    chroot $MNT_DIR mount -t sysfs none /sys || fail "cannot mount /sys"
    LANG=C DEBIAN_FRONTEND=noninteractive chroot $MNT_DIR apt-get install -y --force-yes -q linux-image-$ARCH grub-pc || fail "cannot install linux-image and grub"
    LANG=C DEBIAN_FRONTEND=noninteractive chroot $MNT_DIR apt-get install -y --force-yes -q $BOOT_PKG || fail "cannot install $BOOT_PKG"
    chroot $MNT_DIR grub-install $DISK || fail "cannot install grub"
    chroot $MNT_DIR update-grub || fail "cannot update grub"
    chroot $MNT_DIR apt-get clean || fail "unable to clean apt cache"
  2. jalsot revised this gist Oct 31, 2015. 1 changed file with 22 additions and 7 deletions.
    29 changes: 22 additions & 7 deletions qemu-debian-create-image
    100644 → 100755
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,13 @@
    #!/bin/bash

    # Configs
    VSYSTEM=qemu # Either 'qemu' or 'kvm'
    INCLUDES='less,vim,sudo,openssh-server,acpid'
    MIRROR="http://ftp.br.debian.org/debian"
    ARCH=686
    # Configs overwritable via environment variables
    VSYSTEM=${VSYSTEM:=qemu} # Either 'qemu' or 'kvm'
    INCLUDES=${INCLUDES:="less,vim,sudo,openssh-server,acpid"}
    MIRROR=${MIRROR:="http://ftp.hu.debian.org/debian"}
    #MIRROR=${MIRROR:="http://localhost:3142/debian"}
    ARCH=${ARCH:=amd64}
    APT_CACHER=${APT_CACHER:=no}
    IMGSIZE=${IMGSIZE:=8G}

    clean_debian() {
    [ "$MNT_DIR" != "" ] && chroot $MNT_DIR umount /proc/ /sys/ /dev/ /boot/
    @@ -49,6 +52,17 @@ rm $MNT_DIR
    mkdir $MNT_DIR
    DISK=

    # add apt cacher for faster rebuilds, runs on 3142
    if [ "$APT_CACHER" == "yes" ]; then
    echo "Installing apt-cacher-ng for fast rebuilds"
    apt-get install apt-cacher-ng
    fi

    if [ ! -f $FILE ]; then
    echo "Creating $FILE"
    $VSYSTEM-img create -f qcow2 $FILE $IMGSIZE
    fi

    echo "Looking for nbd device..."

    modprobe nbd max_part=16 || fail "failed to load nbd module into kernel"
    @@ -114,9 +128,10 @@ mount --bind /dev/ $MNT_DIR/dev || fail "cannot bind /dev"
    chroot $MNT_DIR mount -t ext4 ${DISK}p1 /boot || fail "cannot mount /boot"
    chroot $MNT_DIR mount -t proc none /proc || fail "cannot mount /proc"
    chroot $MNT_DIR mount -t sysfs none /sys || fail "cannot mount /sys"
    LANG=C DEBIAN_FRONTEND=noninteractive chroot $MNT_DIR apt-get install -y -q linux-image-$ARCH grub-pc || fail "cannot install linux-image and grub"
    LANG=C DEBIAN_FRONTEND=noninteractive chroot $MNT_DIR apt-get install -y --force-yes -q linux-image-$ARCH grub-pc || fail "cannot install linux-image and grub"
    chroot $MNT_DIR grub-install $DISK || fail "cannot install grub"
    chroot $MNT_DIR update-grub || fail "cannot update grub"
    chroot $MNT_DIR apt-get clean || fail "unable to clean apt cache"

    sed -i "s|${DISK}p1|/dev/vda1|g" $MNT_DIR/boot/grub/grub.cfg
    sed -i "s|${DISK}p2|/dev/vda2|g" $MNT_DIR/boot/grub/grub.cfg
    @@ -132,4 +147,4 @@ grub-install $DISK --root-directory=$MNT_DIR --modules="biosdisk part_msdos" ||

    echo "SUCCESS!"
    clean_debian
    exit 0
    exit 0
  3. Pablo Lorenzzoni revised this gist Apr 9, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion qemu-debian-create-image
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    VSYSTEM=qemu # Either 'qemu' or 'kvm'
    INCLUDES='less,vim,sudo,openssh-server,acpid'
    MIRROR="http://ftp.br.debian.org/debian"
    ARCH=i386
    ARCH=686

    clean_debian() {
    [ "$MNT_DIR" != "" ] && chroot $MNT_DIR umount /proc/ /sys/ /dev/ /boot/
  4. Pablo Lorenzzoni created this gist Apr 9, 2014.
    135 changes: 135 additions & 0 deletions qemu-debian-create-image
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,135 @@
    #!/bin/bash

    # Configs
    VSYSTEM=qemu # Either 'qemu' or 'kvm'
    INCLUDES='less,vim,sudo,openssh-server,acpid'
    MIRROR="http://ftp.br.debian.org/debian"
    ARCH=i386

    clean_debian() {
    [ "$MNT_DIR" != "" ] && chroot $MNT_DIR umount /proc/ /sys/ /dev/ /boot/
    sleep 1s
    [ "$MNT_DIR" != "" ] && umount $MNT_DIR
    sleep 1s
    [ "$DISK" != "" ] && $VSYSTEM-nbd -d $DISK
    sleep 1s
    [ "$MNT_DIR" != "" ] && rm -r $MNT_DIR
    }

    fail() {
    clean_debian
    echo ""
    echo "FAILED: $1"
    exit 1
    }

    cancel() {
    fail "CTRL-C detected"
    }

    if [ $# -lt 3 ]
    then
    echo "author: Kamil Trzcinski (http://ayufan.eu)"
    echo "license: GPL"
    echo "usage: $0 <image-file> <hostname> <release> [optional debootstrap args]" 1>&2
    exit 1
    fi

    FILE=$1
    HOSTNAME=$2
    RELEASE=$3
    shift 3

    trap cancel INT

    echo "Installing $RELEASE into $FILE..."

    MNT_DIR=`tempfile`
    rm $MNT_DIR
    mkdir $MNT_DIR
    DISK=

    echo "Looking for nbd device..."

    modprobe nbd max_part=16 || fail "failed to load nbd module into kernel"

    for i in /dev/nbd*
    do
    if $VSYSTEM-nbd -c $i $FILE
    then
    DISK=$i
    break
    fi
    done

    [ "$DISK" == "" ] && fail "no nbd device available"

    echo "Connected $FILE to $DISK"

    echo "Partitioning $DISK..."
    sfdisk $DISK -q -D -uM << EOF || fail "cannot partition $FILE"
    ,200,83,*
    ;
    EOF

    echo "Creating boot partition..."
    mkfs.ext4 -q ${DISK}p1 || fail "cannot create /boot ext4"

    echo "Creating root partition..."
    mkfs.ext4 -q ${DISK}p2 || fail "cannot create / ext4"

    echo "Mounting root partition..."
    mount ${DISK}p2 $MNT_DIR || fail "cannot mount /"

    echo "Installing Debian $RELEASE..."
    debootstrap --include=$INCLUDES $* $RELEASE $MNT_DIR $MIRROR || fail "cannot install $RELEASE into $DISK"

    echo "Configuring system..."
    cat <<EOF > $MNT_DIR/etc/fstab
    /dev/vda1 /boot ext4 sync 0 2
    /dev/vda2 / ext4 errors=remount-ro 0 1
    EOF

    echo $HOSTNAME > $MNT_DIR/etc/hostname

    cat <<EOF > $MNT_DIR/etc/hosts
    127.0.0.1 localhost
    127.0.1.1 $HOSTNAME
    # The following lines are desirable for IPv6 capable hosts
    ::1 localhost ip6-localhost ip6-loopback
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    EOF

    cat <<EOF > $MNT_DIR/etc/network/interfaces
    auto lo
    iface lo inet loopback
    auto eth0
    iface eth0 inet dhcp
    EOF

    mount --bind /dev/ $MNT_DIR/dev || fail "cannot bind /dev"
    chroot $MNT_DIR mount -t ext4 ${DISK}p1 /boot || fail "cannot mount /boot"
    chroot $MNT_DIR mount -t proc none /proc || fail "cannot mount /proc"
    chroot $MNT_DIR mount -t sysfs none /sys || fail "cannot mount /sys"
    LANG=C DEBIAN_FRONTEND=noninteractive chroot $MNT_DIR apt-get install -y -q linux-image-$ARCH grub-pc || fail "cannot install linux-image and grub"
    chroot $MNT_DIR grub-install $DISK || fail "cannot install grub"
    chroot $MNT_DIR update-grub || fail "cannot update grub"

    sed -i "s|${DISK}p1|/dev/vda1|g" $MNT_DIR/boot/grub/grub.cfg
    sed -i "s|${DISK}p2|/dev/vda2|g" $MNT_DIR/boot/grub/grub.cfg

    echo "Enter root password:"
    while ! chroot $MNT_DIR passwd root
    do
    echo "Try again"
    done

    echo "Finishing grub installation..."
    grub-install $DISK --root-directory=$MNT_DIR --modules="biosdisk part_msdos" || fail "cannot reinstall grub"

    echo "SUCCESS!"
    clean_debian
    exit 0