WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: [solved] mount usb on startup  (Read 12587 times)

Offline Rdesk

  • Newbie
  • *
  • Posts: 13
[solved] mount usb on startup
« on: March 20, 2011, 09:56:32 AM »
Hi

I need to get a usb-stick to mount on boot, so i can redirect it via rdesktop to a remote desktop.
I modified the .xsession file so that rdesktop opens a remote connection like in a kiosk mode.
For now the .xsession file looks like this

xvesa...
sleep 5
rdesktop -f ip
exec exittc

I want to modify the rdesktop command with the -disk parameter to redirect a usb-device.
I dont know how to mount a usb device...
mount command in .xsession?

thanks for any suggestions!
« Last Edit: March 23, 2011, 01:46:57 PM by Rdesk »

Offline gutmensch

  • Administrator
  • Hero Member
  • *****
  • Posts: 605
  • I can make it disappear, have no fear!
    • remembrance blog
Re: mount usb on startup
« Reply #1 on: March 20, 2011, 03:41:27 PM »
for that purpose it's better to write an udev rule, which mounts your usb stick if it's plugged in or already found in the system. just g00gle around for manuals about writing usb key related udev rules. when I had to write stuff like that I ended up in redirecting for example /mnt with rdesktop - and udev called a script when a mass storage device was attached. this script created a folder with some stick vendor info (like /mnt/INTENSO_USB, which you can get from "udevadm info") and mounted the device on that folder.
If I seem unduly clear to you, you must have misunderstood what I said. (Alan Greenspan)

Offline Rdesk

  • Newbie
  • *
  • Posts: 13
Re: mount usb on startup
« Reply #2 on: March 22, 2011, 02:32:46 AM »
Thanks for the answer!

My problem is that any USB-Key needs to be an specific device entry. So when i plug in an USB-key it wil allways be on /dev/sdd for example. In any tutorials i found i can only mount a specific USB-key with an vendor-id not any USB-key.


Offline gutmensch

  • Administrator
  • Hero Member
  • *****
  • Posts: 605
  • I can make it disappear, have no fear!
    • remembrance blog
Re: mount usb on startup
« Reply #3 on: March 22, 2011, 02:50:14 AM »
If you read about udev, you will see, that it is clearly not their idea to give static ordered device names to devices because it's not useful at all.

You don't need static devices names like /dev/sdd, you only need udev rules, which handle the attachment of mass storage devices, one could look like:
Code: [Select]
BUS=="usb", KERNEL=="sd?1", SYSFS{idVendor}=="0204", SYMLINK+="myusb1", RUN+="/usr/local/bin/usbkeystuff"
which adds a symlink /dev/myusb1 to the device that is attached if the vendor is 0204 and runs a script additionally. There are other identifiers for each device which you can use for your usb keys on plugin.

[edit]
to differ between plugged in devices there are udev variables like %c, %k and you can even set own variables when a rule is matched with the ENV{myvar}="value" key or use it as parameter for your script in RUN.
[/edit]
« Last Edit: March 22, 2011, 03:00:45 AM by gutmensch »
If I seem unduly clear to you, you must have misunderstood what I said. (Alan Greenspan)

Offline Rdesk

  • Newbie
  • *
  • Posts: 13
Re: mount usb on startup
« Reply #4 on: March 22, 2011, 03:42:15 AM »
I think i need to clear it a liitle up.

My problem is that i need to mount a usb-key on boot. The Tinycore-System which i use will be used on different PC´s via PXE-booting, so it can happend that more than one HDD is in the System. So i need that when an usb-key that is insert (not a specific usb-key, different usb-keys will be in use) it will be in a specific place like /dev/sd$, because i want to redirect that directory via rdesktop to a other system( i modified the .xsession to load rdesktop right on boot).

Offline gutmensch

  • Administrator
  • Hero Member
  • *****
  • Posts: 605
  • I can make it disappear, have no fear!
    • remembrance blog
Re: mount usb on startup
« Reply #5 on: March 22, 2011, 04:32:46 AM »
Ok and I think I have to give you a better idea of how to do that ;-)

