[gentoo-user] activating swap by udev event

2010-07-01 Thread SpaceCake
Hi,


I would like to increase the speed of my machine by putting some swap on a
fast pendrive. It is working manually by starting a script

swapon -s
swapon -p -1 /dev/sdb1
swapoff /dev/sda6
swapon -p -2 /dev/sda6
swapon -s

but, I would like to make it automatic by creating an udev rule, so when I
plug in that device the swap space is automatically activated and the
priority is changed. I've tried to google for a solution like this, but I
did not find. Maybe you already have some script at hand :) can you please
share this with me?

Thanks
Laszlo


Re: [gentoo-user] activating swap by udev event

2010-07-01 Thread Neil Bothwick
On Thu, 1 Jul 2010 15:49:08 +0200, SpaceCake wrote:

 I would like to increase the speed of my machine by putting some swap
 on a fast pendrive. It is working manually by starting a script
 
 swapon -s
 swapon -p -1 /dev/sdb1
 swapoff /dev/sda6
 swapon -p -2 /dev/sda6
 swapon -s
 
 but, I would like to make it automatic by creating an udev rule, so
 when I plug in that device the swap space is automatically activated
 and the priority is changed.

Create the udev rule in the usual way and add

RUN=/path/to/your/script

You must use a full path when running a program from a udev rule and the
program should exit quickly as udev blocks while it is running.


-- 
Neil Bothwick

What colour is a chameleon on a mirror?


signature.asc
Description: PGP signature


Re: [gentoo-user] activating swap by udev event

2010-07-01 Thread Nils Larsson
tor 2010-07-01 klockan 15:49 +0200 skrev SpaceCake:
 Hi,
 
 
 I would like to increase the speed of my machine by putting some swap
 on a fast pendrive. It is working manually by starting a script
 
 swapon -s
 swapon -p -1 /dev/sdb1
 swapoff /dev/sda6
 swapon -p -2 /dev/sda6
 swapon -s
 
 but, I would like to make it automatic by creating an udev rule, so
 when I plug in that device the swap space is automatically activated
 and the priority is changed. I've tried to google for a solution like
 this, but I did not find. Maybe you already have some script at
 hand :) can you please share this with me?
 
 Thanks
 Laszlo
 

I tried doing something like this awhile ago. The problem I encountered
was that I couldn't disable the swap once the the device was removed,
swapoff couldn't find the device path(as it wasn't there anymore) but
the system thought the swap was still there. So plugging and unplugging
the swap stick got my system thinking it had loads of swap.

But I didn't experiment all that much, maybe there's a way. Tricking
udev to hold on to the device node until swap is disabled might be
possible.




Re: [gentoo-user] activating swap by udev event

2010-07-01 Thread SpaceCake
I've never created udev rule so the usal way does not tell me too much :)
I've checked the net for examples but it looks like very complicated for me
:)

My usb stick looks like this

Bus 007 Device 015: ID 058f:6387 Alcor Micro Corp. Transcend JetFlash Flash
Drive

Can you share with me some example how can I use this info on udev rules?

Thanks
Laszlo


2010/7/1 Neil Bothwick n...@digimed.co.uk

 On Thu, 1 Jul 2010 15:49:08 +0200, SpaceCake wrote:

  I would like to increase the speed of my machine by putting some swap
  on a fast pendrive. It is working manually by starting a script
 
  swapon -s
  swapon -p -1 /dev/sdb1
  swapoff /dev/sda6
  swapon -p -2 /dev/sda6
  swapon -s
 
  but, I would like to make it automatic by creating an udev rule, so
  when I plug in that device the swap space is automatically activated
  and the priority is changed.

 Create the udev rule in the usual way and add

 RUN=/path/to/your/script

 You must use a full path when running a program from a udev rule and the
 program should exit quickly as udev blocks while it is running.


 --
 Neil Bothwick

 What colour is a chameleon on a mirror?



Re: [gentoo-user] activating swap by udev event

2010-07-01 Thread Neil Bothwick
On Thu, 1 Jul 2010 17:17:15 +0200, SpaceCake wrote:

 I've never created udev rule so the usal way does not tell me too
 much :) I've checked the net for examples but it looks like very
 complicated for me :)

http://www.reactivated.net/writing_udev_rules.html

 My usb stick looks like this
 
 Bus 007 Device 015: ID 058f:6387 Alcor Micro Corp. Transcend JetFlash
 Flash Drive
 
 Can you share with me some example how can I use this info on udev
 rules?

