View Single Post
  #1   (View Single Post)  
Old 6th February 2009
JMJ_coder JMJ_coder is offline
VPN Cryptographer
 
Join Date: May 2008
Posts: 464
Default HOWTO: reformat a USB flash drive in NetBSD

HOWTO: reformat a USB flash drive in NetBSD
  • 1.1 Abstract
  • 1.2 Create a new NetBSD partition
  • 1.3 Create the label for the new partition
  • 1.4 Create a new filesystem on the new partition
  • 1.5 Modify any necessary system configurations to accomodate the new setup


1.1 Abstract

This tutorial will demonstrate how to go about reformatting your USB flash drive. It will particularly show how to reformat it with a NetBSD FFS filesystem, but will work for any filesystem, including back to a MSDOS FAT filesystem.

There are numerous reasons why one would want to do this. One example is that this is a key step in creating a NetBSD system installed onto and from a USB drive and boot from it (although there are a couple more steps involved in preparing the drive, most notably installing and activating the MBR). Another reason is that it offers a layer of security as if someone tried to steal and read your files on a Windows system, they might give up when Windows cannot recognize the file format.

In the same light, do not do this if you need this to work on a Windows system, as Windows does not recognize Unix filesystems natively. Also, be sure to read the manpages for fdisk(8), disklabel(8), and newfs(8).


1.2 Create a new NetBSD partition

The first step is to delete the MSDOS partition and create the new NetBSD partition. The USB drives are usually designated as sd0:

Code:
umass0 at uhub6 port 2 configuration 1 interface 0
umass0: SanDisk U3 Cruzer Micro, rev 2.00/2.00, addr 2
umass0: using SCSI over Bulk-Only
scsibus0 at umass0: 2 targets, 2 luns per target
sd0 at scsibus0 target 0 lun 0: <SanDisk, Cruzer, 8.01> disk removable
sd0: 7657 MB, 61260 cyl, 8 head, 32 sec, 512 bytes/sect x 15682559 sectors
cd1 at scsibus0 target 0 lun 1: <SanDisk, Cruzer, 8.01> cdrom removable
They contain 4 partitions named a, b, c, d, which fdisk calls 0, 1, 2, 3 (this corresponds to the four primary partitions of an usual hard drive). We are going to create the NetBSD partition on the a partition.

Code:
# fdisk -u /dev/rsd0d
Disk: /dev/rsd0d
NetBSD disklabel disk geometry:
cylinders: 61260, heads: 8, sectors/track: 32 (256 sectors/cylinder)
total sectors: 15682559

BIOS disk geometry:
cylinders: 976, heads: 255, sectors/track: 63 (16065 sectors/cylinder)
total sectors: 15682559

Do you want to change our idea of what BIOS thinks? [n] n

Partition table:
0: Primary DOS with 32 bit FAT (sysid 11)
    start 44, size 15679396 (7656 MB, Cyls 0/0/45-975)
        PBR is not bootable: All bytes are identical (0x00)
1: <UNUSED>
2: <UNUSED>
3: <UNUSED>
No active partition.
Which partition do you want to change?: [none] 0
The data for partition 0 is:
Primary DOS with 32 bit FAT (sysid 11)
    start 44, size 15679396 (7656 MB, Cyls 0/0/45-975)
        PBR is not bootable: All bytes are identical (0x00)
sysid: [0..255 default: 11] 169
start: [0..976cyl default: 44, 0cyl, 0MB] [RETURN]
size: [0..976cyl default: 15679396, 976cyl, 7656MB] [RETURN]
bootmenu: [] [RETURN]
The bootselect code is not installed, do you want to install it now? [n] n

Partition table:
0: NetBSD (sysid 169)
    start 44, size 15679396 (7656 MB, Cyls 0/0/45-975)
        PBR is not bootable: All bytes are identical (0x00)
1: <UNUSED>
2: <UNUSED>
3: <UNUSED>
No active partition.
Which partition do you want to change?: [none] [RETURN]

Installed bootfile doesn't support required options.
Update the bootcode from /usr/mdec/mbr? [n] [RETURN]

We haven't written the MBR back to disk yet.  This is your last chance.
Partition table:
0: NetBSD (sysid 169)
    start 44, size 15679396 (7656 MB, Cyls 0/0/45-975)
        PBR is not bootable: All bytes are identical (0x00)
1: <UNUSED>
2: <UNUSED>
3: <UNUSED>
No active partition.
Should we write new partition table? [n] y
The big thing to note is that the partition sysid for NetBSD is 169. You can get a complete list of these using fdisk -l. The main ones of interest for *BSD are:
Code:
159 BSDI?
165 FreeBSD or 386BSD or old NetBSD
166 OpenBSD
169 NetBSD
183 BSDI BSD/386 filesystem
184 BSDI BSD/386 swap

1.3 Create the label for the new partition

The next step is to create the disklabel slice(s). The standard partition letter for USB drives is 'e' in NetBSD. (At least my SanDisk Micro's are always 'e'). You can make this any letter from 'a' to 'p', except 'c' or 'd'. If you have another USB drive that you're using another filesystem on (say a MSDOS one for working with Windows machines) it may be helpful to choose another letter.

If you want to delete a partition, just set the filesystem type to unset its start offset and size to 0.

Note: The 'c' (NetBSD partition) and 'd' (whole disk) partitions are needed in NetBSD - don't delete them!

Code:
# disklabel -i -I sd0
partition> i
Filesystem type [?] [MSDOS]: 4.2BSD
Start offset ('x' to start after partition 'x') [0c, 0s, 0M]: [RETURN]
Partition size ('$' for all remaining) [61260c, 15682559s, 7657.5M]: $
partition> W
Label disk [n]? y
Label written
partition> Q
Observe that the NetBSD filesystem type is listed here as 4.2BSD.


1.4 Create a new filesystem on the new partition

This will create and format the new filesystem on the partition created through disklabel.

Code:
# newfs /dev/rsd0i

1.5 Modify any necessary system configurations to accomodate the new setup

At this point, you can modify any system settings that you deem necessary. Probable changes will be a new mount point in /mnt, such as /mnt/usb_ffs. You might also want to add a new entry to your /etc/fstab. Such an entry might look like this (I have in another entry for a MSDOS partitioned drive) :

Code:
/dev/sd0e     /mnt/usb         msdos  rw,nosuid,nodev,noauto 0 0 
/dev/sd0i     /mnt/usb_ffs     ffs    rw,nosuid,nodev,noauto 0 0
For a good tutorial on mounting USB flash drives as a non-root user, read this HOWTO.
__________________
And the WORD was made flesh, and dwelt among us. (John 1:14)