improve createfs createswap (by-id listing)
[cmdllinux.git] / scripts / manage / createfs-byid
blob3dbc68e90873e95b4e7d148083c921e62a0ff602
1 #! /bin/sh
3 : ${DIALOG=dialog}
5 tempfile=`mktemp 2> /dev/null` || tempfile=/tmp/test$$
6 trap "rm -f $tempfile" 0 1 2 5 15
9 lslinpart()
11 for drv in $(find /dev -maxdepth 1 -name "[hs]d[a-z]" -o -name "ub[a-z]" | sort); do
12 LC_ALL=C sfdisk -d $drv 2> /dev/null | grep "^/dev/" | \
13 while read LINE; do
14 if echo "$LINE" | grep -q "type=83"; then
15 DEVICE=$(echo "$LINE" | cut -d : -f 1 | tr -d " ")
16 DEVNAME=$(basename $DEVICE)
17 shopt -s lastpipe
18 (cd /dev/disk/by-id; ls -l ata-* usb-* scsi-* 2> /dev/null | \
19 sed -n -e 's%.*[[:space:]]\+\(ata-.*\)%\1%p' -e 's%.*[[:space:]]\+\(usb-.*\)%\1%p' \
20 -e 's%.*[[:space:]]\+\(scsi-.*\)%\1%p' | grep "/$DEVNAME$") | read -r DEV_ID LNK_S LNK
21 shopt -u lastpipe
22 if [ -n "$DEV_ID" ]; then
23 echo -n "$DEV_ID "
24 eval $(echo "$LINE" | cut -d : -f 2 | cut -d , -f 2 | tr -d " ")
25 echo -n "$(expr $size \* 512 / 1048576)MB "
28 done
29 done
32 $DIALOG --backtitle "Create filesystem on device" \
33 --title "" --clear \
34 --menu "Select partition" 20 70 15 \
35 `lslinpart` 2> $tempfile
37 retval=$?
39 choice=`cat $tempfile`
40 case $retval in
42 if [ ! -z "$choice" ]; then
43 DEV=`realpath /dev/disk/by-id/$choice`
45 $DIALOG --backtitle "Create filesystem on device" \
46 --title "" --clear --default-item ext4 \
47 --menu "Select filesystem to create ($DEV)" 20 61 15 \
48 `find /sbin -name "mkfs.*" | cut -d "." -f 2 | grep -v "bfs\|cramfs\|ext4dev" | sort | sed 's/.*/& </'` 2> $tempfile
50 retval=$?
52 choice=`cat $tempfile`
53 case $retval in
55 if [ ! -z "$choice" ]; then
56 [ "$choice" = "xfs" ] && mkfsopt="-f"
57 [ "$choice" = "jfs" ] && mkfsopt="-q"
58 [ "$choice" = "reiserfs" ] && mkfsopt="-q"
59 [ "$choice" = "reiser4" ] && mkfsopt="-y"
60 mkfs.$choice $mkfsopt $DEV
64 #cancel pressed
66 255)
67 #ESC pressed
69 esac
74 #cancel pressed
76 255)
77 #ESC pressed
79 esac