set -e
dev=$1
luks=$(mktemp)
cryptdev=$(basename $dev)_crypt
pvsize=$(pvs -o pe_start,pv_size --units s --noheadings --nosuffix "$dev" awk ' print $1 + $2 ')
devsize=$(blockdev --getsz "$dev")
mchunk=8
The script takes the physical volume device to convert as an argument. Note there is no check for the validity of its value.
dd of="$luks" seek=$devsize count=0 bs=512 2> /dev/null
luksdev=$(losetup -f)
losetup "$luksdev" "$luks"
trap "losetup -d \"$luksdev\"; rm -f \"$luks\"" EXIT
Next, we create a sparse luks file the same size as the device (in case luksFormat would use the size somehow, but I believe it doesn’t), and a loopback device on this file. The trap is here to avoid leaving the loopback device and the file when an error occurs later (though during the conversion itself, it will be pointless).
cryptsetup luksFormat -q "$luksdev"
cryptsetup luksOpen "$luksdev" $ cryptdev _real
read start length crypt format key IVoff cdev offset <<EOF
$(dmsetup table $ cryptdev _real)
EOF
We create a LUKS device, so that we can get the encryption key ($key), and the size of the LUKS header ($offset). Note you need to add –showkey to the dmsetup table command on sid.
if [ $(expr $devsize - $pvsize) -lt $offset ]; then
echo Not enough free space after LVM physical volume
cryptsetup luksClose $ cryptdev _real
exit
fi
Check we have enough space after the LVM physical volume to offset everything by the size of the LUKS header. If not, you can still try again after you reduce the size of the LVM physical volume by an extent.
if [ $(expr $devsize % $offset % $mchunk) -gt 0 ]; then
echo Last
cryptsetup luksClose $ cryptdev _real
exit
fi
This is another check to avoid surprises at the end, when dealing with the last chunk. As the script is written for the moment, it doesn’t support cases where this last chunk is not a multiple of $mchunk. So we need to abort in these.
read major minor <<EOF
$(stat -t "$dev" awk ' print $10,$11 ')
EOF
maps=$(dmsetup deps awk -F: "/\($major, $minor\)/ print \$1 ")
dmsetup create $cryptdev <<EOF
0 $length linear $dev 0
EOF
dmsetup reload $ cryptdev _real <<EOF
$start $length crypt $format $key $IVoff $dev $offset
EOF
dmsetup resume $ cryptdev _real
for map in $ maps ; do
dmsetup table "$map" sed s,$major:$minor,/dev/mapper/$cryptdev, dmsetup reload "$map"
dmsetup resume "$map"
done
Here, we create the dev_crypt device mapper that is our fake LUKS device, which starts as a simple linear mapper and will end as a complete crypt mapper. This fake LUKS device is inserted as an intermediate mapper between the LVM device mapper and the real device. So, the LVM device mapper will have this fake LUKS device as backend, and, at the beginning, the fake LUKS device maps linearly to the real device.
Note we look for all device mappers using the real device as backend before creating the fake LUKS device to avoid finding the fake LUKS device in the list.
Also note dmsetup reload only loads a new table in the INACTIVE slot, and dmsetup resume makes this inactive table LIVE.
cursor=$length
chunk=$offset
while [ $cursor -gt 0 ]; do
cursor=$(expr $cursor - $chunk)
if [ $cursor -lt 0 ]; then
chunk=$(expr $chunk + $cursor)
cursor=0
fi
(
[ $cursor -ne 0 ] && echo 0 $cursor linear $dev 0
echo $cursor $chunk mirror core 1 $mchunk 2 $dev $cursor /dev/mapper/$ cryptdev _real $cursor
[ $cursor -lt $(expr $length - $chunk) ] && echo $(expr $cursor + $chunk) $(expr $length - $cursor - $chunk) crypt $format $key $(expr $IVoff + $cursor + $chunk) $dev $(expr $offset + $cursor + $chunk)
) dmsetup reload "$cryptdev"
dmsetup resume "$cryptdev"
chunks=$(expr $chunk / $mchunk)
while ! dmsetup status "$cryptdev" grep "$chunks/$chunks"; do
true
done
done
This is where the main work is done : moving the data around. We actually just let the device mapper deal with the data duplication, $offset blocks by $offset blocks ($offset being the LUKS header size), using a mirror target for the chunk being moved. So our disk looks like the following:
dmsetup status, dmsetup wait doesn’t seem to be very helpful here.
dmsetup reload "$cryptdev" <<EOF
$start $length crypt $format $key $IVoff $dev $offset
EOF
dmsetup resume "$cryptdev"
dmsetup remove "$ cryptdev _real"
dd if="$luks" of=$dev count=$offset bs=512 2> /dev/null
Final steps of the conversion : our dev_crypt device becomes a full LUKS volume, so we can remove dev_crypt_real and add the LUKS headers at the top of the device we converted.
At this moment, the LUKS volume is setup just as if it had been setup by cryptsetup. For LVM to recognize the change properly, you need to run pvscan. Once you ran it, you can do whatever you want with LVM.
Now, you may want to add the following to your /etc/crypttab file:
$cryptdev $dev none luks
i.e. hda5_crypt /dev/hda5 none luks if the device was /dev/hda5.
And if the LVM volume you converted contains your root filesystem, you should run (for Debian systems):
update-initramfs -u
I tested this successfully under qemu and will give it a shot on my laptop some time soon.
Now, because I had a hard time not finding much about the mirror target of the device mapper, here is what I could gather about it. The target syntax is as follows:
<logical_start_sector> <num_sectors> mirror [ core disk ] <num_params> <param> ... <num_mirrors> [ <destination > <start_sector> ] ...
logical_start_sector, num_sectors, destination and start_sector have the same meaning as in other targets.core and disk are two different log types (to track differences between mirrors), respectively in memory and on disk.num_params and params depend on the log type:
core, num_params can be either 1 or 2.region_size, which is the size (in blocks) of the chunks the mirror log tracks for synchronization. It can’t be less than the page size divided by 512, i.e. 8 on x86, must be a power of 2, and must not exceed num_sectors. Apparently, it is not a problem if num_sectors is not a multiple of region_size.sync or nosync, meaning of which I’m not sure. I think it determines whether the mirror should do the initial synchronization (sync) or not (nosync).disk, num_params can be either 2 or 3, with params being log_device (device where the logs are kept), region_size, as above, and an optional sync or nosync, as above too.num_mirrors is the number of mirrors and for each mirror, we have a pair destination and start_sector.
[ Update: after a quick look at the device mapper source code in Linus’s git tree, updated the mirror target description ]
Cormac and Caoimhe at a patisserie with Chocolate lion in Concarneau, Brittany


