Skip to content

SD Card formatting and image mounting

Jason Charney edited this page Jan 22, 2015 · 22 revisions

Once you have your image cross compiled and ready to go, it's time to mount it to your SD card.

Watch what you buy!

SD Cards come not only in various sizes but various speeds too! That 32 GB SD card you picked up for cheap might not be a Class 10 or U-1! Keep an eye on that number inside that "C". You could be picking up a Class 4 card.

I highly recommend a Class 10 or U-1 SD card or better. If you are using Raspberry Pi or Odroid you must get the MICRO SD CARD VERSION, and I would recommend any adapters that can come with it such as letting you use that microSD card as a Thumbdrive or regular SD card. Be very careful not to lose them as microSD cards are smaller than a dime and SD cards are about the size of a quarter. (It makes you wonder what the CIA uses these days.)

See what's on it

There are a few ways to see what devices are plugged into your computer. Our microSD card is what is know as a block device as it contains a block of memory. Character devices are generally input and output devices like keyboards and mice but we're not interested in that at the moment.

List your Block devices

On Linux our block devices generally will begin with /dev/sdX or /dev/sdXN where X represents a drive letter (if you are familiar with that concept on Windows) and N is a partition number. You could say that the "sd" part means "storage device", now that I think of it.

Let's pull up a list of our storage devices.

$ ls -l /dev/sd*
brw-rw---- 1 root disk 8,  0 Jan 19 22:25 /dev/sda
brw-rw---- 1 root disk 8,  1 Jan 19 22:25 /dev/sda1
brw-rw---- 1 root disk 8,  2 Jan 19 22:25 /dev/sda2
brw-rw---- 1 root disk 8,  3 Jan 19 22:25 /dev/sda3
brw-rw---- 1 root disk 8, 16 Jan 19 22:25 /dev/sdb
brw-rw---- 1 root disk 8, 32 Jan 19 22:25 /dev/sdc
brw-rw---- 1 root disk 8, 48 Jan 21 19:00 /dev/sdd
brw-rw---- 1 root disk 8, 64 Jan 19 22:25 /dev/sde

You may notice not every item on the list has a partition number. Thats OK. Generally the devices that are mounted are the ones that will have a partition number. A partition is a section the storage device. Partitions are generally allocated when we first install our system and it is generally not recommended to repartition (changing the size of a partition) a storage device unless you plan to wipe the disk.

Clearly, disk partitioning is not a task for the faint of heart or for new geeks. Partitions are sequential and are assigned by their block number at a low-level defintion of the drive of which most people really don't think about. High-level would be people doing their everyday stuff on the computer.

That ls command we did will show both mounted and unmounted devices. But if you just want to see the details about your mounted devices, consider this next command.

$ df -hT
Filesystem               Type      Size  Used Avail Use% Mounted on
/dev/mapper/ubuntu-root  ext4      909G  202G  661G  24% /
none                     tmpfs     4.0K     0  4.0K   0% /sys/fs/cgroup
udev                     devtmpfs  3.9G  4.0K  3.9G   1% /dev
tmpfs                    tmpfs     788M  1.5M  787M   1% /run
none                     tmpfs     5.0M     0  5.0M   0% /run/lock
none                     tmpfs     3.9G  355M  3.5G   9% /run/shm
none                     tmpfs     100M   60K  100M   1% /run/user
/dev/sda2                ext2      229M  151M   66M  70% /boot
/dev/sda1                vfat      190M  124K  190M   1% /boot/efi
/home/jrcharney/.Private ecryptfs  909G  202G  661G  24% /home/jrcharney/Private

You may be wondering "Where's /dev/sda3? Technically, /home/jrcharney/.Private is /dev/sda3. I added the -T argument to show the various filesystem types. This will be important later. Let's do this again only this time with the SD Card inserted.

On most Linux Desktop Environments (KDE, GNOME, MATE, and LXDE), the mounting process is automated. You plug in the SD Card and a window shows up. But if you are using something like Fluxbox, you probably won't even see a window. I'll try to talk about manually mounting devices later.

$ ls -l /dev/sd*
brw-rw---- 1 root disk 8,  0 Jan 19 22:25 /dev/sda
brw-rw---- 1 root disk 8,  1 Jan 19 22:25 /dev/sda1
brw-rw---- 1 root disk 8,  2 Jan 19 22:25 /dev/sda2
brw-rw---- 1 root disk 8,  3 Jan 19 22:25 /dev/sda3
brw-rw---- 1 root disk 8, 16 Jan 19 22:25 /dev/sdb
brw-rw---- 1 root disk 8, 32 Jan 19 22:25 /dev/sdc
brw-rw---- 1 root disk 8, 48 Jan 21 21:25 /dev/sdd
brw-rw---- 1 root disk 8, 49 Jan 21 21:25 /dev/sdd1
brw-rw---- 1 root disk 8, 64 Jan 19 22:25 /dev/sde

Depending on your distribution you may see /dev/sdd and/or /dev/sdd1 or some other letter where that second d is.

Next let's look at what df -hT has to say.

