Difference between revisions of "Repartitioning a CompactFlash Disk for Linux"

From wiki.emacinc.com
Jump to: navigation, search
(Determine which Device to Use: replaced "travis@cheeto")
m (Backup the Existing Filesystem: Fixed grammatical error.)
 
(25 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 +
{{todo|SEOKWREV (12.17.13-00:30->MD+);(12.17.13-15:05->KY+);(01.03.14-19:02->MW+);(01.17.14-13:15->MD-);(03.06.14-15:10->BS-);(04.11.14-12:15->BS+)|Michael Gloff|project=oe 4,oe 5,md,SEOKWREV,ky,mg,bs}}
 +
 +
{{#seo:
 +
|title=Repartitioning a CompactFlash Disk for Linux
 +
|titlemode=append
 +
|keywords=CompactFlash,fdisk,gparted
 +
|description=The majority of EMAC's x86-based systems utilize CompactFlash as the primary storage device.
 +
}}
 
The majority of EMAC's x86-based systems utilize CompactFlash as the primary storage device. By default, EMAC OE is provided with a single ext3 partition on the CompactFlash holding the root filesystem. The most efficient method for repartitioning this type of device is using a CompactFlash card reader on a Linux PC. While command-line tools can be utilized, the application <code>gparted</code> is a graphical application that works very well for resizing existing partitions. Follow the procedure below to resize the existing partition and create a secondary partition on a CompactFlash. The procedure can be modified to generate any partition layout desired.
 
The majority of EMAC's x86-based systems utilize CompactFlash as the primary storage device. By default, EMAC OE is provided with a single ext3 partition on the CompactFlash holding the root filesystem. The most efficient method for repartitioning this type of device is using a CompactFlash card reader on a Linux PC. While command-line tools can be utilized, the application <code>gparted</code> is a graphical application that works very well for resizing existing partitions. Follow the procedure below to resize the existing partition and create a secondary partition on a CompactFlash. The procedure can be modified to generate any partition layout desired.
 
{{mbox | type = notice | text = This procedure requires the <code>gparted</code> utility on the Linux PC being used for partitioning the CF disk. If it is not installed, use the package manager (apt, yum, etc) to install the package.}}
 
{{mbox | type = notice | text = This procedure requires the <code>gparted</code> utility on the Linux PC being used for partitioning the CF disk. If it is not installed, use the package manager (apt, yum, etc) to install the package.}}
 
<br />
 
<br />
{{mbox | type = speedy | text = The commands used in this procedure are can be cause complete and total data loss on the Linux system that they are being run on if the wrong parameters or options are used. Be extremely careful!}}
+
{{mbox | type = speedy | text = The commands used in this procedure can cause complete and total data loss on the Linux system they are being run on if the wrong parameters or options are used. Be extremely careful!}}
 
<br />
 
<br />
  
 
== Setup ==
 
== Setup ==
# Power down the target board and carefully remove the CF disk.
+
<cl>
# Insert the CF disk into the CF reader connected to a Linux PC.
+
1. Power down the target board and carefully remove the CF disk.
# The system will most likely bring up a window asking what to do with the device once it is detected. If this occurs, select ''Do Nothing'' or a similar option to indicate that the device should not be mounted automatically.
+
* Insert the CF disk into the CF reader connected to a Linux PC.
 +
* The system will most likely bring up a window asking what to do with the device once it is detected. If this occurs, select ''Do Nothing'' or a similar option to indicate that the device should not be mounted automatically.
 +
</cl>
  
 
== Determine which Device to Use ==
 
== Determine which Device to Use ==
 
Next you must determine the device node corresponding to the CF disk. There are several methods that can be used to accomplish this as described in the procedure below:
 
Next you must determine the device node corresponding to the CF disk. There are several methods that can be used to accomplish this as described in the procedure below:
# <code>dmesg</code> can provide information about the disk that was reported by the kernel when it was detected as shown below. <syntaxhighlight lang=bash>
+
<cl>
 +
1. <code>dmesg</code> can provide information about the disk that was reported by the kernel when it was detected as shown below.  
 +
<syntaxhighlight lang=console>
 
developer@ldc:~$ dmesg | tail
 
developer@ldc:~$ dmesg | tail
 
[7414822.178026] sd 6:0:0:1: [sdc] 4001760 512-byte logical blocks: (2.04 GB/1.90 GiB)
 
[7414822.178026] sd 6:0:0:1: [sdc] 4001760 512-byte logical blocks: (2.04 GB/1.90 GiB)
Line 18: Line 30:
 
[7414822.180711] sd 6:0:0:1: [sdc] Assuming drive cache: write through
 
[7414822.180711] sd 6:0:0:1: [sdc] Assuming drive cache: write through
 
[7414822.180716]  sdc: sdc1
 
[7414822.180716]  sdc: sdc1
</syntaxhighlight> In this example, <code>sdc</code> is the device corresponding to the CF disk. Also note that the size reported matches the expected size for the CF disk (2 GB in this case). The output also shows that a single partition was detected on the device: <code>sdc1</code>.
+
</syntaxhighlight>
# The <code>fdisk</code> command can also be used to list the available devices using the <code>-l</code> option. An example <code>fdisk -l</code> output is shown below. <syntaxhighlight lang=bash>developer@ldc:~$ sudo /sbin/fdisk -l
+
In this example, <code>sdc</code> is the device corresponding to the CF disk. Also note that the size reported matches the expected size for the CF disk (2 GB in this case). The output also shows that a single partition was detected on the device: <code>sdc1</code>.
 +
* The <code>fdisk</code> command can also be used to list the available devices using the <code>-l</code> option. An example <code>fdisk -l</code> output is shown below.  
 +
<syntaxhighlight lang=console>
 +
developer@ldc:~$ sudo /sbin/fdisk -l
 
Disk /dev/sda: 500.1 GB, 500107862016 bytes
 
Disk /dev/sda: 500.1 GB, 500107862016 bytes
 
255 heads, 63 sectors/track, 60801 cylinders
 
255 heads, 63 sectors/track, 60801 cylinders
Line 45: Line 60:
 
   Device Boot      Start        End      Blocks  Id  System
 
   Device Boot      Start        End      Blocks  Id  System
 
/dev/sdc1  *          1        1008    1999871+  83  Linux
 
/dev/sdc1  *          1        1008    1999871+  83  Linux
</syntaxhighlight> Three devices are listed in this output: <code>/dev/sda</code>, <code>/dev/sdc</code>, and <code>/dev/sdf</code>. Looking at the disk sizes, <code>/dev/sdc</code> matches the 2 GB size of the CF card (2048 MB as listed in the output above).
+
</syntaxhighlight>
# After checking the <code>dmesg</code> and <code>fdisk</code> output, inspect the mount locations on the system to verify that the correct disk is being used and that it has not been mounted by the system. In the example below, the <code>grep</code> command is used to search the output of the <code>mount</code> command for the string "/dev/sd". <syntaxhighlight lang=bash>developer@ldc:~$ mount | grep /dev/sd
+
Three devices are listed in this output: <code>/dev/sda</code>, <code>/dev/sdc</code>, and <code>/dev/sdf</code>. Looking at the disk sizes, <code>/dev/sdc</code> matches the 2 GB size of the CF card (2048 MB as listed in the output above).
developer@ldc:~$ mount | grep /dev/sd
+
 
 +
* After checking the <code>dmesg</code> and <code>fdisk</code> output, inspect the mount locations on the system to verify that the correct disk is being used and that it has not been mounted by the system. In the example below, the <code>grep</code> command is used to search the output of the <code>mount</code> command for the string "/dev/sd".
 +
<syntaxhighlight lang=console>
 +
developer@ldc:~$ mount | grep /dev/sd  
 
/dev/sda1 on / type ext3 (rw,errors=remount-ro)
 
/dev/sda1 on / type ext3 (rw,errors=remount-ro)
 
/dev/sdf1 on /media/backup type ext3 (rw)
 
/dev/sdf1 on /media/backup type ext3 (rw)
</syntaxhighlight> This output illustrates that <code>/dev/sda1</code> is the root filesystem, <code>/dev/sdf1</code> is an auxiliary disk mounted on <code>/media/backup</code>, and <code>/dev/sdc1</code> is not mounted.  
+
</syntaxhighlight>
 
+
This output illustrates that <code>/dev/sda1</code> is the root filesystem, <code>/dev/sdf1</code> is an auxiliary disk mounted on <code>/media/backup</code>, and <code>/dev/sdc1</code> is not mounted.  
 +
</cl>
 
Given the results from these three commands, <code>/dev/sdc</code> is the CF disk. If <code>/dev/sdc1</code> is mounted, it should be unmounted before continuing. <br>'''Do not continue if you are not certain which device node corresponds to the CF disk.'''
 
Given the results from these three commands, <code>/dev/sdc</code> is the CF disk. If <code>/dev/sdc1</code> is mounted, it should be unmounted before continuing. <br>'''Do not continue if you are not certain which device node corresponds to the CF disk.'''
  
== Backup the Existing Filesystem ==
+
== Back Up the Existing Filesystem ==
 
After determining the CF disk on the system, make a backup of the CF disk to ensure that it can be restored if something goes wrong during the repartitioning process. The steps below assume that <code>/dev/sdc</code> is the CF disk as determined in the example above. Replace the <code>/dev/sdc</code> occurrences with the appropriate device for your system.
 
After determining the CF disk on the system, make a backup of the CF disk to ensure that it can be restored if something goes wrong during the repartitioning process. The steps below assume that <code>/dev/sdc</code> is the CF disk as determined in the example above. Replace the <code>/dev/sdc</code> occurrences with the appropriate device for your system.
  
# Begin by mounting the CF disk. It is important to do this manually rather than having the system auto-mount the device to prevent it from being mounted with options that could make it difficult to obtain a complete archive. The listing below demonstrates how to mount the CF disk to a temporary directory: <syntaxhighlight lang=bash>
+
<cl>
developer@ldc:~$ sudo mkdir /tmp/cf
+
1. Begin by mounting the CF disk. It is important to do this manually rather than having the system auto-mount the device to prevent it from being mounted with options that could make it difficult to obtain a complete archive. The listing below demonstrates how to mount the CF disk to a temporary directory:  
developer@ldc:~$ sudo mount /dev/sdc1 /tmp/cf
+
<syntaxhighlight lang=console>
</syntaxhighlight> After mounting, inspect the files at the mount point and verify that the filesystem is there as expected: <syntaxhighlight lang=bash>
+
developer@ldc:~$ sudo mkdir /mnt/cf
developer@ldc:~$ ls /tmp/cf
+
developer@ldc:~$ sudo mount /dev/sdc1 /mnt/cf
 +
</syntaxhighlight>  
 +
After mounting, inspect the files at the mount point and verify that the filesystem is there as expected:  
 +
<syntaxhighlight lang=console>
 +
developer@ldc:~$ ls /mnt/cf
 
bin  boot  dev  etc  home  lib  lost+found  media  mnt  proc  root  sbin  sys  tmp  usr  var
 
bin  boot  dev  etc  home  lib  lost+found  media  mnt  proc  root  sbin  sys  tmp  usr  var
 
</syntaxhighlight>
 
</syntaxhighlight>
# Use tar to create a gzipped tar archive of the entire flash. The following commands will create a backup archive named <code>/home/developer/rootfs-backup/rootfs_20130404.tar.gz</code>: <syntaxhighlight lang=bash>
+
* Use <code>tar</code> to create a gzipped tar archive of the entire flash. The following commands will create a backup archive named <code>/home/developer/rootfs-backup/rootfs_20130404.tar.gz</code>:  
developer@ldc:~$ cd /tmp/cf
+
<syntaxhighlight lang=console>
 +
developer@ldc:~$ cd /mnt/cf
 
developer@ldc:~$ sudo tar czvf ~/rootfs-backup/rootfs_20130404.tar.gz *
 
developer@ldc:~$ sudo tar czvf ~/rootfs-backup/rootfs_20130404.tar.gz *
 
</syntaxhighlight>
 
</syntaxhighlight>
# Unmount the CF disk once the backup is complete:<syntaxhighlight lang=bash>
+
* Unmount the CF disk once the backup is complete:<syntaxhighlight lang=console>
 
developer@ldc:~$ cd
 
developer@ldc:~$ cd
 
developer@ldc:~$ sudo umount /dev/sdc1
 
developer@ldc:~$ sudo umount /dev/sdc1
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
</cl>
  
 
== Repartition the CF Disk using Gparted ==
 
== Repartition the CF Disk using Gparted ==
Line 79: Line 104:
 
{{mbox | type = notice | text =  Note that <code>gparted</code> will not apply any of the changes that are made to the disk partitions until the "Apply" button is pressed.}}
 
{{mbox | type = notice | text =  Note that <code>gparted</code> will not apply any of the changes that are made to the disk partitions until the "Apply" button is pressed.}}
  
# Start the <code>gparted</code> utility, passing the CF disk device node as an argument as shown below:<syntaxhighlight lang=bash>
+
<cl>
 +
1. [[File:Gparted Init.png|right|thumb|500px|Figure 1: Gparted Initial View]]
 +
Start the <code>gparted</code> utility, passing the CF disk device node as an argument as shown below:
 +
<syntaxhighlight lang=console>
 
developer@ldc:~$ sudo gparted /dev/sdc
 
developer@ldc:~$ sudo gparted /dev/sdc
</syntaxhighlight> This should bring up a window similar to the one shown in Figure 1 below. [[File:Gparted Init.png|center|frame|Figure 1: Gparted Initial View]]
+
</syntaxhighlight>
# Click on the partition to select it and press the ''Resize/Move'' button. Enter 200 in the ''New Size (MiB)'' setting in the new window that appears as shown in Figure 2 below. Press ''Resize/Move'' to queue this operation and return to the main window. [[File:Gparted Resize Details.png|center|frame|Figure 2: Gparted Resize Details Dialog]]
+
This should bring up a window similar to the one shown in Figure 1.  
# You should now see a the primary partition followed by a large unallocated section. Although it is acceptable to continue making all changes before applying the changes, it can be advantageous to apply changes in steps. Press the ''Apply'' button to execute the pending operations required to resize the root partition and filesystem. A window will appear with the operations and their status. You may view details for each operation as it is executed. See Figure 3 below for an example. Press ''Close'' to return to the main window once all operations have completed. [[File:Gparted Resize Operations.png|center|frame|Figure 3: Applying Pending Resize Operations in Gparted]]
+
<div style="clear: both"></div>
# To create a new partition in the unallocated portion of the disk, click on the unallocated section to select it and press the ''New'' button. A new window will appear to enter the details for the partition. After entering the settings below (also shown in Figure 4), press ''Add'' to queue the required operations.
+
* [[File:Gparted Resize Details.png|right|thumb|500px|Figure 2: Gparted Resize Details Dialog]]
## Create as: Primary partition
+
Click on the partition to select it and press the ''Resize/Move'' button. Enter 200 in the ''New Size (MiB)'' setting in the new window that appears as shown in Figure 2. Press ''Resize/Move'' to queue this operation and return to the main window.  
## Filesystem: ext3
+
<div style="clear: both"></div>
## Use the maximum allowed size for the New Size setting [[File:Gparted New Details.png|center|frame|Figure 4: New Partitions Details in Gparted]]
+
* [[File:Gparted Resize Operations.png|right|thumb|500px|Figure 3: Applying Pending Resize Operations in Gparted]]
# At the main window the device layout should reflect the new partition as shown below in Figure 5. Press ''Apply'' to commit the changes to the disk. [[File:Gparted New Pending.png|center|frame|Figure 5: New Partition Pending in Gparted]]
+
You should now see the primary partition followed by a large unallocated section. Although it is acceptable to continue making all changes before applying the changes, it can be advantageous to apply changes in steps. Press the ''Apply'' button to execute the pending operations required to resize the root partition and filesystem. A window will appear with the operations and their status. You may view details for each operation as it is executed. See Figure 3 for an example. Press ''Close'' to return to the main window once all operations have completed.
# After all operations finish, verify the device layout and partition table as shown in the main window of <code>gparted</code> . Figure 6 shows that <code>/dev/sdc1</code> is the resized root filesystem and that <code>/dev/sdc2</code> is the new data partition. [[File:Gparted New Complete.png|center|frame|Figure 6: Gparted Window After Creating New Partition]]
+
<div style="clear: both"></div>
# Close <code>gparted</code> and remove the CF disk from the reader.
+
* [[File:Gparted New Details.png|right|thumb|500px|Figure 4: New Partition Details in Gparted]]
 +
To create a new partition in the unallocated portion of the disk, click on the unallocated section to select it and press the ''New'' button. A new window will appear to enter the details for the partition. After entering the settings below (also shown in Figure 4), press ''Add'' to queue the required operations.
 +
i. Create as: Primary partition
 +
* Filesystem: ext3
 +
* Use the maximum allowed size for the New Size setting
 +
<div style="clear: both"></div>
 +
 
 +
* [[File:Gparted New Pending.png|right|thumb|500px|Figure 5: New Partition Pending in Gparted]]
 +
At the main window the device layout should reflect the new partition as shown in Figure 5. Press ''Apply'' to commit the changes to the disk.
 +
<div style="clear: both"></div>
 +
* [[File:Gparted New Complete.png|right|thumb|500px|Figure 6: Gparted Window After Creating New Partition]]
 +
After all operations finish, verify the device layout and partition table as shown in the main window of <code>gparted</code>. Figure 6 shows that <code>/dev/sdc1</code> is the resized root filesystem and that <code>/dev/sdc2</code> is the new data partition.
 +
<div style="clear: both"></div>
 +
* Close <code>gparted</code> and remove the CF disk from the reader.
 +
</cl>
  
 
== Configure the Target Board ==
 
== Configure the Target Board ==
 
After repartitioning the CF disk, it is necessary to configure the target board to tell it where and how to mount the new partition. In this example, the new partition will be mounted on <code>/mnt/data</code> but any mount point may be used.
 
After repartitioning the CF disk, it is necessary to configure the target board to tell it where and how to mount the new partition. In this example, the new partition will be mounted on <code>/mnt/data</code> but any mount point may be used.
# Insert the CF disk into the target board and apply power to the system.
+
<cl>
# Log in as the root user after the system has booted.
+
1. Insert the CF disk into the target board and apply power to the system.
# Verify the root disk and partitioning using the <code>fdisk</code> command as shown below. <syntaxhighlight lang=bash>
+
* Log in as the root user after the system has booted.
root@emac-oe:~# fdisk -l
+
* Verify the root disk and partitioning using the <code>fdisk</code> command as shown below.
 
+
<syntaxhighlight lang=console>
 +
root@emac-oe:~# fdisk -l  
 
Disk /dev/sda: 2048 MB, 2048901120 bytes
 
Disk /dev/sda: 2048 MB, 2048901120 bytes
 
255 heads, 63 sectors/track, 249 cylinders
 
255 heads, 63 sectors/track, 249 cylinders
Line 106: Line 149:
 
/dev/sda1  *          1          25      200781  83 Linux
 
/dev/sda1  *          1          25      200781  83 Linux
 
/dev/sda2              26        249    1799280  83 Linux
 
/dev/sda2              26        249    1799280  83 Linux
</syntaxhighlight> The output above shows that <code>/dev/sda</code> is the CF disk on this system and the new partition scheme was recognized correctly.
+
</syntaxhighlight>  
# Add an entry to the system <code>/etc/fstab</code> file to specify the mount parameters. Note that depending on the system, the new partition may be auto-mounted as a removable storage device. In this case, it was mounted on <code>/media/sda2</code> as shown from the <code>mount</code> command: <syntaxhighlight lang=bash>
+
The output above shows that <code>/dev/sda</code> is the CF disk on this system and the new partition scheme was recognized correctly.
root@emac-oe:~# mount | grep sda2
+
* Add an entry to the system file, <code>/etc/fstab</code>, to specify the mount parameters. Note that depending on the system, the new partition may be auto-mounted as a removable storage device. In this case, it was mounted on <code>/media/sda2</code> as shown from the <code>mount</code> command:  
 +
<syntaxhighlight lang=console>
 +
root@emac-oe:~# mount | grep sda2  
 
/dev/sda2 on /media/sda2 type ext3 (rw,sync,relatime,errors=continue,barrier=1,data=writeback)
 
/dev/sda2 on /media/sda2 type ext3 (rw,sync,relatime,errors=continue,barrier=1,data=writeback)
</syntaxhighlight> Even if the device is auto-mounted, EMAC recommends adding an entry to the <code>fstab</code> to specify the exact parameters for mounting the new partition as described below.
+
</syntaxhighlight>  
## Create the new mount point: <code>/mnt/data</code>. <syntaxhighlight lang=bash>
+
Even if the device is auto-mounted, EMAC recommends adding an entry to the <code>fstab</code> to specify the exact parameters for mounting the new partition as described below.
 +
i. Create the new mount point: <code>/mnt/data</code>.  
 +
<syntaxhighlight lang=console>
 
root@emac-oe:~# mount -o remount,rw /
 
root@emac-oe:~# mount -o remount,rw /
 
root@emac-oe:~# mkdir /mnt/data
 
root@emac-oe:~# mkdir /mnt/data
</syntaxhighlight>
+
</syntaxhighlight>
## Edit the <code>fstab</code> to specify the new mount. For CF disks using ext3, EMAC recommends specifying the following mount parameters: <code>noatime</code>, <code>barrier=1</code>, <code>data=journal</code>. (See the [[EMAC OE Getting Started Guide#Editing a File|EMAC OE Getting Started Guide]] for more information on how to edit a file.) <syntaxhighlight lang=bash>
+
* Edit the <code>fstab</code> to specify the new mount. For CF disks using ext3, EMAC recommends specifying the following mount parameters: <code>noatime</code>, <code>barrier=1</code>, <code>data=journal</code>. (See [[Editing a File|this page]] for more information on how to edit a file.)  
root@emac-oe:~# vi /etc/fstab
+
<syntaxhighlight lang=console>
</syntaxhighlight> Make the following entry in the file from <code>vi</code>: <syntaxhighlight lang=bash>
+
root@emac-oe:~# vi /etc/fstab  
/dev/sda2    /mnt/data    ext3    rw,noatime,barrier=1,data=journal    0 0
+
</syntaxhighlight>  
</syntaxhighlight>
+
Make the following entry in the file from <code>vi</code>:  
## Mount the partition with the new settings and verify that it mounts correctly. <syntaxhighlight lang=bash>
+
<syntaxhighlight lang=console>
 +
/dev/sda2    /mnt/data    ext3    rw,noatime,barrier=1,data=journal    0 0  
 +
</syntaxhighlight>
 +
* Mount the partition with the new settings and verify that it mounts correctly.  
 +
<syntaxhighlight lang=console>
 
root@emac-oe:~# mount -o remount,ro /
 
root@emac-oe:~# mount -o remount,ro /
 
root@emac-oe:~# umount /dev/sda2
 
root@emac-oe:~# umount /dev/sda2
 
root@emac-oe:~# mount /dev/sda2
 
root@emac-oe:~# mount /dev/sda2
root@emac-oe:~# mount | grep sda2
+
root@emac-oe:~# mount | grep sda2  
 
/dev/sda2 on /mnt/data type ext3 (rw,noatime,errors=continue,barrier=1,data=journal)
 
/dev/sda2 on /mnt/data type ext3 (rw,noatime,errors=continue,barrier=1,data=journal)
</syntaxhighlight>
+
</syntaxhighlight>
# Modify any necessary applications to store data on the new partition.
+
* Modify any necessary applications to store data on the new partition.
 
+
</cl>
 
+
<!--
[[Category:Linux]]
 
 
[[Category:Filesystems]]
 
[[Category:Filesystems]]
 +
-->

Latest revision as of 16:02, 10 November 2014

TODO: {{#todo:SEOKWREV (12.17.13-00:30->MD+);(12.17.13-15:05->KY+);(01.03.14-19:02->MW+);(01.17.14-13:15->MD-);(03.06.14-15:10->BS-);(04.11.14-12:15->BS+)|Michael Gloff|oe 4,oe 5,md,SEOKWREV,ky,mg,bs}}

The majority of EMAC's x86-based systems utilize CompactFlash as the primary storage device. By default, EMAC OE is provided with a single ext3 partition on the CompactFlash holding the root filesystem. The most efficient method for repartitioning this type of device is using a CompactFlash card reader on a Linux PC. While command-line tools can be utilized, the application gparted is a graphical application that works very well for resizing existing partitions. Follow the procedure below to resize the existing partition and create a secondary partition on a CompactFlash. The procedure can be modified to generate any partition layout desired.



Setup

  1. Power down the target board and carefully remove the CF disk.

  2. Insert the CF disk into the CF reader connected to a Linux PC.

  3. The system will most likely bring up a window asking what to do with the device once it is detected. If this occurs, select Do Nothing or a similar option to indicate that the device should not be mounted automatically.

Determine which Device to Use

Next you must determine the device node corresponding to the CF disk. There are several methods that can be used to accomplish this as described in the procedure below:

  1. dmesg can provide information about the disk that was reported by the kernel when it was detected as shown below.

    developer@ldc:~$ dmesg | tail
    [7414822.178026] sd 6:0:0:1: [sdc] 4001760 512-byte logical blocks: (2.04 GB/1.90 GiB)
    [7414822.179094] sd 6:0:0:1: [sdc] Assuming drive cache: write through
    [7414822.180711] sd 6:0:0:1: [sdc] Assuming drive cache: write through
    [7414822.180716]  sdc: sdc1
    

    In this example, sdc is the device corresponding to the CF disk. Also note that the size reported matches the expected size for the CF disk (2 GB in this case). The output also shows that a single partition was detected on the device: sdc1.

  2. The fdisk command can also be used to list the available devices using the -l option. An example fdisk -l output is shown below.

    developer@ldc:~$ sudo /sbin/fdisk -l
    Disk /dev/sda: 500.1 GB, 500107862016 bytes
    255 heads, 63 sectors/track, 60801 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Disk identifier: 0x00000000
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *           1       60302   484375783+  83  Linux
    /dev/sda2           60303       60801     4008217+   5  Extended
    /dev/sda5           60303       60801     4008186   82  Linux swap / Solaris
    
    Disk /dev/sdf: 2000.3 GB, 2000398934016 bytes
    255 heads, 63 sectors/track, 243201 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    Disk identifier: 0x0b45dffc
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdf1               1      243201  1953512001   83  Linux
    
    Disk /dev/sdc: 2048 MB, 2048901120 bytes
    64 heads, 62 sectors/track, 1008 cylinders
    Units = cylinders of 3968 * 512 = 2031616 bytes
    Disk identifier: 0x6a5f7029
    
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdc1   *           1        1008     1999871+  83  Linux
    

    Three devices are listed in this output: /dev/sda, /dev/sdc, and /dev/sdf. Looking at the disk sizes, /dev/sdc matches the 2 GB size of the CF card (2048 MB as listed in the output above).

  3. After checking the dmesg and fdisk output, inspect the mount locations on the system to verify that the correct disk is being used and that it has not been mounted by the system. In the example below, the grep command is used to search the output of the mount command for the string "/dev/sd".

    developer@ldc:~$ mount | grep /dev/sd 
    /dev/sda1 on / type ext3 (rw,errors=remount-ro)
    /dev/sdf1 on /media/backup type ext3 (rw)
    

    This output illustrates that /dev/sda1 is the root filesystem, /dev/sdf1 is an auxiliary disk mounted on /media/backup, and /dev/sdc1 is not mounted.

Given the results from these three commands, /dev/sdc is the CF disk. If /dev/sdc1 is mounted, it should be unmounted before continuing.
Do not continue if you are not certain which device node corresponds to the CF disk.

Back Up the Existing Filesystem

After determining the CF disk on the system, make a backup of the CF disk to ensure that it can be restored if something goes wrong during the repartitioning process. The steps below assume that /dev/sdc is the CF disk as determined in the example above. Replace the /dev/sdc occurrences with the appropriate device for your system.


  1. Begin by mounting the CF disk. It is important to do this manually rather than having the system auto-mount the device to prevent it from being mounted with options that could make it difficult to obtain a complete archive. The listing below demonstrates how to mount the CF disk to a temporary directory:

    developer@ldc:~$ sudo mkdir /mnt/cf
    developer@ldc:~$ sudo mount /dev/sdc1 /mnt/cf
    

    After mounting, inspect the files at the mount point and verify that the filesystem is there as expected:

    developer@ldc:~$ ls /mnt/cf
    bin  boot  dev  etc  home  lib  lost+found  media  mnt  proc  root  sbin  sys  tmp  usr  var
    
  2. Use tar to create a gzipped tar archive of the entire flash. The following commands will create a backup archive named /home/developer/rootfs-backup/rootfs_20130404.tar.gz:

    developer@ldc:~$ cd /mnt/cf
    developer@ldc:~$ sudo tar czvf ~/rootfs-backup/rootfs_20130404.tar.gz *
    
  3. Unmount the CF disk once the backup is complete:
    developer@ldc:~$ cd
    developer@ldc:~$ sudo umount /dev/sdc1
    

Repartition the CF Disk using Gparted

At this point you will need to decide on a partition scheme to use. In this example, the primary partition and ext3 filesystem will be resized to 200 MB and a new primary partition will be created. Depending on your application requirements, you could create multiple primary partitions, or an extended partition with multiple logical partitions.

Follow the steps below to repartition the CF disk.


  1. Figure 1: Gparted Initial View

    Start the gparted utility, passing the CF disk device node as an argument as shown below:

    developer@ldc:~$ sudo gparted /dev/sdc
    

    This should bring up a window similar to the one shown in Figure 1.

  2. Figure 2: Gparted Resize Details Dialog

    Click on the partition to select it and press the Resize/Move button. Enter 200 in the New Size (MiB) setting in the new window that appears as shown in Figure 2. Press Resize/Move to queue this operation and return to the main window.

  3. Figure 3: Applying Pending Resize Operations in Gparted

    You should now see the primary partition followed by a large unallocated section. Although it is acceptable to continue making all changes before applying the changes, it can be advantageous to apply changes in steps. Press the Apply button to execute the pending operations required to resize the root partition and filesystem. A window will appear with the operations and their status. You may view details for each operation as it is executed. See Figure 3 for an example. Press Close to return to the main window once all operations have completed.

  4. Figure 4: New Partition Details in Gparted

    To create a new partition in the unallocated portion of the disk, click on the unallocated section to select it and press the New button. A new window will appear to enter the details for the partition. After entering the settings below (also shown in Figure 4), press Add to queue the required operations.

    1. Create as: Primary partition

    2. Filesystem: ext3

    3. Use the maximum allowed size for the New Size setting

  5. Figure 5: New Partition Pending in Gparted

    At the main window the device layout should reflect the new partition as shown in Figure 5. Press Apply to commit the changes to the disk.

  6. Figure 6: Gparted Window After Creating New Partition

    After all operations finish, verify the device layout and partition table as shown in the main window of gparted. Figure 6 shows that /dev/sdc1 is the resized root filesystem and that /dev/sdc2 is the new data partition.

  7. Close gparted and remove the CF disk from the reader.

Configure the Target Board

After repartitioning the CF disk, it is necessary to configure the target board to tell it where and how to mount the new partition. In this example, the new partition will be mounted on /mnt/data but any mount point may be used.

  1. Insert the CF disk into the target board and apply power to the system.

  2. Log in as the root user after the system has booted.

  3. Verify the root disk and partitioning using the fdisk command as shown below.

    root@emac-oe:~# fdisk -l 
    Disk /dev/sda: 2048 MB, 2048901120 bytes
    255 heads, 63 sectors/track, 249 cylinders
    Units = cylinders of 16065 * 512 = 8225280 bytes
    
       Device Boot      Start         End      Blocks  Id System
    /dev/sda1   *           1          25      200781  83 Linux
    /dev/sda2              26         249     1799280  83 Linux
    

    The output above shows that /dev/sda is the CF disk on this system and the new partition scheme was recognized correctly.

  4. Add an entry to the system file, /etc/fstab, to specify the mount parameters. Note that depending on the system, the new partition may be auto-mounted as a removable storage device. In this case, it was mounted on /media/sda2 as shown from the mount command:

    root@emac-oe:~# mount | grep sda2 
    /dev/sda2 on /media/sda2 type ext3 (rw,sync,relatime,errors=continue,barrier=1,data=writeback)
    

    Even if the device is auto-mounted, EMAC recommends adding an entry to the fstab to specify the exact parameters for mounting the new partition as described below.

    1. Create the new mount point: /mnt/data.

      root@emac-oe:~# mount -o remount,rw /
      root@emac-oe:~# mkdir /mnt/data
      
    2. Edit the fstab to specify the new mount. For CF disks using ext3, EMAC recommends specifying the following mount parameters: noatime, barrier=1, data=journal. (See this page for more information on how to edit a file.)

      root@emac-oe:~# vi /etc/fstab
      

      Make the following entry in the file from vi:

      /dev/sda2    /mnt/data    ext3    rw,noatime,barrier=1,data=journal    0 0
      
    3. Mount the partition with the new settings and verify that it mounts correctly.

      root@emac-oe:~# mount -o remount,ro /
      root@emac-oe:~# umount /dev/sda2
      root@emac-oe:~# mount /dev/sda2
      root@emac-oe:~# mount | grep sda2 
      /dev/sda2 on /mnt/data type ext3 (rw,noatime,errors=continue,barrier=1,data=journal)
      
    4. Modify any necessary applications to store data on the new partition.