1. Of course you will have multiple devices, other IDs, some maybe twice, plugging in and plugging out
2. You should setup a script like mentioned in the post before, which is called whenever a device is plugged in or plugged off (the ACTION env of udev tells you whether it has been added or removed)
3. In that script you get the specific device node like /dev/sdd, /dev/sdc1 because udev provides this device name through a variable, when the device is recognized (!you don't have to care if it's sdb or sdz or sdx203, it's in the variable!)
4. You create in that script a mountpoint e.g. mkdir /media/myusb1 and mount the key to that point, to create a unique mountpoint folder you can use udevadm info and get some IDs
5. You should tell rdesktop only to redirect /media with "-r disk:usb=/media" instead of a static device folder as mounting and umounting with udev are dynamic processes - what if the user unplugs the usb in a session? there's still a redirection for your /dev/sda although the new device is /dev/sdb.

If I seem unduly clear to you, you must have misunderstood what I said. (Alan Greenspan)

Offline Rdesk

  • Newbie
  • *
  • Posts: 13
Re: mount usb on startup
« Reply #6 on: March 22, 2011, 10:08:38 AM »
Sorry i dont want to go on your nerves ;) but what happens if i dont know the ID of the stick that is used. For example I use a stick from someone else. I cant modify the script anymore without remaster tinycore all over again. So the Question is can i write a script to mount a usb-key or whatever usb device to specific mount point? Can i just delete iDVendor from the script? Or do i need a identifier like ID or serialnr.?

So my problems are..

1. I dont know the stick that is used, so i have no ID
2. I dont know on what PC its used, so i dont have the nr. of hdd

much thanks for your effort!
« Last Edit: March 22, 2011, 10:14:44 AM by Rdesk »

Offline gutmensch

  • Administrator
  • Hero Member
  • *****
  • Posts: 605
  • I can make it disappear, have no fear!
    • remembrance blog
Re: mount usb on startup
« Reply #7 on: March 22, 2011, 10:33:56 AM »
No prob ;-)

Surely you don't know any ID before it is plugged in. You don't know the device name or path (will it be /dev/sda or /dev/sdz), vendor ID or the UUIDs of the stick, which are builtin. You know absolutely nothing about it. From that fact you must come to the conclusion that you are just not able to create any static device node, which always corresponds to some or any or one special usb device. And now comes udev. When you plug in a USB mass storage device, udev gets a message from the kernel and will act upon it if it has a rule like I mentioned in the post before. It doesn't matter which mass storage device it is. It is one of many possible ones. And since you provided a script with the RUN parameter, udev will call that script e.g. like that:

/usr/local/bin/yourscript %k

%k is a variable, which udev provides to your script! It contains the device you're so desperately searching for. E.g. if you plugged in your device and the kernel recognizes it as /dev/sda, then udev will call your script in that special case like that:

/usr/local/bin/yourscript sda