Agua para todos...
During the walk, we witnessed what the people from the Delta have been
warning about for decades: the Delta is disappearing. The progressive overuse
of the river's flow, excessive irrigation, damm construction and degradation
is killing this unique place. The waters are slowly claiming what once was
theirs. An electricity line, at the beginning of our trip being at the end
of the beach starts approaching the sea, or viceversa, as you move southward,
to end up many metres inside the water. The old people in the Delta still
remember the 200m tall lighthouse in the Illa de Buda, which the sea
managed to devour.

Sunset at the salines del Delta
When we reached the Punta we could watch a beautiful sunset
over the mountains of Els Ports, and when it got dark and cold we
headed to Tortosa, the area's capital, which I had recently visited for the
first time for one of Softcatal 's
meetings. There, we had dinner at a bar and met Jordi and Anna for a while,
before going back to Ampolla, going to bed relatively early due to everyone
being very tired.

On Saturday, we decided to have a look at the Delta... from above. So we
drove to the top of the Cim del Caro, where we could admire a great
view over the flat farmlands of the Delta, and then found a nice trail which
took us to a nice forest, where we had lunch. After a visit to Alfara de
Carles, we went back to the house, had a great dinner and played fun games at
the table.
Unfortunately, Sunday morning was dedicated to cleaning up the mess in the
appartment and packing, as part of the group, the Jordis from Alcoi and the
twins, had to leave early. The remaining four Valencians decided to go to
Deltebre to have a local paella. It was quite good, although the bar
tender laughed at our request to eat directly from the paella, rejecting
dishes. Small cultural clash. ;)
I remember downloading and viewing the pictures of the trip a few days
after getting back in Val ncia, and then losing track of them. Last week,
while testing an EPIA board with some random hard drive, I suddenly realised
there was a big chance the pictures would be on that drive, and after some
search, I found them; I had been close to mkfs the disk a few minutes
before. I hadn't stopped blaming myself for misplacing these pictures, as they
remind me of this wonderful weekend. Luckily, they are back!
Going back to the state of the Delta, experts say it will be mostly gone
in 200 years. All of the Terres de l'Ebre have fought against the previous
government's "PHN" plan to divert part of the river's flow to the North and
South, to supply Barcelona, and specially the South of Val ncia, where the
developing of the coastal areas is out of control. I fear that whenever PP
regains power in Madrid, they will retake this idea. There is a lot of money
involved in building a 700 km long scar in our land. The Delta de
l'Ebre is a fantastic place which doesn't deserve a terrible fate like
this, only for the benefit of the usual suspects. As the good people of this
land would say...
Lo riu s vida. No al transvasament!

