|
Lines 1409-1412
Link Here
|
| 1409 |
|
1409 |
|
| 1410 |
</sect2> |
1410 |
</sect2> |
| 1411 |
</sect1> |
1411 |
</sect1> |
|
|
1412 |
|
| 1413 |
<sect1 id="removable-media"> |
| 1414 |
<sect1info> |
| 1415 |
<authorgroup> |
| 1416 |
<author> |
| 1417 |
<firstname>Dan</firstname> |
| 1418 |
<surname>Pelleg</surname> |
| 1419 |
<contrib>Contributed by </contrib> |
| 1420 |
</author> |
| 1421 |
<!-- daniel+handbook@pelleg.org --> |
| 1422 |
</authorgroup> |
| 1423 |
<!-- 26 September 2003 --> |
| 1424 |
</sect1info> |
| 1425 |
|
| 1426 |
<title>Using Removable Memory Devices</title> |
| 1427 |
|
| 1428 |
<para>Many consumer electronic devices now support removable memory |
| 1429 |
cards. They provide a fast and easy way to transfer information to |
| 1430 |
and from a PC. Here we explore the ways to use those cards under |
| 1431 |
&os;. Our goal is to minimize the administrative overhead for the |
| 1432 |
user - eliminating the need for <username>root</username> access or |
| 1433 |
manual mounts. For simple tasks such as uploading files from the |
| 1434 |
card we can even come up with a completely hands-off operation - |
| 1435 |
the user doesn't have to do anything but insert the card in, wait |
| 1436 |
for a while, and then pop it back out.</para> |
| 1437 |
|
| 1438 |
<sect2 id="cf-intro"> |
| 1439 |
<title>Introduction</title> |
| 1440 |
|
| 1441 |
<para>The discussion refers to Compact Flash (CF) cards. However, |
| 1442 |
most of the instructions also apply, with slight modifications, |
| 1443 |
to other types of 'flash' media (SmartMedia, MMC, MemoryStick and |
| 1444 |
all their variations), as well as cameras, MP3 players, and other |
| 1445 |
devices supported by the &man.umass.4 device driver. </para> |
| 1446 |
|
| 1447 |
<para>Compact Flash cards typically interface to a PC in one of |
| 1448 |
two ways: an external USB card-reader, or through the PC-CARD |
| 1449 |
(also known as "PCMCIA") slot. For the PC-CARD case, a cheap |
| 1450 |
adapter allows insertion of CF media to a standard PC-CARD slot. |
| 1451 |
Some laptops have a special CF slot, eliminating the need for an |
| 1452 |
adapter. The way &os; accesses your device will vary, depending |
| 1453 |
on the reader type. PC-CARD readers will show up as ATA disks, |
| 1454 |
while USB readers will show up as SCSI disks.</para> |
| 1455 |
|
| 1456 |
<para>Once the "disks" show up, we can mount the filesystem on |
| 1457 |
them, and then manipulate it in the standard way - copying and |
| 1458 |
deleting files. We explore two approaches to doing this. The |
| 1459 |
first involves a script that handles the specific task of |
| 1460 |
uploading all of the new files from the CF card to the PC, |
| 1461 |
requiring no user intervention. The second is more general, and |
| 1462 |
uses the automounter to mount the filesystem as soon as it is |
| 1463 |
accessed, letting the user directly manipulate it. </para> |
| 1464 |
|
| 1465 |
<para>To make things easy to follow, we first detail the steps |
| 1466 |
for USB readers, and then explain how the same procedures work |
| 1467 |
for PC-CARD devices.</para> |
| 1468 |
|
| 1469 |
</sect2> |
| 1470 |
<sect2 id="cf-usb"> |
| 1471 |
<title>USB-Based Compact-Flash Readers</title> |
| 1472 |
<sect3> |
| 1473 |
<title>Requirements</title> |
| 1474 |
|
| 1475 |
<para>Support for USB readers requires the &man.umass.4 |
| 1476 |
driver. As the manpage tells you, you will need the |
| 1477 |
following options in your kernel config: </para> |
| 1478 |
|
| 1479 |
<programlisting>device usb |
| 1480 |
device ohci (OR device uhci) |
| 1481 |
device umass |
| 1482 |
device scbus |
| 1483 |
device da |
| 1484 |
device pass</programlisting> |
| 1485 |
|
| 1486 |
<para>Note that the GENERIC kernel that comes with 4.8 |
| 1487 |
already includes them all. After recompiling your kernel |
| 1488 |
(or not, if you're using GENERIC), you should be able to |
| 1489 |
plug the reader into the USB slot and have it show up. |
| 1490 |
Usually, the media has to be already in the reader before |
| 1491 |
you plug in the USB connector. So, if things don't work |
| 1492 |
for you, unplug the reader from the USB slot, stick the |
| 1493 |
memory card in, and plug it into USB again.</para> |
| 1494 |
|
| 1495 |
<para>The message log will tell you the device name for |
| 1496 |
the CF card. <devicename>umass</devicename>-attached |
| 1497 |
devices will show up like this:</para> |
| 1498 |
|
| 1499 |
<screen>da0 at umass-sim0 bus 0 target 0 lun 0</screen> |
| 1500 |
|
| 1501 |
<para>indicating that the device name is |
| 1502 |
<devicename>da0</devicename>. Note that if you already |
| 1503 |
have SCSI devices in the system, this might interfere |
| 1504 |
with whatever you have in &man.fstab.5;. To avoid nasty |
| 1505 |
surprises, you should wire down all of your other SCSI |
| 1506 |
devices so their numbering doesn't change.</para> |
| 1507 |
|
| 1508 |
<para>If the reader is supported, at this point you |
| 1509 |
should be able to mount the device. If you are using a |
| 1510 |
card from a standard camera or music player, the |
| 1511 |
following command, typed as root, will mount it under |
| 1512 |
<filename class="directory">/mnt</filename>:</para> |
| 1513 |
|
| 1514 |
<screen>&prompt.root; <userinput>mount -t msdos /dev/da0s1 /mnt</userinput></screen> |
| 1515 |
|
| 1516 |
<para>In theory, at this point you can become |
| 1517 |
<username>root</username> whenever you want and |
| 1518 |
<command>mount</command> the device. However the goal |
| 1519 |
here is to try and avoid this step. The following |
| 1520 |
section and <xref linkend="cf-automount"> describe two |
| 1521 |
different strategies that accomplish this.</para> |
| 1522 |
|
| 1523 |
</sect3> |
| 1524 |
<sect3> |
| 1525 |
<title>Using an upload script</title> |
| 1526 |
|
| 1527 |
<para>Most often, the computer is used as a backing store |
| 1528 |
for the small-capacity CF card. You take a few photos on |
| 1529 |
your digital camera, and want to make sure you have |
| 1530 |
copied them to a "real" disk so you can later process |
| 1531 |
them. We use a script that uploads all the new photos |
| 1532 |
from a CF card into the PC's disk-drive. Once installed, |
| 1533 |
it will go into action as soon as the CF media is |
| 1534 |
attached. It will <command>mount</command> the |
| 1535 |
filesystem, copy the new files over, and then unmount it. |
| 1536 |
The only thing the user has to do is insert the media and |
| 1537 |
later pull it out.</para> |
| 1538 |
|
| 1539 |
<para>You can <link linkend="cf-script">download a copy of the |
| 1540 |
script</link>. We assume you have it installed at |
| 1541 |
<filename>/usr/local/sbin/copy-flash</filename> and |
| 1542 |
marked it as executable.</para> |
| 1543 |
|
| 1544 |
<para>Next, you will need the USB daemon, &man.usbd.8;. |
| 1545 |
Add this line to <filename>/etc/rc.conf</filename>, |
| 1546 |
unless it's already there:</para> |
| 1547 |
|
| 1548 |
<programlisting>usbd_enable="YES"</programlisting> |
| 1549 |
|
| 1550 |
<para>To make the script run when a USB reader is |
| 1551 |
attached, add an entry to |
| 1552 |
<filename>/etc/usbd.conf</filename>. It will look like |
| 1553 |
this:</para> |
| 1554 |
|
| 1555 |
<programlisting>device "CF card" |
| 1556 |
devname "umass[0-9]+" |
| 1557 |
attach "/usr/local/sbin/copy-flash da0 /tmp/cf <replaceable>CFOWNER</replaceable>"</programlisting> |
| 1558 |
|
| 1559 |
<para>where <devicename>da0</devicename> should be |
| 1560 |
substituted by whatever device you see in the message |
| 1561 |
log. Before you use this hook, replace the string |
| 1562 |
CFOWNER. It should be a user name, and the copied files |
| 1563 |
will be owned by that user. This is normally you or the |
| 1564 |
primary user of the machine. Also, create the target |
| 1565 |
directory into which the files will be copied. In this |
| 1566 |
example it is <filename |
| 1567 |
class="directory">/tmp/cf</filename>. Make sure you |
| 1568 |
choose a partition that is big enough to contain all the |
| 1569 |
files you plan on using. Finally, don't forget to kill |
| 1570 |
and restart <command>usbd</command>.</para> |
| 1571 |
|
| 1572 |
<para>The provided script will also sound a short melody |
| 1573 |
once the copying is done. This lets you know it is safe |
| 1574 |
to pull out the CF card. If you don't hear it, add the |
| 1575 |
&man.spkr.4 psuedo-device to your kernel.</para> |
| 1576 |
|
| 1577 |
</sect3> |
| 1578 |
</sect2> |
| 1579 |
<sect2 id="cf-pccard"> |
| 1580 |
<title>PC-CARD readers</title> |
| 1581 |
|
| 1582 |
<para>This section repeats the previous one, this time |
| 1583 |
explaining how to make a PC-CARD slot (or a dedicated CF |
| 1584 |
slot) work.</para> |
| 1585 |
|
| 1586 |
<sect3> |
| 1587 |
<title>Requirements</title> |
| 1588 |
|
| 1589 |
<para>Compared to USB readers, PC-CARD readers are |
| 1590 |
simpler. They only require the &man.pccardd.8 daemon |
| 1591 |
to run. In all likelihood, you already have it. If |
| 1592 |
not, add the following line to |
| 1593 |
<filename>/etc/rc.conf</filename>:</para> |
| 1594 |
|
| 1595 |
<programlisting>pccard_enable="YES"</programlisting> |
| 1596 |
|
| 1597 |
<para>Again, the message log will tell us the name of |
| 1598 |
the device. For example, one |
| 1599 |
<command>pccardd</command>-attached device shows up |
| 1600 |
as:</para> |
| 1601 |
|
| 1602 |
<screen>ad8: 124MB <SAMSUNG CF/ATA> [496/16/32] at ata4-master BIOSPIO</screen> |
| 1603 |
|
| 1604 |
<para>indicating that the device is <devicename>ad8</devicename>.</para> |
| 1605 |
|
| 1606 |
</sect3> |
| 1607 |
<sect3> |
| 1608 |
<title>Using an upload script</title> |
| 1609 |
|
| 1610 |
<para>The <link linkend="cf-script">same script as |
| 1611 |
above</link> can be used for PC-CARD readers. The only |
| 1612 |
difference is what runs it - in this case, it is |
| 1613 |
<command>pccardd</command> instead of |
| 1614 |
<command>usbd</command>. Add this entry to |
| 1615 |
<filename>/etc/pccardd.conf</filename> (create the file |
| 1616 |
if it doesn't exist on your system):</para> |
| 1617 |
|
| 1618 |
<programlisting># GENERIC Flash ATA / ATA HDD |
| 1619 |
generic fixed_disk |
| 1620 |
config 0x1 "ata" ? |
| 1621 |
insert /usr/local/sbin/copy-flash $device /tmp/cf <replaceable>CFOWNER</replaceable> |
| 1622 |
logstr "GENERIC Flash ATA / ATA HDD"</programlisting> |
| 1623 |
|
| 1624 |
<para>Again, change CFOWNER to the name of the user you |
| 1625 |
want to have access to the files.</para> |
| 1626 |
</sect3> |
| 1627 |
</sect2> |
| 1628 |
<sect2 id="cf-automount"> |
| 1629 |
<title>Using the automounter</title> |
| 1630 |
|
| 1631 |
<para>Much of the information in this section is from a |
| 1632 |
<ulink url="http://www.daemonnews.org/200202/automounting.html"> |
| 1633 |
Dæmon News article by Renaud Waldura </ulink>. The |
| 1634 |
automounter can mount filesystems automatically whenever |
| 1635 |
they are accessed. That is, you simply |
| 1636 |
<command>ls</command> or <command>cd</command> to a |
| 1637 |
directory that represents a filesystem, and the |
| 1638 |
automounter intercepts the operation, and makes sure the |
| 1639 |
underlying filesystem is mounted. It is typically used |
| 1640 |
for managing NFS mounts, but here we use it for local |
| 1641 |
mounts.</para> |
| 1642 |
|
| 1643 |
<para>Add these lines to <filename>/etc/amd.map</filename>:</para> |
| 1644 |
|
| 1645 |
<programlisting>localhost type:=auto;fs:=${map};pref:=${key}/ |
| 1646 |
|
| 1647 |
localhost/cf type:=program;fs:=/mnt/cf;\ |
| 1648 |
mount:="/sbin/mount mount /mnt/cf";\ |
| 1649 |
unmount:="/sbin/umount umount /mnt/cf"</programlisting> |
| 1650 |
|
| 1651 |
<para>Next, add a line for the filesystem in |
| 1652 |
<filename>/etc/fstab</filename>. It will look like this, |
| 1653 |
assuming that in your system the reader appears as the |
| 1654 |
device <filename>/dev/ad8</filename>:</para> |
| 1655 |
|
| 1656 |
<programlisting>/dev/ad8s1 /mnt/cf msdos rw,noauto 0 0</programlisting> |
| 1657 |
|
| 1658 |
<para>Note that we specify the first slice on the disk, |
| 1659 |
and the <option>msdos</option> filesystem. Many digital |
| 1660 |
cameras expect their CF cards to be configured in this |
| 1661 |
way. We also need to create the mount point:</para> |
| 1662 |
|
| 1663 |
<screen>&prompt.root; <userinput>mkdir -p /mnt/cf</userinput></screen> |
| 1664 |
|
| 1665 |
<para>To enable the automounter, add this line to |
| 1666 |
<filename>/etc/rc.conf</filename>:</para> |
| 1667 |
|
| 1668 |
<programlisting>portmap_enable=YES |
| 1669 |
amd_enable="YES" |
| 1670 |
amd_flags="-a /.amd_mnt -l syslog /host /etc/amd.map"</programlisting> |
| 1671 |
|
| 1672 |
<para>Once you start <command>amd</command> (or reboot), |
| 1673 |
any user should be able to access the CF card, simply by |
| 1674 |
doing a <command>ls</command> or <command>cd</command> on |
| 1675 |
the directory <filename |
| 1676 |
class="directory">/host/localhost/cf</filename></para> |
| 1677 |
|
| 1678 |
<para>Remember that <command>amd</command> only unmounts |
| 1679 |
the filesystem after it has been idle for some time. |
| 1680 |
This means that before you take the CF card out of the |
| 1681 |
slot, you need to stop using it (remembering to |
| 1682 |
<command>cd</command> out of any directories on it), and |
| 1683 |
wait. Pulling out a mounted device can cause all kinds |
| 1684 |
of trouble, including data loss and system crashes. So |
| 1685 |
you want to make sure you unmount first. If you're in a |
| 1686 |
hurry and can't wait for <command>amd</command> to time |
| 1687 |
out, you can ask it to unmount the filesystem immediately |
| 1688 |
by doing:</para> |
| 1689 |
|
| 1690 |
<screen>&prompt.user; <userinput>amq -u /host/localhost/cf</userinput></screen> |
| 1691 |
|
| 1692 |
<para>This, again, is not a privileged operation and does |
| 1693 |
not require <username>root</username> access.</para> |
| 1694 |
|
| 1695 |
</sect2> |
| 1696 |
<sect2 id="cf-summary"> |
| 1697 |
<title>Summary</title> |
| 1698 |
|
| 1699 |
<para>We eliminated the need for root access to read and |
| 1700 |
write files stored on CF media. We can let the |
| 1701 |
automounter do the mounting for us, or have a script that |
| 1702 |
does all the copying as well. In fact, both of these |
| 1703 |
solutions can co-exist: whenever media is inserted, new |
| 1704 |
files are immediately copied over. If the user later |
| 1705 |
wants to access the card and, say, delete some files, |
| 1706 |
they can do that and <command>amd</command> will take |
| 1707 |
care of mounting.</para> |
| 1708 |
|
| 1709 |
<para>Once you start using the file-copy script, you will |
| 1710 |
probably discover that managing multiple copies of the |
| 1711 |
same data is hard. The original photo is kept on the CF |
| 1712 |
card, the script makes another one on your local disk, |
| 1713 |
and you will also probably make a final copy in your |
| 1714 |
electronic album. To make room and reduce clutter, you |
| 1715 |
will want to delete redundant copies. It is often |
| 1716 |
easiest to do this on the PC where you can easily view |
| 1717 |
the photos on a big screen. The problem is, the files |
| 1718 |
will re-appear the next time you pop the CF card in. One |
| 1719 |
solution is to wipe the CF card clean every time you |
| 1720 |
insert it. But it is wiser to give the good photos |
| 1721 |
another chance, just in case they get lost. A trick I |
| 1722 |
find useful is to create a text file in the directory |
| 1723 |
which stores the files from the CF card, and in that file |
| 1724 |
I record the name of each file that I delete from the PC |
| 1725 |
copy. This way, I can also quickly delete the same files |
| 1726 |
from the CF card whenever it fills up.</para> |
| 1727 |
|
| 1728 |
</sect2> |
| 1729 |
<sect2> |
| 1730 |
<title>Acknowledgments</title> |
| 1731 |
|
| 1732 |
<para>Scott Mitchell provided greatly valuable help for |
| 1733 |
this article. Thanks to Joshua Schachter and Nadav |
| 1734 |
Eiron.</para> |
| 1735 |
</sect2> |
| 1736 |
|
| 1737 |
<sect2 id="cf-script"> |
| 1738 |
<title>Helper Script for Copying Files from CF Card</title> |
| 1739 |
<programlisting>#!/bin/sh |
| 1740 |
# Dan Pelleg, March 2003 |
| 1741 |
# |
| 1742 |
# copy-flash: mount a compact-flash card, copy the contents over, unmount |
| 1743 |
# usage: copy-flash [flash-device] [target-dir] [user] |
| 1744 |
|
| 1745 |
dev=${1:-da6} |
| 1746 |
tgt=${2:-/tmp/foo} |
| 1747 |
user=${3:-root} |
| 1748 |
|
| 1749 |
[ -e "$tgt" ] || mkdir -p $tgt && chown $user $tgt |
| 1750 |
[ -e /mnt/cf ] || mkdir -p /mnt/cf |
| 1751 |
|
| 1752 |
# figure out if we were called with a device like ad8 or ata4. If it's |
| 1753 |
# the latter, figure out the usable device name |
| 1754 |
case ${dev} in |
| 1755 |
ata* ) |
| 1756 |
channel=${dev##ata} |
| 1757 |
|
| 1758 |
if [ -n ${channel} ]; then |
| 1759 |
realdev=$(atacontrol info $channel | grep ^Master | cut -d " " -f 3) |
| 1760 |
fi |
| 1761 |
;; |
| 1762 |
|
| 1763 |
ad* | da* ) |
| 1764 |
realdev=${dev} |
| 1765 |
;; |
| 1766 |
esac |
| 1767 |
|
| 1768 |
if [ -n "${realdev}" ]; then |
| 1769 |
# on -CURRENT we might need to wait a bit before the device node appears |
| 1770 |
if [ ! -e /dev/${realdev} ]; then |
| 1771 |
sleep 5 |
| 1772 |
fi |
| 1773 |
# if the slice doesn't yet exist, try to nudge |
| 1774 |
# things so it does. Might be necessary for some devices |
| 1775 |
# on -CURRENT |
| 1776 |
# This mount is not supposed to succeed, but it does |
| 1777 |
# sometime cause the device node to be created. |
| 1778 |
if [ ! -e /dev/${realdev}s1 ]; then |
| 1779 |
mount -t msdos /dev/${realdev} /mnt/cf && umount /mnt/cf |
| 1780 |
fi |
| 1781 |
mount -t msdos /dev/${realdev}s1 /mnt/cf && \ |
| 1782 |
su ${user} -c "cp -nRp /mnt/cf/ ${tgt}" ; \ |
| 1783 |
# play a tune to let the user know it's all over |
| 1784 |
umount /mnt/cf && echo "MLT250o3fc" > /dev/speaker |
| 1785 |
fi</programlisting> |
| 1786 |
|
| 1787 |
</sect2> |
| 1788 |
|
| 1789 |
</sect1> |
| 1412 |
</chapter> |
1790 |
</chapter> |