$ df -hT
Filesystem               Type      Size  Used Avail Use% Mounted on
/dev/mapper/ubuntu-root  ext4      909G  202G  661G  24% /
none                     tmpfs     4.0K     0  4.0K   0% /sys/fs/cgroup
udev                     devtmpfs  3.9G  4.0K  3.9G   1% /dev
tmpfs                    tmpfs     788M  1.5M  787M   1% /run
none                     tmpfs     5.0M     0  5.0M   0% /run/lock
none                     tmpfs     3.9G  365M  3.5G  10% /run/shm
none                     tmpfs     100M   60K  100M   1% /run/user
/dev/sda2                ext2      229M  151M   66M  70% /boot
/dev/sda1                vfat      190M  124K  190M   1% /boot/efi
/home/jrcharney/.Private ecryptfs  909G  202G  661G  24% /home/jrcharney/Private
/dev/sdd1                vfat       30G   32K   30G   1% /media/jrcharney/5630-C8FD

Generally, an SD card will use the vfat filesystem type, although the image will write an ext4 filesystem on it.

Wiping a (micro)SD Card

If you have your image file ready, and the SD card that you had was previously used, it might be a good idea to wipe it first. I hightly recommend doing this inside a terminal (GNOME Terminal, KTerm, URXVT, etc.) running a terminal multiplexer program such as tmux first. This will be important later when we use a kill command to check on the status of the system wipe.

These instructions assume you have that done and that you don't have an SD card plugged in. Let's begin. These instructions also assume you are not using Windows as your operating system but Linux or BSD.

  1. ls -l /dev/sd* to list the block devices that are storage devices.
  2. df -h to list the file partitions that are mounted.
  3. Insert the microSD card into your computer via a microSD card read or an SD Card adapter or USB adapter that will allow access the microSD card. If a window opens close it.
  4. Repeat steps 1 and 2 but take note of any new devices or partitions in that list. We'll call the device that shows up /dev/sdd and the first partition (if there is one) /dev/sdd1. If there is a /dev/sdd2, this device will likely need to be formatted first or you will need to use a different microSD card. If you have a used card that you are willing to format, continue to step 5.
  5. sudo umount /dev/sdd* to unmount all partitions on /dev/sdd and/or the device itself. But don't remove the microSD card. Take note that umount is spelled with only one "n" in it.
  6. Repeat steps 1 and 2 again. The df command will not show /dev/sdd or any of its partitions (if there were any), but the ls command will still show /dev/sdd as well as any of its partitions (if there are any) still in the /dev/ directory.
  7. Assuming you have tmux running, Control+B " to split the screen in half. Youcan bounce between these windows using Control+B followed by the Up or Down arrow keys. Keep in mind, this happens if you do the arrow commands immediately following the Control+B.
  8. In one of the two windows, sudo dd if=/dev/zero of=/dev/sdd bs=4M & This is probably the most dangerous step, but I'll explain why at the bottom. Basically, we are telling dd ("disk destroyer" as some people call it") write a bunch of zeros as the input file and it will write that to output file which is the entire disk obliterating any partitions on it. The ampersand (&) tells the computer to run this in the background.
  9. In the same window, watch -n5 'sudo pkill -USR1 -n -x dd'. This will trigger the output in the other window to show message update every few minutes on how the dd command is doing.
  10. When the dd reports that can't write to the the disk anymore, you may need to terminate the watch command with a Control+C. (I'll need to review this step. It's been a while.)
  11. sync and remove the microSD card. The sync command will write any data buffered (saved) in the memory out the the disk and make it safe to disconnect the Micro SD card. This will be quick.

dd is DEATHLY DANGEROUS!

As I had previously stated, the very dangerous dd command. Misuse of this command will and can format the hard drive. Even a typo could wipe out some other drive EVEN YOUR HARDDRIVE if you are not careful.

To format an SD card do this command and then go do something else for a while because it will take some time.

Also, it is important that you use the bs (block size) attribute for the dd command. Setting bs to about 4M (four megabytes) is typical for most modern file systems. You could try 16M, but I definitely wouldn’t recommended it. I would recommend setting the bs setting because the default of 512 bytes is very slow!

Writing an image to the file.

The procedure for writing an image to an SD card is very similar to wiping it. Only instead of using this command on step 8:

sudo dd if=/dev/zero of=/dev/sdd bs=4M &

You use this command

sudo dd if=kodos_linux_armhf-20150121.img of=/dev/sdd bs=4M &

where kodos_linux_armhf-20150121.img is the name of the image file you created. (Obviously that is a fake file name. It's what I could come up with on such short notice.)

If it works when plug it in, there should be what looks like a file system set up on the device.

Backing up your system.

dd may be a dangerous command but it is also an important command to use.

You should find time to occasionally back up your file system image by using dd to create a .img file then use something like xz to commpress it.

  1. Plug the microSD card into your card reader or adapter. Close any windows that may show up.
  2. dd if=/dev/sdd bs="16M" | xz > bill_clinton.img.xz Do not do this with a live system! You will have a bad time!

See Also

  • [cross compiled](Cross-Compiling for the Cowardly Coder)
  • [Mounting devices manually](Mounting devices manually)

External Links

Mainly some ideas.

Clone this wiki locally