Agua para todos...
During the walk, we witnessed what the people from the Delta have been
warning about for decades: the Delta is disappearing. The progressive overuse
of the river's flow, excessive irrigation, damm construction and degradation
is killing this unique place. The waters are slowly claiming what once was
theirs. An electricity line, at the beginning of our trip being at the end
of the beach starts approaching the sea, or viceversa, as you move southward,
to end up many metres inside the water. The old people in the Delta still
remember the 200m tall lighthouse in the Illa de Buda, which the sea
managed to devour.

Sunset at the salines del Delta
When we reached the Punta we could watch a beautiful sunset
over the mountains of Els Ports, and when it got dark and cold we
headed to Tortosa, the area's capital, which I had recently visited for the
first time for one of Softcatal 's
meetings. There, we had dinner at a bar and met Jordi and Anna for a while,
before going back to Ampolla, going to bed relatively early due to everyone
being very tired.
On Saturday, we decided to have a look at the Delta... from above. So we
drove to the top of the Cim del Caro, where we could admire a great
view over the flat farmlands of the Delta, and then found a nice trail which
took us to a nice forest, where we had lunch. After a visit to Alfara de
Carles, we went back to the house, had a great dinner and played fun games at
the table.
Unfortunately, Sunday morning was dedicated to cleaning up the mess in the
appartment and packing, as part of the group, the Jordis from Alcoi and the
twins, had to leave early. The remaining four Valencians decided to go to
Deltebre to have a local paella. It was quite good, although the bar
tender laughed at our request to eat directly from the paella, rejecting
dishes. Small cultural clash. ;)
I remember downloading and viewing the pictures of the trip a few days
after getting back in Val ncia, and then losing track of them. Last week,
while testing an EPIA board with some random hard drive, I suddenly realised
there was a big chance the pictures would be on that drive, and after some
search, I found them; I had been close to mkfs the disk a few minutes
before. I hadn't stopped blaming myself for misplacing these pictures, as they
remind me of this wonderful weekend. Luckily, they are back!
Going back to the state of the Delta, experts say it will be mostly gone
in 200 years. All of the Terres de l'Ebre have fought against the previous
government's "PHN" plan to divert part of the river's flow to the North and
South, to supply Barcelona, and specially the South of Val ncia, where the
developing of the coastal areas is out of control. I fear that whenever PP
regains power in Madrid, they will retake this idea. There is a lot of money
involved in building a 700 km long scar in our land. The Delta de
l'Ebre is a fantastic place which doesn't deserve a terrible fate like
this, only for the benefit of the usual suspects. As the good people of this
land would say...
Lo riu s vida. No al transvasament!
$ ept-cache related wesnoth -t clean
wesnoth-editor - map editor for Wesnoth
wesnoth-server - multiplayer network server for Wesnoth
wesnoth-tsg - The South Guard official campaign for Wesnoth
wesnoth-ttb - A Tale of Two Brothers official campaign for Wesnoth
wesnoth-utbs - Under the Burning Suns official campaign for Wesnoth
asc - turn-based strategy game
attal - turn-based strategy game
xbattle - Concurrent multi-player battle strategy game
crimson - A hex-based tactical game
stratagus - realtime strategy game for Unix and X
fcmp - Free media files for FreeCraft
wormux - funny fight game on 2D maps
stratagus-gl - realtime strategy game for Unix and X - OpenGL version
boson - core package for Boson
netpanzer - online multiplayer tactical warfare game
You're looking for a package sort of like mutt and firefox?
$ ept-cache related mutt firefox -t clean
mozilla-thunderbird - Transition package for icedove rename
balsa - An e-mail client for GNOME
mozilla-mailnews - Transition package for Iceape Mail & Newsgroups and Address Book
mozilla-thunderbird-dev - Transition package for icedove-dev rename
mozilla-thunderbird-enigmail - Transition package for enigmail rename
mozilla-thunderbird-inspector - Transition package for icedove-inspector rename
firefox-gnome-support - Transition package for iceweasel rename
mozilla-firefox-gnome-support - Transition package for iceweasel rename
mozilla-thunderbird-typeaheadfind - Transition package for icedove-typeaheadfind rename
firefox-dom-inspector - Transition package for iceweasel rename
etpan-ng - console mail user agent based on libEtPan!
firefox-webdeveloper - Transition package for iceweasel rename
firefox-greasemonkey - Transition package for iceweasel rename
tkrat - Mail user agent with POP, IMAP, mh support using Tk
thunderbird - Transition package for icedove rename
All of this in ept-cache 0.5.7 uploaded tonight to unstable.
Remember to ept-cache reindex after upgrades, as the indexing algorithm
sometimes improves. With 0.5.7, for example, it generates an index which is
something like 66% smaller than the previous versions.
MAJOR=$(awk '/device-mapper/ print $1;exit ' /proc/devices)
dmsetup ls --target linear while read map extra; do
TABLE=$(dmsetup table $map while IFS=" :" read start length linear major minor offset; do
map2=
if [ "$major" -eq "$MAJOR" ]; then
IFS=":" read map2 extra <<EOF
$(dmsetup info -c --noheadings -j $major -m $minor)
EOF
fi
if [ -z "$map2" ] [ $(dmsetup table "$map2" wc -l) -gt 1 ] [ $(dmsetup table --target crypt "$map2" wc -l) -eq 0 ]; then
echo $start $length $linear $major:$minor $offset
else
IFS=" " read cstart clength crypt format key IVoff dev coffset <<EOF
$(dmsetup table "$map2")
EOF
echo $start $length crypt $format $key $(expr $IVoff + $offset) $dev $(expr $coffset + $offset)
fi
done)
if (dmsetup table $map; echo "$TABLE") sort uniq -c grep -v "^ *2 " > /dev/null 2>&1;
then
echo Diverting $map
dmsetup reload $map <<EOF
$TABLE
EOF
dmsetup resume $map
fi
done
[ Update: There was a dmsetup resume missing in the script ]
After running this script, you should be able to run cryptsetup luksClose on your LUKS volume. Once you’ve done it, pvdisplay and other LVM tools will show you nothing, because the physical volume will just have disappeared… but everything will still be in place. Note that if you leave the LUKs volume open, you’d better be careful NOT to do operations with LVM (such as pvmove) because it could break everything.
Note that on sid, you would need to add a –showkey option into the $(dmsetup table "$map2") line. This option is necessary for dmsetup to display the encryption keys.
I wonder if LVM upstream would consider supporting LUKS volumes directly…
Anyways, back to the in-place conversion to LVM over LUKs. The idea would be to:
fnreduce :: [(a->a)]->a->a
fnreduce [] value = value
fnreduce (a:as) value = freduce as $ a value
fnreduce as value = (foldr (flip (.)) id as) value
f . (g . (h . id)) = ((id . f) . g) . h
fnreduce as value = foldr (.) id (reverse as) value
fnreduce as value = foldl (flip ($)) value as
module Test where
data TransformList a b where
Nil :: TransformList a a
Cons :: (a -> b) -> TransformList b c -> TransformList a c
apply :: TransformList a b -> a -> b
apply Nil = id
apply (Cons f fs) = (apply fs) . f
> apply (Cons (\x -> x + 1) (Cons (\x -> show x) Nil)) 5
"6"
> ((\x -> show x) . (\x -> x + 1)) 5
"6"
traceIntermediates Nil x = (x, [])
traceIntermediates (Cons f l) x = let x' = f x
(x'', ss) = traceIntermediates l x'
in
(x'', (show x'):ss)
data ShowTransformList a b where
Nil :: (Show a) => ShowTransformList a a
Cons :: (Show a, Show b, Show c) => (a -> b) -> ShowTransformList b c -> ShowTransformList a c
traceIntermediates :: ShowTransformList a b -> a -> (b, [String])
traceIntermediates Nil x = (x, [])
traceIntermediates (Cons f l) x = let x' = f x
(x'', ss) = traceIntermediates l x'
in
(x'', (show x'):ss)
apply :: ShowTransformList a b -> a -> b
apply Nil = id
apply (Cons f fs) = (apply fs) . f
> traceIntermediates
(Cons (\x -> x + 1)
(Cons (\x -> x * 2)
(Cons (\x -> x - 5)
(Cons show
(Cons (read :: String -> Double)
Nil))))) 5
(7.0,["6","12","7","\"7\"","7.0"])
Next.