From cf0edd94c05a9fc0c609758016a697c24dd913a0 Mon Sep 17 00:00:00 2001 From: VG Date: Sun, 6 Nov 2016 20:04:54 +0100 Subject: add other debootstrap scripts and older instructions --- prepare-vm-by-debootstrap/README | 1 + prepare-vm-by-debootstrap/make-vm-debootstrap | 370 ++++++++++++++++++++++++++ 2 files changed, 371 insertions(+) create mode 100644 prepare-vm-by-debootstrap/README create mode 100755 prepare-vm-by-debootstrap/make-vm-debootstrap (limited to 'prepare-vm-by-debootstrap') diff --git a/prepare-vm-by-debootstrap/README b/prepare-vm-by-debootstrap/README new file mode 100644 index 0000000..b506420 --- /dev/null +++ b/prepare-vm-by-debootstrap/README @@ -0,0 +1 @@ +Here disk.img is just a temp disk to prepare a vm. I don't mind erasing it. diff --git a/prepare-vm-by-debootstrap/make-vm-debootstrap b/prepare-vm-by-debootstrap/make-vm-debootstrap new file mode 100755 index 0000000..cad9bf0 --- /dev/null +++ b/prepare-vm-by-debootstrap/make-vm-debootstrap @@ -0,0 +1,370 @@ +#!/bin/bash + +set -e +exec 3>&1 +exec >/tmp/debootstrap.log +echo "I: log=/tmp/debootstrap.log">&2 +#set -x + +if [ "$(id -ru)" != "0" ]; then + echo "Run this script as root">&2 + exit 1 +fi + +# you can abuse official mirror for debootstrap since we are going to cache +# packages anyway. + +RELEASE=raring +MIRROR=http://fr.archive.ubuntu.com/ubuntu/ +ARCH=amd64 +#DESTDIR=/tmp/debootstrap-mountpoint +PKGS="$(sed 's/^#.*$//'<&2; exit 1;; + esac + shift +done + +#[ -z "$BLOCKDEV" ] && { echo "Specify block device (-d)">&2; exit 1; } +[ -z "$DESTDIR" ] && { echo "Specify destdir (-d)">&2; exit 1; } + +ARCHIVE=$PWD/$RELEASE-$ARCH-debootstrap.tgz +KERNEL=$PWD/$RELEASE-$ARCH-vmlinuz + +#BLOCKDEV="$(df -P "$DESTDIR" | tail -n 1 | cut -d' ' -f1)" +#( +#echo "I: blockdevice associated to destination is $BLOCKDEV" +#echo "I: blockdevice will not be manipulated directly," +#echo "I: it is only used for informational purposes (such as UUID)" +#)>&2 + +echo "I: script will begin in 10seconds...">&2 +printf "I: ">&2 +for i in $(seq 10 -1 0); do sleep 1; printf "%d... " $i>&2; done +echo>&2 + +#echo "Warning: will erase $BLOCKDEV with mkfs.ext4 in 10seconds...">&2 +#for i in $(seq 10 -1 0); do sleep 1; printf "%d... " $i>&2; done +#echo>&2 +#mkfs -q -F -t ext4 -L rootfs "$BLOCKDEV" || exit 1 +#mkdir -p "$DESTDIR" +#echo "mounting block device...">&2 +#mount -v -t ext4 "$BLOCKDEV" "$DESTDIR" || \ +# mount -v -t ext4 -o loop "$BLOCKDEV" "$DESTDIR" || exit 1 +#echo "In case of error, please remember to umount $DESTDIR">&2 + +if [ ! -e "$ARCHIVE" ]; then + echo "I: debootstrap archive does not exists, making it">&2 + debootstrap --foreign --arch="$ARCH" \ + --include="$PKGS" --components=main,universe \ + --make-tarball "$ARCHIVE" \ + "$RELEASE" "$DESTDIR" "$MIRROR" || exit 1 +fi + +echo "I: debootstraping...">&2 +debootstrap --foreign --arch="$ARCH" \ + --include="$PKGS" --components=main,universe \ + --unpack-tarball "$ARCHIVE" \ + "$RELEASE" "$DESTDIR" "$MIRROR" + +if [ ! -e "$KERNEL" ];then + echo "I: wget kernel...">&2 + wget -q "$MIRROR/dists/$RELEASE/main/installer-$ARCH/current/images/netboot/ubuntu-installer/$ARCH/linux" -O "$KERNEL" +fi + +echo "I: adjust configuration...">&2 +rm -f "$DESTDIR/etc/localtime" +cat "$DESTDIR/usr/share/zoneinfo/Europe/Paris" > "$DESTDIR/etc/localtime" + +# network +cat >"$DESTDIR/etc/network/interfaces"<"$DESTDIR/etc/hosts" +echo "localhost">"$DESTDIR/etc/hostname" +echo "servername 10.1.1.254">"$DESTDIR/etc/resolv.conf" + +for f in "$DESTDIR"/etc/init/tty[1-6].conf; do + echo "manual">${f%.conf}.override +done + +chmod +w /etc/sudoers +sed 's,#* *%sudo.*,%sudo ALL=(ALL) NOPASSWD: ALL,' -i /etc/sudoers +chmod -w /etc/sudoers + +#UUID="$(blkid "$BLOCKDEV" | sed 's/^.*UUID="\([a-zA-Z0-9-]\+\)".*$/\1/')" +cat >"$DESTDIR/etc/fstab"< +/dev/vda / ext4 errors=remount-ro,noatime 0 1 +tmpfs /tmp tmpfs mode=1777,noatime,size=128m,nodev,nosuid,noexec 0 0 +tmpfs /var/log tmpfs mode=1777,noatime,size=4m,nodev,nosuid,noexec 0 0 +tmpfs /var/tmp tmpfs mode=1777,noatime,size=128m,nodev,nosuid,noexec 0 0 + +#UUID=$UUID / ext4 errors=remount-ro,noatime 0 1 +#UUID=use blkid for uuid discovery /home ext4 noatime 0 2 +#UUID=6d9ac889-3c25-4e05-90f3-d183f8b14aca none swap sw,auto 0 0 +EOF + +mkdir -p "$DESTDIR/etc/sv/ttyS0/" +mkdir -p "$DESTDIR/etc/service/" +cat >"$DESTDIR/etc/sv/ttyS0/run"<<"EOF" +#!/bin/bash +dev=/dev/ttyS0 +/bin/stty -F $dev sane clocal -crtscts -hupcl +exec <$dev >$dev 2>$dev +echo +echo "Press enter to continue..."; read +cd /root +/usr/bin/env -i HOME=/root PATH=/usr/sbin:/usr/bin:/sbin:/bin TERM=screen-256color /usr/bin/setsid /bin/bash +exec wait +#exec /bin/bash +#exec /usr/bin/env -i /bin/sh +#exec /usr/bin/setsid /bin/sh +EOF +chmod +x "$DESTDIR/etc/sv/ttyS0/run" +ln -sfv "/etc/sv/ttyS0/" "$DESTDIR/etc/service/" + +mkdir -p "$DESTDIR/etc/ssh" +cat >"$DESTDIR/etc/ssh/sshd_config"< "$DESTDIR/etc/apt/sources.list" < "$DESTDIR/etc/default/console-setup"<"$DESTDIR/etc/default/keyboard"<"$DESTDIR/etc/default/locale"<>"$DESTDIR/etc/bash.bashrc"<"$DESTDIR/etc/vim/vimrc"<,[,] +set scrolloff=1 +EOF + +rm -r "$DESTDIR/etc/skel" +mkdir -p "$DESTDIR/etc/skel" + +cat > "$DESTDIR/init-stage2.sh" << EOF +#!/bin/sh +export PATH="/usr/sbin:/usr/bin:/sbin:/bin" +mount -no remount,rw / +mount -t proc proc /proc +mount -t sysfs sysfs /sys + +/debootstrap/debootstrap --second-stage + +# second stage may unmount this... +[ -e /proc/mounts ] || mount -t proc proc /proc +[ -e /sys/kernel ] || mount -t sysfs sysfs /sys +busybox mdev -s + +echo '------ mounts:' +cat /proc/mounts +echo '------' + +mv /etc/default/extlinux /etc/default/extlinux.dist +cat >/etc/default/extlinux<~root/.bashrc +echo '. ~/.bashrc' >~root/.bash_profile +echo '. /etc/bash.bashrc' >~calendros/.bashrc +echo '. ~/.bashrc' >~calendros/.bash_profile + +locale-gen en_US.UTF-8 en_GB.UTF-8 en_DK.UTF-8 fr_FR.UTF-8 + +# dhclient may erase /etc/resolv.conf, and is not really useful here +#ip link set eth0 up +#dhclient eth0 +#apt-get update + +echo "Dropping a shell, press Ctrl-D or type exit to finish." +/bin/bash + +rm -v /etc/udev/rules.d/70-persistent-*.rules + +echo "Bootstrap ended, poweroff in 10seconds..." +rm /init-stage2.sh +#sleep 10 || exec /bin/bash # for debug +#poweroff & # does not work +sync +fuser -k / +sleep 1 +mount -o remount,ro / +exit 0 # will panic, but does not matter now (don't know how to shutdown o/w) +EOF +chmod +x "$DESTDIR/init-stage2.sh" + +cat >&2 <