Udev does that for every device and/or partition! You just cannot know yourself which device gets which device name and if you're thinking it to the end you don't even want to know it. Let the kernel address the devices. Now when you want to have more info about the device to create a unique mountpoint, you could for example call in yourscript a command, which uses the %k parameter to script like that (whereas $1 evaluates to %k or to sda in our special example):
Code: [Select]
$ udevadm info -q all -n /dev/$1
P: /devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda
N: sda
W: 52
S: block/8:0
S: disk/by-id/ata-WDC_WD5000BEVT-22ZAT0_WD-WXNX08S93798
S: disk/by-id/scsi-SATA_WDC_WD5000BEVT-_WD-WXNX08S93798
S: disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0
S: disk/by-id/wwn-0x50014ee25778b7f4
E: UDEV_LOG=3
E: DEVPATH=/devices/pci0000:00/0000:00:1f.2/host0/target0:0:0/0:0:0:0/block/sda
E: MAJOR=8
E: MINOR=0
E: DEVNAME=/dev/sda
E: DEVTYPE=disk
E: SUBSYSTEM=block
E: ID_ATA=1
E: ID_TYPE=disk
E: ID_BUS=ata
E: ID_MODEL=WDC_WD5000BEVT-22ZAT0
E: ID_MODEL_ENC=WDC\x20WD5000BEVT-22ZAT0\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20
E: ID_REVISION=01.01A10
E: ID_SERIAL=WDC_WD5000BEVT-22ZAT0_WD-WXNX08S93798
E: ID_SERIAL_SHORT=WD-WXNX08S93798
E: ID_ATA_WRITE_CACHE=1
E: ID_ATA_WRITE_CACHE_ENABLED=1
E: ID_ATA_FEATURE_SET_HPA=1
E: ID_ATA_FEATURE_SET_HPA_ENABLED=1
E: ID_ATA_FEATURE_SET_PM=1
E: ID_ATA_FEATURE_SET_PM_ENABLED=1
E: ID_ATA_FEATURE_SET_SECURITY=1
E: ID_ATA_FEATURE_SET_SECURITY_ENABLED=0
E: ID_ATA_FEATURE_SET_SECURITY_ERASE_UNIT_MIN=134
E: ID_ATA_FEATURE_SET_SECURITY_ENHANCED_ERASE_UNIT_MIN=134
E: ID_ATA_FEATURE_SET_SECURITY_FROZEN=1
E: ID_ATA_FEATURE_SET_SMART=1
E: ID_ATA_FEATURE_SET_SMART_ENABLED=1
E: ID_ATA_FEATURE_SET_AAM=1
E: ID_ATA_FEATURE_SET_AAM_ENABLED=1
E: ID_ATA_FEATURE_SET_AAM_VENDOR_RECOMMENDED_VALUE=128
E: ID_ATA_FEATURE_SET_AAM_CURRENT_VALUE=128
E: ID_ATA_FEATURE_SET_APM=1
E: ID_ATA_FEATURE_SET_APM_ENABLED=1
E: ID_ATA_FEATURE_SET_APM_CURRENT_VALUE=128
E: ID_ATA_DOWNLOAD_MICROCODE=1
E: ID_ATA_SATA=1
E: ID_ATA_SATA_SIGNAL_RATE_GEN2=1
E: ID_ATA_SATA_SIGNAL_RATE_GEN1=1
E: ID_ATA_ROTATION_RATE_RPM=5400
E: ID_WWN=0x50014ee25778b7f4
E: ID_WWN_WITH_EXTENSION=0x50014ee25778b7f4
E: ID_SCSI_COMPAT=SATA_WDC_WD5000BEVT-_WD-WXNX08S93798
E: ID_PATH=pci-0000:00:1f.2-scsi-0:0:0:0
E: ID_PART_TABLE_TYPE=dos
E: UDISKS_PRESENTATION_NOPOLICY=0
E: UDISKS_PARTITION_TABLE=1
E: UDISKS_PARTITION_TABLE_SCHEME=mbr
E: UDISKS_PARTITION_TABLE_COUNT=8
E: UDISKS_ATA_SMART_IS_AVAILABLE=1
E: DEVLINKS=/dev/block/8:0 /dev/disk/by-id/ata-WDC_WD5000BEVT-22ZAT0_WD-WXNX08S93798 /dev/disk/by-id/scsi-SATA_WDC_WD5000BEVT_WD-WXNX08S93798 /dev/disk/by-path/pci-0000:00:1f.2-scsi-0:0:0:0 /dev/disk/by-id/wwn-0x50014ee25778b7f4

To make it more simple, in yourscript you could just call a mount command like that:

Code: [Select]
mount /dev/$1 /mnt/mymountpoint

See... there is no need to know anything about any device before it is really there. That's were your logical error is... ;-)
If I seem unduly clear to you, you must have misunderstood what I said. (Alan Greenspan)

Offline Rdesk

  • Newbie
  • *
  • Posts: 13
Re: mount usb on startup
« Reply #8 on: March 22, 2011, 11:19:25 AM »
OK, so what i unterstand is that i need to make a udev rule with

BUS=="usb", KERNEL=="sd?1", SYMLINK+="myusb1", RUN+="/usr/local/bin/myscript $k

and then in the script /usr/local/bin/myscript %k

i use the commad mount /dev/$1 /mnt/mymedia

And udev does the work for me, because it recognizes where the USB-media is mounted and put it in the variable!?

Offline Lee

  • Hero Member
  • *****
  • Posts: 645
    • My Core wiki user page
Re: mount usb on startup
« Reply #9 on: March 22, 2011, 11:27:58 AM »
Yet again I find myself wishing for a "like" button here in the forums.  I put a wee bit of effort into just that sort of thing with various scripts using UUIDs or labels - never did come up with a decent solution.

Thanks for the explanation of udev.