SUBSYSTEMS==usb, KERNEL==sd[a-z]1, ATTRS{idVendor}==058f, 
ATTRS{idProduct}==6387, RUN=/path/to/your/script

That's all on one line.

-- 
Neil Bothwick

Like an atheist in a grave: all dressed up and no place to go.


signature.asc
Description: PGP signature


Re: [gentoo-user] activating swap by udev event

2010-07-01 Thread SpaceCake
So, it solves the first problem, identifiying the device, but how can I tell
to udev to use always /dev/sds (for example) for this device? Also I'm
thinking how can I instruct udev to turn off swap when the device is
removed, but this is another story :)

Thanks
L:


2010/7/1 Neil Bothwick n...@digimed.co.uk

 On Thu, 1 Jul 2010 17:17:15 +0200, SpaceCake wrote:

  I've never created udev rule so the usal way does not tell me too
  much :) I've checked the net for examples but it looks like very
  complicated for me :)

 http://www.reactivated.net/writing_udev_rules.html

  My usb stick looks like this
 
  Bus 007 Device 015: ID 058f:6387 Alcor Micro Corp. Transcend JetFlash
  Flash Drive
 
  Can you share with me some example how can I use this info on udev
  rules?

 SUBSYSTEMS==usb, KERNEL==sd[a-z]1, ATTRS{idVendor}==058f,
 ATTRS{idProduct}==6387, RUN=/path/to/your/script

 That's all on one line.

 --
 Neil Bothwick

 Like an atheist in a grave: all dressed up and no place to go.



Re: [gentoo-user] activating swap by udev event

2010-07-01 Thread Bill Longman
On 07/01/2010 08:44 AM, SpaceCake wrote:
 So, it solves the first problem, identifiying the device, but how can I
 tell to udev to use always /dev/sds (for example) for this device? Also
 I'm thinking how can I instruct udev to turn off swap when the device is
 removed, but this is another story :)

Here's what I have in my /etc/fstab file for one of my USB keys. I
assume you could just change it to say swap instead.

fs   mntpt  fs opts  dump/pass
UUID=BA62-89BD /mnt/key auto noauto,user,exec,nosuid 0 0



Re: [gentoo-user] activating swap by udev event

2010-07-01 Thread SpaceCake
that's good if I want to mount at a specific location, but for swap I need
the device name, but this is changes depending on how many other usb drives
are connected. Looks lik this is a tricky question  :)

L:


2010/7/1 Bill Longman bill.long...@gmail.com

 On 07/01/2010 08:44 AM, SpaceCake wrote:
  So, it solves the first problem, identifiying the device, but how can I
  tell to udev to use always /dev/sds (for example) for this device? Also
  I'm thinking how can I instruct udev to turn off swap when the device is
  removed, but this is another story :)

 Here's what I have in my /etc/fstab file for one of my USB keys. I
 assume you could just change it to say swap instead.

 fs   mntpt  fs opts  dump/pass
 UUID=BA62-89BD /mnt/key auto noauto,user,exec,nosuid 0 0




Re: [gentoo-user] activating swap by udev event

2010-07-01 Thread Alex Schuster
SpaceCake writes:

 So, it solves the first problem, identifiying the device, but how can I
 tell to udev to use always /dev/sds (for example) for this device?

I think, you can's. But you can add SYMLINK=swap to make the device 
appear as /dev/swap, too.

 Also I'm thinking how can I instruct udev to turn off swap when the
 device is removed, but this is another story :)

I doubt this is possible at all. If you unplug it, the memory that has 
been swapped there is lost.

Wonko



Re: [gentoo-user] activating swap by udev event

2010-07-01 Thread Nils Larsson
tor 2010-07-01 klockan 08:49 -0700 skrev Bill Longman:
 On 07/01/2010 08:44 AM, SpaceCake wrote:
  So, it solves the first problem, identifiying the device, but how can I
  tell to udev to use always /dev/sds (for example) for this device?

You need to have the udev rule or the script that it runs look at
something specific(the swaplabel for instance).

  I'm thinking how can I instruct udev to turn off swap when the device is
  removed, but this is another story :)

I tried doing exactly what you're doing now awhile ago and this is where
I got stuck, swapoff needs the deivce node(path) to still exist, it
can't disable swap without it. I could never get swapoff to run before
udev removed the device node, so I ended up with the system thinking(or
at least reporting) that it had loads more swap than it actually did.





Re: [gentoo-user] activating swap by udev event

2010-07-01 Thread Neil Bothwick
On Thu, 1 Jul 2010 17:44:24 +0200, SpaceCake wrote:

 So, it solves the first problem, identifiying the device, but how can I
 tell to udev to use always /dev/sds (for example)

