1 | #!/bin/sh |
---|
2 | # |
---|
3 | # $Id: label-partitions-as-necessary 3472 2015-09-29 00:26:05Z bruno $ |
---|
4 | # |
---|
5 | ############################################ |
---|
6 | |
---|
7 | |
---|
8 | read_partition_line() { |
---|
9 | local label mountpt command format device d m |
---|
10 | |
---|
11 | device=`echo "$1" | awk '{print $1}'` |
---|
12 | d=`echo "$device" | cut -c1` |
---|
13 | format=`echo "$1" | awk '{print $3}'` |
---|
14 | label=`echo "$device" | cut -s -d'=' -f2` |
---|
15 | if [ "$d" = "/" ] && [ "$label" = "" ]; then |
---|
16 | m=`echo "$1" | awk '{print $2}'` |
---|
17 | # We force UUID even if nothing in fstab as some new distro like RHEL7 rely on it |
---|
18 | [ "$format" = "reiserfs" ] && opttun="-u" || opttun="-U" |
---|
19 | label=`awk '{print $2,$5}' $mountlist | grep -E "^$m " | awk '{print $2}'` |
---|
20 | else |
---|
21 | if [ "`echo "$1" | grep -E 'LABEL='`" != "" ] ; then |
---|
22 | [ "$format" = "reiserfs" ] && opttun="-l" || opttun="-L" |
---|
23 | elif [ "`echo "$1" | grep -E 'UUID='`" != "" ] ; then |
---|
24 | [ "$format" = "reiserfs" ] && opttun="-u" || opttun="-U" |
---|
25 | else |
---|
26 | LogIt "Nothing to do on $1" |
---|
27 | return |
---|
28 | fi |
---|
29 | fi |
---|
30 | mountpt=`awk '{print $1,$5}' $mountlist | grep " $label$" | awk '{print $1}'` |
---|
31 | |
---|
32 | if [ ! "$mountpt" ] ; then |
---|
33 | LogIt "Not labeling anything as ($label) because ($mountpt) is not a mountpoint" |
---|
34 | elif [ ! "$label" ] ; then |
---|
35 | LogIt "Not labeling ($mountpt) as anything because ($label) is not a label/uuid" |
---|
36 | else |
---|
37 | if [ "$format" = "ext2" ] || [ "$format" = "ext3" ] || [ "$format" = "ext4" ]; then |
---|
38 | if [ "$format" = "ext4" ] && [ -x "/sbin/tune4fs" ]; then |
---|
39 | command="/sbin/tune4fs $opttun $label $mountpt" |
---|
40 | else |
---|
41 | command="tune2fs $opttun $label $mountpt" |
---|
42 | fi |
---|
43 | LogIt "Running $command" |
---|
44 | $command |
---|
45 | elif [ "$format" = "reiserfs" ]; then |
---|
46 | command="reiserfstune $opttun $label $mountpt" |
---|
47 | LogIt "Running $command" |
---|
48 | $command |
---|
49 | elif [ "$format" = "btrfs" ]; then |
---|
50 | # Also see https://btrfs.wiki.kernel.org/index.php/Project_ideas#Filesystem_UUID_change_-_off-line |
---|
51 | command="btrfs filesystem label $mountpt $label" |
---|
52 | LogIt "Running $command" |
---|
53 | $command |
---|
54 | elif [ "$format" = "xfs" ]; then |
---|
55 | command="xfs_admin $opttun $label $mountpt" |
---|
56 | LogIt "Running $command" |
---|
57 | $command |
---|
58 | elif [ "$format" = "fat" ] || [ "$format" = "vfat" ]; then |
---|
59 | if [ -x "/sbin/fatlabel" ]; then |
---|
60 | command="/sbin/fatlabel $mountpt $label" |
---|
61 | LogIt "Running $command" |
---|
62 | $command |
---|
63 | else |
---|
64 | LogIt "No fatlabel command available to label a FAT/VFAT FS" |
---|
65 | fi |
---|
66 | elif [ "$format" = "swap" ] ; then |
---|
67 | if [ "$opttun" = "-U" ]; then |
---|
68 | typelabel="UUID" |
---|
69 | else |
---|
70 | typelabel="Label" |
---|
71 | fi |
---|
72 | if [ -x "/sbin/swaplabel" ]; then |
---|
73 | # Better choice |
---|
74 | command="/sbin/swaplabel $opttun $label $mountpt" |
---|
75 | else |
---|
76 | if [ "$opttun" = "-U" ]; then |
---|
77 | LogIt "Really running dd conv=notrunc of=$mountpt obs=1 seek=1036" |
---|
78 | echo -n "$label" | perl -ne 's/-//g;chomp;print pack "H*",$_' | dd conv=notrunc "of=$mountpt" obs=1 seek=1036 |
---|
79 | command=/bin/true |
---|
80 | else |
---|
81 | command="mkswap -f $opttun $label $mountpt" |
---|
82 | fi |
---|
83 | fi |
---|
84 | LogIt "Creating $typelabel $label on swap partition $mountpt" |
---|
85 | LogIt "Running $command" |
---|
86 | $command |
---|
87 | else |
---|
88 | LogIt "I am NOT going to run tune2|4fs/reiserfstune: the partition is format '$format', which doesn't like tune2|4fs/reiserfstune anyway" |
---|
89 | fi |
---|
90 | fi |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | # --------------------------------------------- |
---|
95 | |
---|
96 | LogIt "Identifying your drives with FS tune tool" |
---|
97 | if [ "$#" -ne "1" ] ; then |
---|
98 | LogIt "label-partitions-as-necessary /tmp/mountlist.txt < /tmp/fstab.new" 1 |
---|
99 | exit 1 |
---|
100 | fi |
---|
101 | mountlist=$1 |
---|
102 | noof_blank_lines=0 |
---|
103 | read line |
---|
104 | # We end after 50 empty lines, which hopefully means we reach the end of the file |
---|
105 | while [ "$noof_blank_lines" -le "50" ] ; do |
---|
106 | if [ "$line" = "" ] ; then |
---|
107 | noof_blank_lines=$(($noof_blank_lines+1)) |
---|
108 | else |
---|
109 | noof_blank_lines=0 |
---|
110 | read_partition_line "$line" |
---|
111 | fi |
---|
112 | read line |
---|
113 | done |
---|
114 | LogIt "Labeling complete." |
---|
115 | exit 0 |
---|