32 bit core4.7.7, Xprogs, Xorg-7.6, wbar, jwm  |  - Testing -
PPR, data persistence through filetool.sh          |  32 bit core 8.0 alpha 1
USB Flash drive, one partition, ext2, grub4dos  | Otherwise similar

Offline gutmensch

  • Administrator
  • Hero Member
  • *****
  • Posts: 605
  • I can make it disappear, have no fear!
    • remembrance blog
Re: mount usb on startup
« Reply #10 on: March 22, 2011, 11:40:51 AM »
@Rdesk: almost right, the udev vars are called %k, %n etc. the (in-)script vars are called $1, $2, etc. you don't have to symlink the device, it's just another feature. you really have to read about udev http://reactivated.net/writing_udev_rules.html to get a proper understanding of what's going on there... please spare me the embarrassment of rtfm you :p
udev doesn't mount anything itself and does not know anything about what you are doing in your script with any device, it just provides an abstraction layer for devices on which you can easily work, when the devices are added and removed.

@Lee: Thanks, unfortunately I'm not very convinced of my ability to explain how "mighty" udev can be, but I'm glad that it was at least of some help :)
If I seem unduly clear to you, you must have misunderstood what I said. (Alan Greenspan)

Offline Rdesk

  • Newbie
  • *
  • Posts: 13
Re: mount usb on startup
« Reply #11 on: March 23, 2011, 01:46:31 PM »
Got it done! ;D

Used your script which creates a symlink which i then mount at startup.

Big thanks to you gutmensch!!!

Offline hiro

  • Hero Member
  • *****
  • Posts: 1217
Re: [solved] mount usb on startup
« Reply #12 on: December 17, 2011, 12:08:25 PM »
Which tool can I use to gather the partition label for use in such script?

Offline gutmensch

  • Administrator
  • Hero Member
  • *****
  • Posts: 605
  • I can make it disappear, have no fear!
    • remembrance blog
Re: [solved] mount usb on startup
« Reply #13 on: December 17, 2011, 12:32:32 PM »
the fs label should be queryable through udevadm info as well, e.g. when a device like /dev/md1 is connected you could run
Code: (bash) [Select]
root@petastation:~# udevadm info -q all -n /dev/md1
S: disk/by-label/tinycore
E: DEVNAME=/dev/md1
E: DEVTYPE=disk
E: SUBSYSTEM=block
E: MD_LEVEL=raid1
E: MD_DEVICES=2
E: MD_METADATA=1.2
E: MD_UUID=6a0ca457:5c2f9f96:81c85adb:73642661
E: MD_DEVNAME=1
E: MD_NAME=petastation:1
E: ID_FS_LABEL=tinycore
E: ID_FS_LABEL_ENC=tinycore
E: ID_FS_TYPE=ext4
E: ID_FS_USAGE=filesystem
E: DEVLINKS=/dev/block/9:1 /dev/disk/by-id/md-name-petastation:1 /dev/disk/by-id/md-uuid-6a0ca457:5c2f9f96:81c85adb:73642661 /dev/md/1 /dev/disk/by-uuid/52917cb1-9b94-4384-873b-7ff08dd26b1f /dev/disk/by-label/tinycore
...and ID_FS_LABEL contains the label you could use for your purpose. I don't know if the label is accessible directly via udev vars, but a little scripting is the only thing to do in this case.
If I seem unduly clear to you, you must have misunderstood what I said. (Alan Greenspan)

Offline hiro

  • Hero Member
  • *****
  • Posts: 1217
Re: [solved] mount usb on startup
« Reply #14 on: December 17, 2011, 02:46:55 PM »
My sd card (/dev/mmcblk0p1) doesn't seem to have this info:

Code: [Select]
tc@thinkpad:~$ udevadm info -q all -n /dev/mmcblk0p1
P: /devices/pci0000:00/0000:00:1e.0/0000:15:00.2/mmc_host/mmc0/mmc0:e624/block/mmcblk0/mmcblk0p1
N: mmcblk0p1
S: block/179:1
E: DEVPATH=/devices/pci0000:00/0000:00:1e.0/0000:15:00.2/mmc_host/mmc0/mmc0:e624/block/mmcblk0/mmcblk0p1
E: MAJOR=179
E: MINOR=1
E: DEVNAME=/dev/mmcblk0p1
E: DEVTYPE=partition
E: SUBSYSTEM=block
E: DEVLINKS=/dev/block/179:1