I take it you haven't read the link I posted?

BTW, please don't top-post on this list.

-- 
Neil Bothwick

Programmer (n): A red-eyed, mumbling mammal capable of conversing
with inanimate objects.


signature.asc
Description: PGP signature


Re: [gentoo-user] activating swap by udev event

2010-07-01 Thread Neil Bothwick
On Thu, 1 Jul 2010 18:05:46 +0200, Alex Schuster wrote:

  So, it solves the first problem, identifiying the device, but how can
  I tell to udev to use always /dev/sds (for example) for this device?  
 
 I think, you can's. But you can add SYMLINK=swap to make the device 
 appear as /dev/swap, too.

That's a better method anyway, later versions of udev complain if you
try to rename a device to a non-kernel name.


-- 
Neil Bothwick

Guns don't kill people--it's those little pieces of lead.


signature.asc
Description: PGP signature


Re: [gentoo-user] activating swap by udev event

2010-07-01 Thread Albert Hopkins
On Thu, 2010-07-01 at 18:06 +0200, Nils Larsson wrote:
 tor 2010-07-01 klockan 08:49 -0700 skrev Bill Longman:
  On 07/01/2010 08:44 AM, SpaceCake wrote:
   So, it solves the first problem, identifiying the device, but how can I
   tell to udev to use always /dev/sds (for example) for this device?
 
 You need to have the udev rule or the script that it runs look at
 something specific(the swaplabel for instance).
 
   I'm thinking how can I instruct udev to turn off swap when the device is
   removed, but this is another story :)
 
 I tried doing exactly what you're doing now awhile ago and this is where
 I got stuck, swapoff needs the deivce node(path) to still exist, it
 can't disable swap without it. I could never get swapoff to run before
 udev removed the device node, so I ended up with the system thinking(or
 at least reporting) that it had loads more swap than it actually did.

This is a bad thing to do.  If you have pages swapped out to the device
and you remove the device before putting those pages elsewhere then you
have effectively hosed your system.  If it doesn't fail immediately then
as soon as the kernel tries to swap in those pages and finds out the
device it's on can't be accessed then you are in for a world of pain.

I guess the deeper question (although entirely rhetorical AFAIC) is why
would someone want to swap out to a removable device?




Re: [gentoo-user] activating swap by udev event

2010-07-01 Thread Nils Larsson
 I guess the deeper question (although entirely rhetorical AFAIC) is why
 would someone want to swap out to a removable device?

Hot-pluggable swapspace. For various desperate and/or just-in-case
scenarios. One might argue that if you find there you did it wrong in
the first place.





Re: [gentoo-user] activating swap by udev event

2010-07-01 Thread SpaceCake
This is a shame but the idea coming from Windows world. In Win7/Vista there
is a feature called ready boost which I suppose do something similar... or
maybe not :) the main goal is to break the bottleneck of the slow HDD, but
it is maybe a better idea to put some part of the system on a SDHC card
which can reside in my bulting SD slot :) I know, this is also removable :)

L:


2010/7/1 Albert Hopkins mar...@letterboxes.org

 On Thu, 2010-07-01 at 18:06 +0200, Nils Larsson wrote:
  tor 2010-07-01 klockan 08:49 -0700 skrev Bill Longman:
   On 07/01/2010 08:44 AM, SpaceCake wrote:
So, it solves the first problem, identifiying the device, but how can
 I
tell to udev to use always /dev/sds (for example) for this device?
 
  You need to have the udev rule or the script that it runs look at
  something specific(the swaplabel for instance).
 
I'm thinking how can I instruct udev to turn off swap when the device
 is
removed, but this is another story :)
 
  I tried doing exactly what you're doing now awhile ago and this is where
  I got stuck, swapoff needs the deivce node(path) to still exist, it
  can't disable swap without it. I could never get swapoff to run before
  udev removed the device node, so I ended up with the system thinking(or
  at least reporting) that it had loads more swap than it actually did.

 This is a bad thing to do.  If you have pages swapped out to the device
 and you remove the device before putting those pages elsewhere then you
 have effectively hosed your system.  If it doesn't fail immediately then
 as soon as the kernel tries to swap in those pages and finds out the
 device it's on can't be accessed then you are in for a world of pain.

 I guess the deeper question (although entirely rhetorical AFAIC) is why
 would someone want to swap out to a removable device?





Re: [gentoo-user] activating swap by udev event

