Meego Wiki
Views

User:Jketreno/meego-image-resize

From MeeGo wiki
< User:Jketreno(Difference between revisions)
Jump to: navigation, search
m (moved ~jketreno/meego-image-resize to User:Jketreno/meego-image-resize: move to user page namespace)
 
(4 intermediate revisions not shown)
Line 1: Line 1:
 +
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
  #!/bin/bash
  TARGET=$1
  TARGET=$1
Line 57: Line 58:
  TMP=.tmp-resize
  TMP=.tmp-resize
  hook ${TMP} "${TARGET}"
  hook ${TMP} "${TARGET}"
-
  sudo btrfsctl -r ${SIZE}M -A $(mount | sed -ne "s,^\([^ ]*\) .*${PWD}/${TMP}.*$,\1,p")
+
  sudo btrfs filesystem resize max "${TARGET}".*${PWD}/${TMP}.*$,\1,p")
  unhook ${TMP}
  unhook ${TMP}
  echo "done"
  echo "done"
 +
 +
'''NOTE:'''  To perform the above you need the btrfs utilities.

Latest revision as of 10:53, 26 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 btrfs filesystem resize max "${TARGET}".*${PWD}/${TMP}.*$,\1,p")
unhook ${TMP}
echo "done"

NOTE: To perform the above you need the btrfs utilities.

Personal tools