#############
Update 14 nov 2013
Create and setup an SD card
##############

# Quick start
- use gwcard to:
- partition and format SD card
- install boot, rootfs and data partitions
- insert sd card into bbone and boot
- login as root and run gwsetup [-f <archive>]

#############
Older
##############

Create a virtual disk and use it to make a bootable SD card
	•	Shut down Linux guest VM
	•	Open Linux guest VM settings > Hard Disk > Add Device... 
	•	Set Disk Size
	•	To use the entire target device (SD Card, USB stick), use same capacity as targeted device , e.g. 4.0 GB, 8.0 GB, 16.0 etc.
	•	For testing, may use smaller capacity (will take up less host disk space and less time to transfer image to device)
	•	Configure Advanced options
	•	select IDE or SCSI (either OK)
	•	select Pre-allocate disk space
	•	de-select Split into 2 GB files
	•	Click Apply
	•	Choose name and path for virtual disk file
	•	Restart the Linux guest VM

The new disk icon should appear among the devices as a hard disk in the VM devices toolbar at the top of the VM window A device node will automatically created (e.g. /dev/sda) in the Linux VM, but will not be mounted automatically. It will have a name like /dev/sd<X>, where <X> is a,b, etc, but will not have any partitions listed (e.g. /dev/sda1). Use the mount command (no arguments) to determine which volumes are currently mounted; any current mounts are NOT the new virtual disk. Be careful not to partition or format the volume used by the Linux VM.  
Partition and format virtual disk as SD card
Before installing files, the virtual disk must be partitioned and formatted. This may vary depending on what Linux distribution is being installed. In this example, we'll create three partitions and format them as :
	•	A FAT32 boot partition (beagleboot) that will contain the bootloaders (MLO and u-boot) and kernel image (uImage.bin)
	•	An ext4 partition (beagleroot) to contain the linux root file system (rootfs)
	•	An ext4 partition (beagledata) for application data
For this example, we'll assume a 2 GB virtual disk on /dev/sda. As root (or use sudo), do the following:
$ sudo fdisk /dev/sda

Print the partition table :
Command (m for help): p
 Disk /dev/sda: 2147 MB, 2147483648 bytes
255 heads, 63 sectors/track, 261 cylinders, total 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x9006ae65

   Device Boot      Start         End      Blocks   Id  System

If the disk size is correct and there is no partition table, proceed. Otherwise press 'q' to quit fdisk.
Create a 64 MB boot partition
	•	'n' - Create a new partition
	•	'p' - Type primary
	•	'1' - Partition 1
	•	<Enter> - Accept default start sector
	•	'+64M' - Specify partition size
	•	't' - set partition type
	•	'1' - Partition 1
	•	'c' - Type 'c' [WFAT32 LBA]

Now the partition table should include the new partition:
Device Boot      Start     End      Blocks   Id  System
/dev/sda1   *    2048      133119   65536    c  W95 FAT32 (LBA)

Create a 256 MB (or suitable size) rootfs partition
	•	'n' - Create a new partition
	•	'p' - Type primary
	•	'2' - Partition 2
	•	<Enter> - Accept default start sector
	•	'+256M' - Specify partition size 
Now if you print the partition table, it should look something like this:  
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      133119       65536    c  W95 FAT32 (LBA)
/dev/sda2          133120      657407      262144   83  Linux

Use the remainder of the disk for application data on a Linux partition
	•	'n' - Create a new partition
	•	'p' - Type primary
	•	'3' - Partition 3
	•	<Enter> - Accept default start sector
	•	<Enter> - Accept default end sector
Now if you print the partition table, it should look something like this:
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      133119       65536    c  W95 FAT32 (LBA)
/dev/sda2          133120      657407      262144   83  Linux
/dev/sda3          657408     4194303     1768448   83  Linux

