-
Notifications
You must be signed in to change notification settings - Fork 1
SD Card formatting and image mounting
UPDATE: I need to update this article. Aparently setting a low bs setting matters if you are using a magnetic device (classic hard drive). On SD Cards, It's OK to set up up high, in fact, I encourage it! Setting bs=4M on for the dd command will take SIX HOURS (for a 64GB microSD Class 10/U-1) to format and hours to mount the image. So bs=1G to get it done in about 15 minutes if you are using a 32GB or 64GB microSD card. The rest of the details are in this article which I will edit later.
Also, I still haven't done any cross compiling work. So don't take any of my advice on Cross Compiling just yet.
Once you have your image cross compiled and ready to go, or if you have an image that you downloaded and want to use, it's time to mount it to your SD card.
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.)
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.
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/sdeYou 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/PrivateYou 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/sdeDepending 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-C8FDGenerally, an SD card will use the vfat filesystem type, although the image will write an ext4 filesystem on it.
Another command that some people use to see what is on a device, be it mounted or not, is fdisk
$ sudo fdisk -l
WARNING: GPT (GUID Partition Table) detected on '/dev/sda'! The util fdisk doesn't support GPT. Use GNU Parted.
Disk /dev/sda: 1000.2 GB, 1000204886016 bytes
255 heads, 63 sectors/track, 121601 cylinders, total 1953525168 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: 0x00000000
Device Boot Start End Blocks Id System
/dev/sda1 1 1953525167 976762583+ ee GPT
Disk /dev/mapper/ubuntu-root: 991.2 GB, 991151783936 bytes
255 heads, 63 sectors/track, 120500 cylinders, total 1935843328 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: 0x00000000
Disk /dev/mapper/ubuntu-root doesn't contain a valid partition table
Disk /dev/mapper/ubuntu-swap_1: 8547 MB, 8547991552 bytes
255 heads, 63 sectors/track, 1039 cylinders, total 16695296 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: 0x00000000
Disk /dev/mapper/ubuntu-swap_1 doesn't contain a valid partition table
Disk /dev/sdd: 31.2 GB, 31167873024 bytes
64 heads, 32 sectors/track, 29724 cylinders, total 60874752 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: 0x00000000
Disk /dev/sdd doesn't contain a valid partition table
I'm not a fan of fdisk, especially with the whole WARNING: GPT (GUID Partition Table) detected on '/dev/sda'! The util fdisk doesn't support GPT. Use GNU Parted. message. The Ubuntu forums has a thread dedicated to what that means. (What is really means is that they want you to use gdisk which is another version of fdisk, which I've never used.)
It is OK if fdisk reports that a disk doesn't contain a valid partition table. fdisk is supposed to show partitions by the partitions' block addresses. If you need to think about what a block address is, think of your hard drive as a sky scrapper that his home to several large companies, and think of each company as a partition. A company (partition) is located within a specific set of consecutive floors. Some companies may have empty floors (empty disk space) while some floors are occupied (used disk space). This does happen in real life by the way.
For instance, much of the new One World Trade Center in New York is unoccupied as of 2015. Thus, think of those empty floors empty disk partitions. The only time it is OK to partition a partition is of that partition is BLANK! Never repartition a partition that is occupied with data. You will lose it or you won't be able to access that partition. And definitely do not partition the harddrive you are currently using unless you don't want it to work ever again...or at least not function properly.
Partitioning is not a task for new users, and you would be better off only experiementing with it with blank data.
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.
-
ls -l /dev/sd*to list the block devices that are storage devices. -
df -hto list the file partitions that are mounted. - 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 any new windows open, close them.
- 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/sddand 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. -
sudo umount /dev/sdd*to unmount all partitions on/dev/sddand/or the device itself. But don't remove the microSD card. Take note thatumountis spelled with only one "n" in it. - Repeat steps 1 and 2 again. The
dfcommand will not show/dev/sddor any of its partitions (if there were any), but thelscommand will still show/dev/sddas well as any of its partitions (if there are any) still in the/dev/directory. - This will be important for the next step. Type
sudo echo "". The importance of this is so that when thepvcommand that is used in the next step is used, thesudoprompt is not called and exposes your root password or any other wierd behavior. - Assuming you have Pipe Viewer,
pv, installed, run this command to format the SD card:sudo dd if=/dev/zero bs=1G | pv | sudo dd of=/dev/sdd bs=1G. I'd also recommend doing this command inside oftmuxso that you canCONTROL+Bto another tab and do something else while you wait. This is probably the most dangerous step, but I'll explain why at the bottom. Basically, we are tellingdd("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. Usingpv, we can see how much progress has been made, how much time has elapsed during this process, and the rate of which the format is being done. -
syncand remove the microSD card. Thesynccommand 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.
If for some reason the block device doesn't mount up again after a wipe, you may have to mount the device manually.
Also, if dd is still running, be sure to kill it. (sudo kill -9 <pid of the dd command>)
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!
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 bs=4M | pv | sudo dd of=/dev/sdd bs=4MYou use this command
sudo dd if=kodos_linux_armhf-20150121.img bs=4M | pv | sudo dd of=/dev/sdd bs=4Mwhere 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.
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.
- Plug the microSD card into your card reader or adapter. Close any windows that may show up.
-
dd if=/dev/sdd bs="16M" | xz > bill_clinton.img.xzDo not do this with a live system! You will have a bad time!
TODO: This section is under construction
- [cross compiled](Cross-Compiling for the Cowardly Coder)
- [Mounting devices manually](Mounting devices manually)
Mainly some ideas.
- http://go.kblog.us/2012/10/disk-cloning-imaging-over-network-with.html
- http://www.commandlinefu.com/commands/view/1868/watch-the-progress-of-dd
- http://www.commandlinefu.com/commands/view/8708/watch-the-progress-of-dd
- http://www.thegeekstuff.com/2010/10/dd-command-examples/
- http://askubuntu.com/questions/491082/steps-to-create-dd-image-file-from-usb-and-restore-image-to-a-different-usb
- https://wiki.archlinux.org/index.php/disk_cloning
- http://elinux.org/RPi_Resize_Flash_Partitions
- http://archlinuxarm.org/forum/viewtopic.php?f=31&t=3119&start=20
- http://elinux.org/RPi_Easy_SD_Card_Setup
- http://www.armhf.com/boards/odroid-xu/odroid-sd-install/