2010-07-01 Thread Bill Longman
On 07/01/2010 08:59 AM, SpaceCake wrote:
 that's good if I want to mount at a specific location, but for swap I
 need the device name, but this is changes depending on how many other
 usb drives are connected. Looks lik this is a tricky question  :)

No, you don't *NEED* the device name to mount swap, you can use a UUID
or a filesystem label:

# swapon -h

Usage:
 swapon -a [-e] [-v] [-f] enable all swaps from /etc/fstab
 swapon [-p priority] [-v] [-f] special  enable given swap
 swapon -sdisplay swap usage summary
 swapon -hdisplay help
 swapon -Vdisplay version

The special parameter:
 {-L label | LABEL=label} LABEL of device to be used
 {-U uuid  | UUID=uuid}   UUID of device to be used
 device name of device to be used
 file   name of file to be used

Just put

LABEL=cakeswap none swap sw,user 0 0

in your fstab

and use mkswap -L cakeswap on your USB stick.

Certainly, you'd want to use swapoff *before* you removed the stick,
so that's going to have to be a manual step so I think you're stuck
unsticking your stick.





Re: [gentoo-user] activating swap by udev event

2010-07-01 Thread Neil Bothwick
On Thu, 01 Jul 2010 11:59:43 -0700, Bill Longman wrote:

  that's good if I want to mount at a specific location, but for swap I
  need the device name, but this is changes depending on how many other
  usb drives are connected. Looks lik this is a tricky question  :)  
 
 No, you don't *NEED* the device name to mount swap, you can use a UUID
 or a filesystem label:

You have the device name in a UDEV rule, it is /dev/%k.


-- 
Neil Bothwick

deja noo - reminds you of the last time you visited Scotland


signature.asc
Description: PGP signature


Re: [gentoo-user] activating swap by udev event

2010-07-01 Thread Alan McKinnon
On Thursday 01 July 2010 18:06:54 Nils Larsson wrote:
 tor 2010-07-01 klockan 08:49 -0700 skrev Bill Longman:
  On 07/01/2010 08:44 AM, SpaceCake wrote:
   So, it solves the first problem, identifiying the device, but how can I
   tell to udev to use always /dev/sds (for example) for this device?
 
 You need to have the udev rule or the script that it runs look at
 something specific(the swaplabel for instance).
 
   I'm thinking how can I instruct udev to turn off swap when the device
   is removed, but this is another story :)
 
 I tried doing exactly what you're doing now awhile ago and this is where
 I got stuck, swapoff needs the deivce node(path) to still exist, it
 can't disable swap without it. I could never get swapoff to run before
 udev removed the device node, so I ended up with the system thinking(or
 at least reporting) that it had loads more swap than it actually did.
 
It is blindingly obvious what mistake you are both making.

You want to remove the device then disable swap.

That will never work in a zillion years for all the good reasons mentioned 
elsewhere in this thread.

You MUST disable swap THEN remove the device. This sequence is inviolate.


-- 
alan dot mckinnon at gmail dot com



Re: [gentoo-user] activating swap by udev event

2010-07-01 Thread Neil Bothwick
On Thu, 1 Jul 2010 21:46:15 +0200, Alan McKinnon wrote:

 That will never work in a zillion years for all the good reasons
 mentioned elsewhere in this thread.
 
 You MUST disable swap THEN remove the device. This sequence is
 inviolate.

You could use a udev rule to enable the swap, but it has to be removed
manually. But it does raise the question of why you would want swap on a
USB stick in the first place. It must be slower than hard disk swap, and
will wear out the flash memory. Instead of all this messing around, just
increase the size of your swap partition, or add another one.


-- 
Neil Bothwick

Rainbows are just to look at, not to really understand.


signature.asc
Description: PGP signature


Re: [gentoo-user] activating swap by udev event

2010-07-01 Thread Alan McKinnon
On Thursday 01 July 2010 23:50:56 Neil Bothwick wrote:
 On Thu, 1 Jul 2010 21:46:15 +0200, Alan McKinnon wrote:
  That will never work in a zillion years for all the good reasons
  mentioned elsewhere in this thread.
  
  You MUST disable swap THEN remove the device. This sequence is
  inviolate.
 
 You could use a udev rule to enable the swap, but it has to be removed
 manually. But it does raise the question of why you would want swap on a
 USB stick in the first place. It must be slower than hard disk swap, and
 will wear out the flash memory. Instead of all this messing around, just
 increase the size of your swap partition, or add another one.

Or deal with the actual underlying issue which is probably 

get more RAM


-- 
alan dot mckinnon at gmail dot com