To exit without modifying the partition table, press 'q', otherwise... 
Note: the next step will (for practical purposes) delete all of the data on the disk. Press 'w' to write the changes to the partition table and exit fdisk.
Install boot and rootfs files on the virtual SD card 
Notes: the boot and rootfs images created by oe/bitbake are somewhere like 
oe/build/tmp-angstrom_v2012_05-eglibc/deploy/images/beaglebone
which contains symlinks to MLO, uBoot.img and the rootfs images.
The files are installed by mounting virtual SD card partitions on the Ubuntu VM guest, then copying the files from the openEmbedded/bitbake build directories to the mounted partitions.
- Mount the boot and root partitions:
$ sudo mount -t msdosfs /dev/sda1 /mnt/beagleboot
$ sudo mount /dev/sda2 /mnt/beagleroot
- Copy MLO+uBoot to the boot partion (beagleboot):
$ cp /path/to/oe/build/tmp-angstrom_v2012_05-eglibc/deploy/images/beaglebone/MLO /mnt/beagleboot [MLO must be the first file copied]
$  cp /path/to/oe/build/tmp-angstrom_v2012_05-eglibc/deploy/images/beaglebone/u-boot.img /mnt/beagleboot 
- Unpack the rootfs image to the root partition (beagleroot):
$ cd /mnt/beagleroot 
$ sudo tar jxvf /path/to/oe/build/tmp-angstrom_v2012_05-eglibc/deploy/images/beaglebone/imageName-beaglebone.tar.bz2
- Unmount the mounted virtual SD card partitions:
$ cd /mnt
$ sudo unmount /dev/sda1
$ sudo unmount /dev/sda2
- Use dd to write the virtal disk image to a shared drive on the mac host
$ sudo dd if=/dev/sda of=/mnt/hgfs/headley/ubuntu/sdcard-ide2G-03feb13.img
Alterntively, you can transfer the disk image directly from the virtual SD card to a physical one from the Ubuntu guest VM in one of two ways: either by writing to a USB card reader attached to the Ubuntu guest VM:
- Plug in USB card reader to mac host OS (prompt: connect to linux/mac?)
- Choose to connect to Linux (Ubuntu guest VM)
- Use dmesg to find the card reader (/dev/sdd)
- Insert a micro SD card and use mount command to find mount point ( /media/9016-4EF8, e.g.)
$ mount
...
vmware-vmblock on /run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)

unmount the SD card
	$ sudo umount /dev/sdd1
use dd to transfer the image
	$ sudo dd if=/dev/sda of=/dev/sdd
Or write to the mac (host OS) built in card reader by making by inserting an SD card, unmounting it, and using sharing the volume with the Ubuntu guest VM
Use dd to write SD card [mac]
Insert SD card in slot or card reader
Use diskutil as root to find device:
$ diskutil list
/dev/disk0
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *500.3 GB   disk0
   1:                        EFI                         209.7 MB   disk0s1
   2:                  Apple_HFS mbari1633               499.4 GB   disk0s2
   3:                 Apple_Boot Recovery HD             650.0 MB   disk0s3
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *1.0 TB     disk1
   1:                        EFI                         209.7 MB   disk1s1
   2:                  Apple_HFS SuperDuper              500.3 GB   disk1s2
   3:                  Apple_HFS TimeMachine             499.4 GB   disk1s3
/dev/disk2
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:     FDisk_partition_scheme                        *2.0 GB     disk2
   1:                 DOS_FAT_16 NO NAME                 2.0 GB     disk2s1 
Unmount the SD card volume:

$ sudo diskutil unmountDisk /dev/disk2
Password:
Unmount of all volumes on disk2 was successful 
Use dd to write the image from the mac (host OS) to the SD card

$ sudo dd if=sdcard-ide2G.img of=/dev/disk2  
[linux] The process to do this under Linux is very similar except that it's not required to un-mount the drive before using the 'dd' command and the commands are a little different. For example, you would use "fdisk -l" instead of "diskutil list", your device node would be located at "/dev/sda" instead of "/dev/disk" and the un-mount command is "umount" instead of "diskutil unmountDisk".
[Resizing data partition]
Use a 2GB basic image with boot, rootfs and data partitions to make a new SD card, then resize the data partition (before putting data on it!) to enable using all the remaining space on the SD card for data.

Prerequisite: virtual 2GB SD card image with boot, rootfs and data partitions

# Unmount the physical (e.g, /dev/sda) and virtual (e.g, /dev/sda) SD cards:
$ sudo umount /dev/sda
$ sudo umount /dev/sdd

# Copy Virtual (2G) SD card to real (4G) SD card
# (using dd as root - CHECK DEVICE NAMES)
$ sudo dd if=/dev/sda of=/dev/sdd bs=1M conv=noerror
2048+0 records in
2048+0 records out
2147483648 bytes (2.1 GB) copied, 368.537 s, 5.8 MB/s

# Use fdisk as root to resize physical SD card data partition (e.g., sdd3):
$ sudo fdisk /dev/sdd3
d (delete partition)
3 (thrird partition)
n (create new partition)
p (primary)
<accept defaults for partition number, start/end sectors>
[optional: p to print partition table]
w (write partition and exit)
partition table should look something like:

Disk /dev/sdd: 3904 MB, 3904897024 bytes
121 heads, 62 sectors/track, 1016 cylinders, total 7626752 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x9006ae65

   Device Boot      Start         End      Blocks   Id  System
/dev/sdd1   *        2048      133119       65536    c  W95 FAT32 (LBA)
/dev/sdd2          133120     3205119     1536000   83  Linux
/dev/sdd3         3205120     7626751     2210816   83  Linux

# use mke2fs to create ext4 filesystem on the new partition
# options: t (fs type), j (create journal), L (volume label), -O option (largefile)

sudo /sbin/mke2fs -t ext4 -j -L xfdata -O large_file /dev/sdd3