Skip to content

Instantly share code, notes, and snippets.

@drinkcat
Created November 4, 2013 15:13
Show Gist options
  • Select an option

  • Save drinkcat/7303954 to your computer and use it in GitHub Desktop.

Select an option

Save drinkcat/7303954 to your computer and use it in GitHub Desktop.
Resize stateful partition
#!/bin/sh
# Reset partition format (Samsung ARM Chromebook only!)
cgpt add -i 13 -t unused /dev/mmcblk0
cgpt add -i 1 -s 22073344 /dev/mmcblk0
partprobe /dev/mmcblk0
# We could resize2fs here, maybe after next reboot
#!/bin/sh -e
set -e
croutonsizemb="8192"
rootdev="`rootdev -d -s`"
if [ -z "$rootdev" ]; then
echo "Cannot find root device."
exit 1
fi
rootc="`cgpt find -n -l ROOT-C "$rootdev"`"
if [ "`cgpt show -i $rootc -s "$rootdev"`" -gt 1 ]; then
echo "ROOT-C is not empty (Chrubuntu?), aborting."
exit 1
fi
if cgpt find -n -l CROUTON > /dev/null; then
echo "CROUTON partition already exists, aborting."
exit 1
fi
# All sizes/offsets are expressed in sectors (512 bytes)
croutonsize=$((croutonsizemb*1024*2))
statestart="`cgpt show -i 1 -b "$rootdev"`"
statesize="`cgpt show -i 1 -s "$rootdev"`"
newstatesize=$((statesize-croutonsize))
croutonstart=$((statestart+newstatesize))
if [ "$newstatesize" -lt $((1500*2)) ]; then
echo "You must leave at least 1500 MB for the stateful partition."
fi
echo "New sizes:"
echo " stateful partition (Chromium OS): $((newstatesize/(2*1024))) MB"
echo " crouton: $((croutonsize/(2*1024))) MB"
echo "You have 10 seconds to press Ctrl-C to abort..."
sleep 10
stop tty2 || true
# Start a subshell in VT2
(
cd /
clear
set -x
# Switch to VT2
chvt 2
echo "Logging user out..."
dbus-send --system --dest=org.chromium.SessionManager --type=method_call --print-reply \
/org/chromium/SessionManager org.chromium.SessionManagerInterface.StopSession string:"crouton installer"
sleep 5
chvt 2
#FIXME: Add trap somewhere here, to reboot whatever happens
echo "Stopping all services..."
# Stop all services, || true is needed as sometimes services stop
# dependencies before we can stop them ourselves
initctl list | grep process | cut -f1 -d' ' | xargs -I{} stop {} || true
sleep 2
tries=0
while [ "$tries" -lt 10 ]; do
echo "Unmounting stateful partition..."
ok=1
for mnt in `cat /proc/mounts | cut -f 2 -d' ' | \
sed -n -e '/^\/var/p;/^\/home/p;/^\/mnt\/stateful_partition/p;/^\/usr\/local/p'`; do
umount $mnt 2> /dev/null || ok=0
done
if [ -e /dev/mapper/encstateful ]; then
dmsetup remove /dev/mapper/encstateful 2> /dev/null || ok=0
fi
if [ -n "`losetup -a`" ]; then
losetup -D 2> /dev/null || ok=0
fi
# Make sure we stay on tty2
chvt 2
if [ "$ok" = 1 ]; then
break
fi
sleep 1
tries="$((tries+1))"
done
if [ "$tries" = 10 ]; then
echo "Cannot umount stateful partition."
exit 1
fi
cgpt show "$rootdev" -i 1
echo "Resizing stateful partition..."
e2fsck -f -p /dev/mmcblk0p1
resize2fs "${rootdev}p1" "${newstatesize}s"
echo "Updating partition table..."
cgpt add -i 1 -b "$statestart" -s "$newstatesize" -l STATE "$rootdev"
cgpt add -i 13 -t data -b "$croutonstart" -s "$croutonsize" -l CROUTON "$rootdev"
partprobe "$rootdev"
echo "Formatting crouton partition..."
mkfs.ext4 "${rootdev}p13"
sync
echo "Success! Press enter to reboot.."
read line
reboot
) >/dev/tty2 2>&1 </dev/tty2 &
@dnschneid

Copy link
Copy Markdown

Why not save the output of initctl and restart everything? That way we can keep running and continue the installation. Or require a reboot after the rest of the installation takes place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment