Skip to content

Instantly share code, notes, and snippets.

@darkmuggle
Created March 31, 2016 16:28
Show Gist options
  • Save darkmuggle/49b827d53290a1fe24bd779d200822dc to your computer and use it in GitHub Desktop.
Save darkmuggle/49b827d53290a1fe24bd779d200822dc to your computer and use it in GitHub Desktop.
#!/bin/bash
local_img="/tmp/squasher.qcow2"
mountpoint="/mnt/new_mnt"
url="<URL>"
bash <<EOM
mkdir -p /run/old_root
mkdir -p $mountpoint
apt-get -y update
apt-get -y install qemu-utils cloud-utils
swapoff -a
mount -t tmpfs none /tmp
mount -t tmpfs none /mnt
wget -O /mnt/dirty.img http://cloud-images.ubuntu.com/wily/current/wily-server-cloudimg-amd64-disk1.img
wget -O ${local_img} ${url}
qemu-img resize /mnt/dirty.img +10G
modprobe nbd
qemu-nbd -c /dev/nbd0 /mnt/dirty.img
e2fsck -yf /dev/nbd0p1
resize2fs /dev/nbd0p1
# create new root
mkdir -p /mnt/new_mnt
mount /dev/nbd0p1 "$mountpoint"
mount --bind /dev "$mountpoint/dev"
mount --bind /run "$mountpoint/run"
mount devpts-live -t proc "$mountpoint/dev/pts"
mount proc-live -t proc "$mountpoint/proc"
mount sysfs-live -t sysfs "$mountpoint/sys"
mount -t tmpfs none "$mountpoint/tmp"
EOM
# Switch to new root
cp -v /tmp/*qcow2 "$mountpoint/tmp"
cd $mountpoint
pivot_root . run/old_root
exec chroot . /bin/bash
lsb_release -a
# Strip the old stuff away
bash <<EOM
umount /run/old_root -lf
wipefs --force --all /dev/sda1
wipefs --force --all /dev/sda2
dd if=/dev/zero of=/dev/sda bs=1M count=10
sync
EOM
# Copy the new image in place
bash <<EOM
qemu-nbd -c /dev/nbd10 ${local_img}
dd if=/dev/nbd10 of=/dev/sda bs=1M
sync
EOM
# Now panic it and reboot
bash <<EOM
echo 1 > /proc/sys/kernel/sysrq
sync
echo u > /proc/sysrq-trigger
echo b > /proc/sysrq-trigger
EOM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment