Meego Wiki
Views

User:Jketreno/meego-image-resize

From MeeGo wiki
(Difference between revisions)
Jump to: navigation, search
Line 65: Line 65:
  zypper install btrfs-progs
  zypper install btrfs-progs
  btrfs filesystem resize max /
  btrfs filesystem resize max /
-
Your file system should now be resized to the full image size.  
+
Your file system should now be resized to the full image size.
-
 
+
-
zypper install
+

Revision as of 19:28, 4 April 2011

If you download a MeeGo image and would like to run it from a USB stick or MicroSD, you may want to resize the image to use all of the space available on your removable media. This script will resize the downloaded .bin image to whatever size you specify (in M). You can then mount it locally, install packages, etc. before transferring it to your removable media.

#!/bin/bash
TARGET=$1
[ -z ${TARGET} ] && die "Usage: $0 meego-image.bin [size in M]"
shift
SIZE=$1
[ -z ${SIZE} ] && SIZE=7600

function die {
	echo $@
	exit -1
}

function unhook {
	local path=$1
	[ -e "${path}" ] && {
		mount | grep -q "${PWD}/${path}" && {
			sudo umount "${path}" || die "Unable to unmount '${path}'"
		}
		rmdir "${path}" || die "Unable to rmdir '${path}'"
	}
	return 0
}

function hook {
	local path=$1
	local image=$2
	unhook "${path}"
	mkdir "${path}" || die "Unable to create directory '${path}'"
	local VALUE=($(echo -ne "u b\np\nq\n" | parted "${image}" | grep primary))
	local OFFSET=${VALUE[1]/B}
	sudo mount -o loop,offset=${OFFSET} "${image}" "${path}" ||
		die "Unable to mount '${image}' to '${path}'"
	return 0
}

function resize {
	local image=$1
	local size=$2
	local tmp=($(du --apparent-size -B $((1024*1024)) "${image}"))
	local current="${tmp[0]}"
	[ "${current}" != "${size}" ] && {
		echo -n "Settting '${image}' to ${size}M..."
		dd if=/dev/zero of=${image} bs=1M seek=$((size - 1)) count=1 > /dev/null
		local VALUE=($(echo -ne "u b\np\nq\n" | parted "${image}" | grep btrfs\$))
		[ -z "${VALUE[0]}" ] && die "$0 only supports BTRFS images"
		local OFFSET=${VALUE[1]/B}
		echo -e "rm 1\nmkpart primary btrfs ${OFFSET}b -1\nq" | parted "${image}" > /dev/null
		echo "done"
	}

	return 0
}

resize "${TARGET}" 7600

echo -n "Finalizing image resize..."
TMP=.tmp-resize
hook ${TMP} "${TARGET}"
sudo btrfsctl -r ${SIZE}M -A $(mount | sed -ne "s,^\([^ ]*\) .*${PWD}/${TMP}.*$,\1,p")
unhook ${TMP}
echo "done"

NOTE: The above might not actually resize the file system within the disk image. If you mount the drive and find that it is only 2G still, you can fix this by using meego-chroot-sdk to enter the image on your system. Then perform the following:

zypper install btrfs-progs
btrfs filesystem resize max /

Your file system should now be resized to the full image size.

Personal tools