slitaz-tools view lib/libmountbox @ rev 380

mountbox: cleanup
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Aug 15 22:20:22 2009 +0200 (2009-08-15)
parents 9eafe366717d
children d8c2f18f96c2
line source
1 #!/bin/sh
2 #
3 # Libmountbox provides devices list in suitable format for GTK tree
4 # and various dialog boxes to mount, umount, etc.
5 #
6 # (C) 2009 - SliTaz GNU/Linux project.
7 #
9 # Short usage.
10 usage()
11 {
12 echo -e "\nUsage: $0 command\n
13 Output commands:
14 list-mounted List all mounted devices in suitable GTK tree format.
15 list-umounted List all umounted in suitable GTK tree format.
17 GTKdialog boxes
18 mounted-fs-infos Display a mounted devices infos with actions.
19 umounted-fs-infos Display a umounted devices infos with actions.\n"
20 }
22 # Format df -h output for GTK tree.
23 mounted_fs_data()
24 {
25 SIZE=`echo $RES | cut -d " " -f 2`
26 USED=`echo $RES | cut -d " " -f 3`
27 AVAILABLE=`echo $RES | cut -d " " -f 4`
28 PCT=`echo $RES | cut -d " " -f 5`
29 MOUNTED_ON=`echo $RES | cut -d " " -f 6`
30 }
32 # Update BLOCKS SIZE UUID TYPE LABEL variables
33 getdevinfo()
34 {
35 local dev
36 dev=${1#/dev/}
37 BLOCKS=0
38 if [ -f /sys/block/$dev/size ]; then
39 BLOCKS=`cat /sys/block/$dev/size`
40 elif [ -f /sys/block/*/$dev/size ]; then
41 BLOCKS=`cat /sys/block/*/$dev/size`
42 fi
43 if [ $BLOCKS -gt 2097152 ]; then
44 unit=G
45 n=$(($BLOCKS * 10 / 2097152))
46 elif [ $BLOCKS -gt 2048 ]; then
47 unit=M
48 n=$(($BLOCKS * 10 / 2048))
49 else
50 unit=K
51 n=$(($BLOCKS * 5))
52 fi
53 SIZE=$(($n/10)).$(($n%10))$unit
54 UUID=`blkid | grep ^/dev/$dev: | grep UUID= | sed 's/.*UUID=\"\([^\"]*\)\".*/\1/'`
55 TYPE=`blkid | grep ^/dev/$dev: | grep TYPE= | sed 's/.*TYPE=\"\([^\"]*\)\".*/\1/'`
56 LABEL=`blkid | grep ^/dev/$dev: | grep LABEL= | sed 's/.*LABEL=\"\([^\"]*\)\".*/\1/'`
57 }
59 # Generate label for device manager
60 dmlabel()
61 {
62 echo crypto-$(basename $1)
63 }
65 unwrap()
66 {
67 awk '{
68 if ($2 == "") last=$1;
69 else {
70 print last $0
71 last=""
72 }
73 }'
74 }
76 case $1 in
77 list-mounted)
78 # List all fs found by: df -h
79 #
80 for dev in `df -h | grep ^/dev/ | cut -d " " -f 1`
81 do
82 RES=`df -h $dev | unwrap | grep ^$dev`
83 mounted_fs_data
84 echo "$dev | $SIZE | $USED | $AVAILABLE | $PCT | $MOUNTED_ON"
85 done ;;
86 list-umounted)
87 # List all umounted fs found by: fdisk -l
88 #
89 for dev in `fdisk -l | grep ^/dev | cut -d " " -f 1`
90 do
91 RES=`fdisk -l | grep $dev | sed s/*//g`
92 START=`echo $RES | cut -d " " -f 2`
93 END=`echo $RES | cut -d " " -f 3`
94 BLOCKS=`echo $RES | cut -d " " -f 4`
95 ID=`echo $RES | cut -d " " -f 5`
96 SYSTEM=`echo $RES | cut -d " " -f 6-`
97 # Bootable...
98 if fdisk -l | grep $dev | grep -q "*"; then
99 BOOT="yes"
100 else
101 BOOT="-"
102 fi
103 # Skip swap, extended, and mounted partitions.
104 if echo $RES | grep -q "swap" || echo $RES | grep -q "Extended" ; then
105 continue
106 elif mount | grep -q "^$dev "; then
107 continue
108 else
109 getdevinfo $dev
110 echo "$dev | $SIZE | $SYSTEM | $TYPE | $LABEL | $BOOT | $START | $END | $BLOCKS | $ID | $UUID"
111 fi
112 done
113 for dev in /dev/mapper/* ; do
114 [ -b $dev ] || continue
115 mount | grep -q "^$dev " && continue
116 mdev=dm-$(ls -l $dev | awk '{ print $6 }')
117 mount | grep -q "^/dev/$mdev " && continue
118 getdevinfo /dev/$mdev
119 echo "$dev | $SIZE | - | $TYPE | $LABEL | - | - | - | $BLOCKS | - | $UUID"
120 done
121 for dev in $(losetup | cut -d: -f1); do
122 [ -b $dev ] || continue
123 mount | grep -q "^$dev " && continue
124 getdevinfo $dev
125 echo "$dev | $SIZE | - | $TYPE | $LABEL | - | - | - | $BLOCKS | - | $UUID"
126 done
127 [ -e /dev/cdrom ] &&
128 echo "/dev/cdrom | - | CD/DVD | iso9660 | - | - | - | - | - | - | -"
129 for i in /sys/devices/platform/floppy.*/block:*; do
130 [ -e $i ] && echo "/dev/${i#*block:} | - | - | - | floppy | - | - | - | - | -"
131 done
132 ;;
133 mounted-fs-infos)
134 # Mounted fs info and actions, rootfs or other fs.
135 #
136 if [ "$MOUNTED" == "/dev/root" ]; then
137 export MOUNTED_DEVICE="
138 <window title=\"Device: rootfs\" icon-name=\"media-flash\">
139 <vbox>
140 <text use-markup=\"true\" width-chars=\"56\">
141 <label>\"
142 <b>/dev/root</b>
143 \"
144 </label>
145 </text>
146 <text use-markup=\"true\" width-chars=\"56\">
147 <input>df -h / | grep ^rootfs</input>
148 </text>
149 <hbox>
150 <button>
151 <label>Browse</label>
152 <input file icon=\"folder-open\"></input>
153 <action>pcmanfm / &</action>
154 <action type=\"closewindow\">MOUNTED_DEVICE</action>
155 </button>
156 <button>
157 <input file icon=\"gtk-close\"></input>
158 <action type=\"closewindow\">MOUNTED_DEVICE</action>
159 </button>
160 </hbox>
161 </vbox>
162 </window>"
163 gtkdialog --center --program=MOUNTED_DEVICE
164 else
165 getdevinfo $MOUNTED
166 RES=`df -h $MOUNTED | grep ^$MOUNTED`
167 mounted_fs_data
168 export MOUNTED_DEVICE="
169 <window title=\"Device: $MOUNTED\" icon-name=\"media-flash\">
170 <vbox>
171 <text use-markup=\"true\" width-chars=\"56\">
172 <label>\"
173 Device <b>$MOUNTED</b> is mounted on <b>$MOUNTED_ON</b>
175 Size: $SIZE
176 UUID: $UUID
177 Type: $TYPE
178 Label: $LABEL
179 \"
180 </label>
181 </text>
182 <hbox>
183 <button>
184 <label>Browse</label>
185 <input file icon=\"folder-open\"></input>
186 <action>pcmanfm $MOUNTED_ON &</action>
187 <action type=\"closewindow\">MOUNTED_DEVICE</action>
188 </button>
189 <button>
190 <label>Umount</label>
191 <input file icon=\"undo\"></input>
192 <action>umount $MOUNTED_ON</action>
193 <action type=\"closewindow\">MOUNTED_DEVICE</action>
194 </button>
195 <button>
196 <input file icon=\"gtk-close\"></input>
197 <action type=\"closewindow\">MOUNTED_DEVICE</action>
198 </button>
199 </hbox>
200 </vbox>
201 </window>"
202 gtkdialog --center --program=MOUNTED_DEVICE
203 fi ;;
204 umounted-fs-infos)
205 # Mounted fs info and actions, rootfs or other fs.
206 #
207 case "$DEVICE" in
208 /dev/mapper/*) mdev=/dev/dm-$(ls -l $DEVICE | awk '{ print $6 }');;
209 *) mdev=$DEVICE;;
210 esac
211 getdevinfo $mdev
212 export UMOUNTED_DEVICE="
213 <window title=\"Device: $DEVICE\" icon-name=\"media-flash\">
214 <vbox>
215 <text use-markup=\"true\" width-chars=\"56\">
216 <label>\"
217 Mount <b>$DEVICE</b> on <b>$MOUNT_POINT</b>
219 Size: $SIZE
220 UUID: $UUID
221 Type: $TYPE
222 Label: $LABEL
223 \"
224 </label>
225 </text>
227 <hbox>
228 <button>
229 <label>Mount</label>
230 <input file icon=\"edit-redo\"></input>
231 <action>mkdir -p $MOUNT_POINT</action>
232 <action>mount $DEVICE $MOUNT_POINT</action>
233 <action type=\"closewindow\">MOUNTED_DEVICE</action>
234 </button>"
235 while read type fsck args; do
236 [ "$TYPE" == "$type" ] || continue
237 which $fsck > /dev/null || continue
238 UMOUNTED_DEVICE="$UMOUNTED_DEVICE
239 <button>
240 <label>$fsck check</label>
241 <input file icon=\"drive-harddisk\"></input>
242 <action>xterm -T \"$fsck $args $DEVICE\" \
243 -geometry 80x12 \
244 -e \"echo; $fsck $args $DEVICE; \
245 echo -e '----\\nENTER to close Terminal'; \
246 read i\" &</action>
247 <action type=\"closewindow\">MOUNTED_DEVICE</action>
248 </button>"
249 done <<EOT
250 ext3 e2fsck -p
251 ext2 e2fsck -p
252 vfat dosfsck -a
253 msdos dosfsck -a
254 xfs fsck.xfs -s
255 reiserfs reiserfsck --fix-fixable
256 jfs jfs_fsck -a
257 EOT
258 UMOUNTED_DEVICE="$UMOUNTED_DEVICE
259 <button>
260 <input file icon=\"gtk-close\"></input>
261 <action type=\"closewindow\">UMOUNTED_DEVICE</action>
262 </button>
263 </hbox>
264 </vbox>
265 </window>"
266 gtkdialog --center --program=UMOUNTED_DEVICE ;;
267 loopmgr)
268 export LOOP_MANAGER="
269 <window title=\"Loop manager\" icon-name=\"media-flash\">
270 <vbox>
271 <tree>
272 <width>420</width><height>120</height>
273 <variable>LOOP_DEV</variable>
274 <label>Device|File|Size|Type|Label|Mounted on|Offset|UUID</label>"
275 while read dev offset file; do
276 DEV=$(echo $dev | cut -d: -f1)
277 [ -b "$DEV" ] || continue
278 MOUNT=$(mount | grep ^$DEV | awk '{ print $3 }')
279 getdevinfo $DEV
280 LOOP_MANAGER="$LOOP_MANAGER
281 <item>$DEV | $file | $SIZE | $TYPE | $LABEL | $MOUNT | $offset | $UUID</item>
282 "
283 done <<EOT
284 $(losetup)
285 EOT
286 LOOP_MANAGER="$LOOP_MANAGER
287 <action>/usr/lib/slitaz/libmountbox remove-loop</action>
288 <action>/usr/lib/slitaz/libmountbox loopmgr &</action>
289 <action type=\"closewindow\">LOOP_MANAGER</action>
290 </tree>
291 <hbox>
292 <text use-markup=\"true\">
293 <label>\"<b>File</b>\"</label>
294 </text>
295 <entry accept=\"filename\">
296 <variable>FILE</variable>
297 </entry>
298 <button>
299 <input file icon=\"folder-open\"></input>
300 <action type=\"fileselect\">FILE</action>
301 </button>
302 </hbox>
303 <hbox>
304 <text use-markup=\"true\">
305 <label>\"<b>Offset</b>\"</label>
306 </text>
307 <entry>
308 <default>0</default>
309 <variable>OFFSET</variable>
310 </entry>
311 <button>
312 <label>Disable</label>
313 <input file icon=\"remove\"></input>
314 <action>/usr/lib/slitaz/libmountbox remove-loop</action>
315 <action>/usr/lib/slitaz/libmountbox loopmgr &</action>
316 <action type=\"closewindow\">LOOP_MANAGER</action>
317 </button>
318 <button>
319 <label>Enable</label>
320 <input file icon=\"add\"></input>
321 <action>/usr/lib/slitaz/libmountbox add-loop</action>
322 <action>/usr/lib/slitaz/libmountbox loopmgr &</action>
323 <action type=\"closewindow\">LOOP_MANAGER</action>
324 </button>
325 <button>
326 <label>Create</label>
327 <input file icon=\"filenew\"></input>
328 <action>/usr/lib/slitaz/libmountbox new-file</action>
329 <action>/usr/lib/slitaz/libmountbox loopmgr &</action>
330 <action type=\"closewindow\">LOOP_MANAGER</action>
331 </button>
332 <button>
333 <input file icon=\"gtk-close\"></input>
334 <action type=\"closewindow\">LOOP_MANAGER</action>
335 </button>
336 </hbox>
337 </vbox>
338 </window>"
339 gtkdialog --center --program=LOOP_MANAGER ;;
340 remove-loop)
341 getdevinfo $LOOP_DEV
342 export REMOVE_DEVICE="
343 <window title=\"Device: $LOOP_DEV\" icon-name=\"media-flash\">
344 <vbox>
345 <text use-markup=\"true\" width-chars=\"56\">
346 <label>\"
347 Disable <b>$LOOP_DEV</b> ?
349 Size: $SIZE
350 UUID: $UUID
351 Type: $TYPE
352 Label: $LABEL
353 \"
354 </label>
355 </text>
356 <hbox>
357 <button yes>
358 <action>umount $LOOP_DEV</action>
359 <action>losetup -d $LOOP_DEV</action>
360 <action type=\"closewindow\">LOOP_MANAGER</action>
361 <action>/usr/lib/slitaz/libmountbox loopmgr &</action>
362 <action type=\"closewindow\">REMOVE_DEVICE</action>
363 </button>
364 <button no>
365 <action type=\"closewindow\">REMOVE_DEVICE</action>
366 </button>
367 </hbox>
368 </vbox>
369 </window>"
370 gtkdialog --center --program=REMOVE_DEVICE ;;
371 add-loop)
372 losetup -o $OFFSET $(losetup -f) $FILE
373 ;;
374 create-file)
375 file=$2
376 size=$3
377 [ -n "$file" -a -n "$size" ] || exit
378 [ "$4" == "true" ] && bs="1K"
379 [ "$5" == "true" ] && bs="1M"
380 [ "$6" == "true" ] && bs="1024M"
381 if [ "$7" != "true" -a -e "$file" ]; then
382 export FILE_EXIST="
383 <window title=\"File exist\" icon-name=\"media-flash\">
384 <vbox>
385 <hbox>
386 <text use-markup=\"true\">
387 <label> \"The file <b>$file</b> already exists\" </label>
388 </text>
389 </hbox>
390 <hbox>
391 <button>
392 <input file icon=\"gtk-close\"></input>
393 <action type=\"closewindow\">FILE_EXIST</action>
394 </button>
395 </hbox>
396 </vbox>
397 </window>"
398 gtkdialog --center --program=FILE_EXIST
399 exit
400 fi
401 dd if=/dev/zero of=$file bs=$bs count=$size
402 ;;
403 new-file)
404 export CREATE_FILE="
405 <window title=\"Create file\" icon-name=\"media-flash\">
406 <vbox>
407 <hbox>
408 <text use-markup=\"true\">
409 <label>\"<b>File:</b>\"</label>
410 </text>
411 <entry>
412 <variable>FILE</variable>
413 $( [ -n "$FILE" ] && echo "<default>$FILE</default>" )
414 </entry>
415 <button>
416 <input file icon=\"folder-open\"></input>
417 <action type=\"fileselect\">FILE</action>
418 </button>
419 </hbox>
420 <hbox>
421 <text use-markup=\"true\">
422 <label>\"<b>Size:</b>\"</label>
423 </text>
424 <entry>
425 <variable>NEW_SIZE</variable>
426 <default>0</default>
427 </entry>
428 <radiobutton>
429 <label>Kb</label>
430 <variable>KB</variable>
431 </radiobutton>
432 <radiobutton>
433 <label>Mb</label>
434 <variable>MB</variable>
435 </radiobutton>
436 <radiobutton>
437 <label>Gb</label>
438 <variable>GB</variable>
439 </radiobutton>
440 </hbox>
441 <hbox>
442 <checkbox>
443 <label>Override</label>
444 <variable>OVERRIDE</variable>
445 <default>false</default>
446 </checkbox>
447 <button>
448 <label>Create</label>
449 <input file icon=\"filenew\"></input>
450 <action>/usr/lib/slitaz/libmountbox create-file \"\$FILE\" \"\$NEW_SIZE\" \$KB \$MB \$GB \$OVERRIDE</action>
451 <action type=\"closewindow\">CREATE_FILE</action>
452 </button>
453 <button>
454 <input file icon=\"gtk-close\"></input>
455 <action type=\"closewindow\">CREATE_FILE</action>
456 </button>
457 </hbox>
458 </vbox>
459 </window>"
460 gtkdialog --center --program=CREATE_FILE ;;
461 cryptomgr)
462 missing=""
463 for i in libdevmapper dmsetup linux-md hashalot cryptsetup ; do
464 [ -d /var/lib/tazpkg/installed/$i ] || missing="$missing $i"
465 done
466 if [ -n "$missing" ]; then
467 export CRYPTO_MANAGER="
468 <window title=\"Crypto manager\" icon-name=\"media-flash\">
469 <vbox>
470 <hbox>
471 <text use-markup=\"true\">
472 <label>\"
473 <b>Need the packages :</b>
475 $(for i in $missing ; do echo $i; done)
476 \" </label>
477 </text>
478 </hbox>
479 <hbox>
480 <button>
481 <label>Install</label>
482 <input file icon=\"go-jump\"></input>
483 <action>xterm -T \"Install packages\" \
484 -geometry 80x16+120+120 -e \"for i in $missing ; do \
485 yes y | tazpkg get-install \\\$i ; done; sleep 2\" \
486 </action>
487 <action>/usr/lib/slitaz/libmountbox cryptomgr &</action>
488 <action type=\"closewindow\">CRYPTO_MANAGER</action>
489 </button>
490 <button>
491 <input file icon=\"gtk-close\"></input>
492 <action type=\"closewindow\">CRYPTO_MANAGER</action>
493 </button>
494 </hbox>
495 </vbox>
496 </window>"
497 gtkdialog --center --program=CRYPTO_MANAGER
498 exit
499 fi
500 for i in dm-mod dm-crypt aes-i586 ; do
501 lsmod | grep -q $i || modprobe $i
502 done
503 export CRYPTO_MANAGER="
504 <window title=\"Crypto manager\" icon-name=\"media-flash\">
505 <vbox>
506 <tree>
507 <width>420</width><height>120</height>
508 <variable>CRYTO_DEV</variable>
509 <label>Name|Device|Size|Type|Label|Mounted on|UUID</label>"
510 while read name ; do
511 [ -b /dev/mapper/$name ] || continue
512 DEV=/dev/dm-$(ls -l /dev/mapper/$name | awk '{ print $6 }')
513 MOUNT=$(mount | egrep "^$DEV|/dev/mapper/$name" | awk '{ print $3 }')
514 getdevinfo $DEV
515 CRYPTO_MANAGER="$CRYPTO_MANAGER
516 <item>$name|$DEV | $SIZE | $TYPE | $LABEL | $MOUNT | $UUID</item>
517 "
518 done <<EOT
519 $(ls /dev/mapper 2> /dev/null)
520 EOT
521 CRYPTO_MANAGER="$CRYPTO_MANAGER
522 </tree>
523 <hbox>
524 <text use-markup=\"true\">
525 <label>\"<b>Device :</b>\"</label>
526 </text>
527 <combobox>
528 <label>Device</label>
529 $( (losetup ; blkid | grep -v /dev/loop ) | cut -d: -f1 | awk '{ printf "<item>%s</item>\n",$1}')
530 <variable>DEVICE</variable>
531 </combobox>
532 <text use-markup=\"true\">
533 <label>\"<b>Cipher :</b>\"</label>
534 </text>
535 <combobox>
536 <variable>CRYPTO</variable>
537 <item>aes-256</item>
538 <item>aes-192</item>
539 <item>aes-128</item>
540 </combobox>
541 </hbox>
542 <hbox>
543 <button>
544 <label>Remove</label>
545 <input file icon=\"remove\"></input>
546 <action>/usr/lib/slitaz/libmountbox remove-crypto</action>
547 <action>/usr/lib/slitaz/libmountbox cryptomgr &</action>
548 <action type=\"closewindow\">CRYPTO_MANAGER</action>
549 </button>
550 <button>
551 <label>Create</label>
552 <input file icon=\"add\"></input>
553 <action>/usr/lib/slitaz/libmountbox add-crypto</action>
554 <action>/usr/lib/slitaz/libmountbox cryptomgr &</action>
555 <action type=\"closewindow\">CRYPTO_MANAGER</action>
556 </button>
557 <button>
558 <label>Format</label>
559 <input file icon=\"gtk-clear\"></input>
560 <action>/usr/lib/slitaz/libmountbox format-crypto</action>
561 <action>xterm -T \"Format LUKS\" \
562 -geometry 80x16+120+120 -e \"\
563 cryptsetup luksFormat -c aes -h sha\${CRYPTO#aes-} \$DEVICE \" \
564 </action>
565 <action>/usr/lib/slitaz/libmountbox cryptomgr &</action>
566 <action type=\"closewindow\">CRYPTO_MANAGER</action>
567 </button>
568 <button>
569 <input file icon=\"gtk-close\"></input>
570 <action type=\"closewindow\">CRYPTO_MANAGER</action>
571 </button>
572 </hbox>
573 </vbox>
574 </window>"
575 gtkdialog --center --program=CRYPTO_MANAGER ;;
576 add-old-crypto)
577 # Not luks format
578 # Try old loop-eas
579 case "$CRYPTO" in
580 *128) key="16 sha256";;
581 *192) key="24 sha384";;
582 *) key="32 sha512";;
583 esac
584 getdevinfo $DEVICE
585 key=$(echo $PASS_PHRASE | hashalot -x -n $key)
586 echo 0 $BLOCKS crypt aes-plain $key 0 $DEVICE 0 | \
587 dmsetup create $(dmlabel $DEVICE)
588 ;;
589 add-crypto)
590 if cryptsetup isLuks $DEVICE 2> /dev/null; then
591 xterm -T "Open LUKS" -e \
592 "cryptsetup luksOpen $DEVICE $(dmlabel $DEVICE)"
593 else
594 export GET_PASS_PHRASE="
595 <window title=\"Crypto manager\" icon-name=\"media-flash\">
596 <vbox>
597 <hbox>
598 <text use-markup=\"true\">
599 <label>\"<b>Pass phrase :</b>\"</label>
600 </text>
601 <entry>
602 <variable>PASS_PHRASE</variable>
603 </entry>
604 <button ok>
605 <action>/usr/lib/slitaz/libmountbox add-old-crypto</action>
606 <action type=\"closewindow\">GET_PASS_PHRASE</action>
607 </button>
608 </hbox>
609 </vbox>
610 </window>"
611 gtkdialog --center --program=GET_PASS_PHRASE
612 fi
613 ;;
614 remove-crypto)
615 cryptsetup luksClose $CRYTO_DEV
616 [ -e /dev/mapper/$CRYTO_DEV ] && dmsetup remove $CRYTO_DEV
617 ;;
618 *)
619 usage ;;
620 esac
622 exit 0