Welcome
Welcome to refracta

You are currently viewing our boards as a guest, which gives you limited access to view most discussions and access our other features. By joining our free community, you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content, and access many other special features. In addition, registered members also see less advertisements. Registration is fast, simple, and absolutely free, so please, join our community today!

Automatic fstab update scripts

Stuff that has not yet gone into the official build.

Automatic fstab update scripts

Postby dzz » Sun Jan 29, 2012 1:51 pm

Configuring fixed-disk mounts in /etc/fstab, whether in a live session, a new install, when partition layouts have changed or when a new disk is added can be tedious and error-prone.

It is possible to use pmount (that's a good option for a live session) but most of us probably prefer fixed mounts in fstab.

An old script, probably originating from early Knoppix days, has been resurrected and updated to automate the job. It will optionally append to fstab or do a complete rebuild. Can be used for a "proper install" or a live session, persistent or not.

A hurdle before was what usb devices to include, that might be already mostly sorted and automatic.

There are two variants for now, one to be called manually from a root shell (configure-mounts.sh) and one for use as an automatic "live-hook" (configure_live_mounts) They are not refracta-specific and should be good for any Debian system (although something new in sid might break them)

Near the top of the script are some user-editable mount options.

This is experimental, so far only tested on the machines that exist here so some help is needed to test in different environments. The script should automatically do a fstab backup but best do one manually as well, till it's tested a bit more.

http://exe-linux.fastfishwebsolutions.c ... re-mounts/

BTW there is xfce4-mount-plugin available (but it sees only what is in fstab)
dzz
 
Posts: 647
Joined: Wed Apr 27, 2011 11:53 am
Location: Devon, England

Re: Automatic fstab update scripts

Postby fsmithred » Sun Jan 29, 2012 4:07 pm

I had the xfce-mount-plugin in the previous version of refracta, and I couldn't remember why I took it out, other than the fact that I never used it. Thanks for the reminder.

I ran configure-mounts.sh from a live usb running the 64-bit refracta. First test was with internal sata drive. All partitions on the internal drive got mounted and entered into fstab. Second test, I added an ide drive that has some encrypted partitions, and I added an error log to the script. All except the encrypted partitions were handled correctly, and the only thing in the error log was a mount error for "unknown filesystem type 'crypto_LUKS'. Not a surprise to you, I'm sure. For both tests, I did not wipe fstab.

Nice work!

Here's a piece I wrote last night to unmount anything that got mounted in /media. You can run it as user if you haven't disabled sudo.
Code: Select all
#!/usr/bin/env bash

unmountables=$(mount | sort | awk '/media/ { print "FALSE\n" $3 }' |  zenity --list --checklist --multiple --column ' ' --column 'Mounted' --height=200)

unmounts=$(echo "$unmountables" | sed 's/|/ /g')

sudo umount $(echo "${unmounts[@]}")


exit 0


Edit: Tried it again with a couple extra usb sticks plugged in. It works with my grub2 usb refracta, and it gracefully skips the encrypted partition on that stick. It hangs when it tries to figure out what to do with a stick that's been imaged with a debian netinstall iso.
User avatar
fsmithred
 
Posts: 2101
Joined: Wed Mar 09, 2011 9:13 pm

Re: Automatic fstab update scripts

Postby fsmithred » Tue Jan 31, 2012 2:10 pm

I added a zenity window to configure-mounts_0.02.sh so that you can choose which volumes to add to fstab. See lines 287-295. This is just a sample, and it only displays the linux partitions. Separate zenity windows will be needed for fat and ntfs partitions.

Synopsis of what I did. $LINUXPARTS needs to be an array so it'll work correctly in the for loop. $linuxparts populates the zenity list. The zenity list puts all the checked items on one line (in the variable) and the items are separated with pipes. The pipes get replaced with spaces, then the array gets used in the for loop. (echo the array puts the items on separate lines again.)

Seems to work correctly. It's here -
http://paste.debian.net/154163/

Edit: Oh yeah, I keep getting this error - "line 274: too many arguments." That's the first line of this piece
Code: Select all
if [ "$ROOTPARTDEV" = /dev/${REMOVABLE1}[0-9] ]; then
REMOVABLE1=include_usb
fi


Edit2: Here's the section I edited, posted for when the pastebin disappears in a couple days.
Code: Select all
### SORT LINUX PARTITIONS #######################################################################################################

# grep out swap root boot home and live-mount partitions # divert fdisk warnings to /dev/null

# this will not work if $ROOTPART $BOOTPART $HOMEPART $LIVEMOUNT $LIVERW are blank !
linuxparts=$(/sbin/fdisk -l  2>/dev/null| grep -i 'linux'| grep -v 'swap'| grep -v "$ROOTPART"| grep -v "$LIVEMOUNT"| grep -v "$LIVERW"|grep -v "$BOOTPART"| grep -v "$HOMEPART"| grep -v "$REMOVABLE1"| sed 's:/dev/::'g| awk '{print "TRUE\n" $1}' \
  | zenity --list --title="/linux partitions" --text="De-select any partitions you don't want added to fstab." \
--checklist --multiple --column ' ' --column 'Partitions' --height=380 --width=150)


LINUXPARTS=$(echo "$linuxparts" | sed 's/|/ /g')

# might need to make this an array.
for PART in $(echo "${LINUXPARTS[@]}"); do
   # see if it's NOT in fstab
   if [[ ! `grep -w '$PART' $FSTAB` ]]; then
User avatar
fsmithred
 
Posts: 2101
Joined: Wed Mar 09, 2011 9:13 pm

Re: Automatic fstab update scripts

Postby fsmithred » Tue Jan 31, 2012 6:24 pm

http://paste.debian.net/154194/

Added similar code for FAT and NTFS partitions. Also added tests to see if Linux, FAT or NTFS appears in the output of 'fdisk -l'. If any of those filesystem types are not present on the system, then the corresponding zenity window won't come up. This eliminates a problem of empty lists popping up.

I did nothing with the HFS section, since it hasn't been tested at all.
User avatar
fsmithred
 
Posts: 2101
Joined: Wed Mar 09, 2011 9:13 pm

Re: Automatic fstab update scripts

Postby dzz » Wed Feb 01, 2012 11:13 am

Tests fine like that on Debian/refracta/xfce. Great work, thanks, I got those versions from pastebin. I am not very well-up on bash arrays but can see that is a neat way of incorporating the dialog.

Unfortunately in my Trinity Desktop installs it plays havoc and locks the session (script does complete), the old problem of GUI stuff (zenity) as root. I don't know if that will affect kde4 or others, it might but I don't use them. Maybe gksu/kdesu/dbus-launch or similar wrapper would fix it but make things more complex. Would using "dialog" be better, at least to keep it all in the terminal and independent of X? An "advanced-mode" similar to snapshot/installer?

I have been using variations of this script for quite some time, the basic idea is useful but it has grown larger than intended. It is now almost unrecognisable from the old script it was based on, I can't remember how even some bits I had to reinvent and wrote myself work! Still, it is open to do whatever makes it more functional and reliable including individual needs.

It was originally meant only for post-install use only but we already have it working for live usb with normal partition layouts. Someone in irc mentioned a problem if used with a "live-usb-image" type which emulates a cd, not had time to investigate that yet.

I am doing particularly well with the "automatic" non-interactive version in a full-persistence live usb as a "live-hook". Each startup the fstab gets built from scratch, in tandem with the default live-boot stuff, no problem when plugged to different machines.

It apparently just ignores encrypted partitions withot error but I know little about those. Presumably a user would set encryption up manually. However if the script is used to rebuild rather than just append fstab entries there is no mechanism as yet to retain custom lines for encrypted parts.
dzz
 
Posts: 647
Joined: Wed Apr 27, 2011 11:53 am
Location: Devon, England

Re: Automatic fstab update scripts

Postby fsmithred » Thu Feb 02, 2012 3:18 am

Using dialog might be a good idea for portability. I've never used it, but I think I'll look into it.

Entries in fstab for encrypted partitions could be identified by the presence of the word, "mapper". I haven't looked at how the script handles preserving lines, or if it even does that. Seems like it should be easy to keep any encrypted partitions that are already listed. Listing ones that aren't already in fstab is a little trickier, and I haven't really worked on that problem yet.

Two hours later...
These would be good for a live session. They just mount and unmount encrypted volumes. They don't deal with lvm at all.

Code: Select all
#!/usr/bin/env bash
# mount-crypto.sh
# mounts luks-encrypted partitions
# Copyright 2012 [email protected]
# License: GPL-3


# Check for root
[[ $(id -u) -eq 0 ]] || { echo -e "\t You need to be root!\n" ; exit 1 ; }


# Record errors in a logfile.
error_log="./errors_mountcrypto"
exec 2>"$error_log"



cryptvols=$(blkid | awk -F: '/crypto/ { print $1 }')


mount_crypto () {
   while true; do
      echo -n "
Create a label for $cvol.
The label will be used for the mapper name and the mountpoint for this volume.
"
      read ans
      label="$ans"
      mountpoint="/media/$label"
      if [[ -d "$mountpoint" ]]; then
         echo "$mountpoint already exists!"
         sleep 3
      else
         mkdir -p "$mountpoint"
      fi
      break
   done

#   echo -n "
# Give the passphrase to open and mount $cvol.
# "
   cryptsetup luksOpen "$cvol" "$label"
   mount /dev/mapper/"$label" "$mountpoint"
   echo " $label was mounted at $mountpoint"
}


for i in $(echo "$cryptvols"); do
   cvol="$i"
   mount_crypto
done



exit 0


Code: Select all
#!/usr/bin/env bash
# unmount-crypto.sh
# Copyright 2012 [email protected]
# License: GPL-3


# Check for root
[[ $(id -u) -eq 0 ]] || { echo -e "\t You need to be root!\n" ; exit 1 ; }


# Record errors in a logfile.
error_log="./errors_unmountcrypto"
exec 2>"$error_log"


for i in $(ls -1 /media); do
   if [[ -h /dev/mapper/"$i" ]]; then
      umount /media/"$i"
      cryptsetup luksClose /dev/mapper/"$i"
      rmdir /media/"$i"
      echo "$i unmounted and closed."
   fi
done

exit 0
User avatar
fsmithred
 
Posts: 2101
Joined: Wed Mar 09, 2011 9:13 pm

Re: Automatic fstab update scripts

Postby dzz » Thu Feb 02, 2012 11:18 am

Preserving needed existing lines: for now is done around line 213 (configure-mounts_0.2.sh), before (re-)processing and appending:

Code: Select all
egrep -i "information|dump|$ROOTPART|$ROOTPARTUUID|cdrom|swap|boot|home|var|proc|aufs|tmpfs" $FSTAB  >/tmp/fstab-temp && cp -a /tmp/fstab-temp $FSTAB && rm -f /tmp/fstab-temp

Other keywords, e.g. "mapper", could be tried there

I also forgot that "(/dev/)cdrom" could be listed as "/dev/sr0" or "/dev/scd0" (or there might be more than one) that could be changed to "iso9660" which would surely be on the line(s)

More stuff untested in a real situation: seperate home, boot and swapfile. It looks as it should work but none of those exist here at the moment

BTW: watch out using awk on blkid output, the columns don't always seem to order as expected!
dzz
 
Posts: 647
Joined: Wed Apr 27, 2011 11:53 am
Location: Devon, England

Re: Automatic fstab update scripts

Postby dzz » Thu Feb 02, 2012 6:49 pm

Updated script configure-mounts_0.3.sh at same url. Only these changes as yet:

(hopefully)
Fixed script hang if isohybrid or similar is present (fdisk gives extra output for them, which needed discarding)
Check for and exclude a second removable, if plugged

(total rebuild mode):
existing fstab lines including "mapper" should be kept in (encrypted parts) but can't test here
existing fstab lines including "iso9660" should be kept in

Still is experimental and open to more changes/improvements including fsr's extras!
dzz
 
Posts: 647
Joined: Wed Apr 27, 2011 11:53 am
Location: Devon, England

Re: Automatic fstab update scripts

Postby fsmithred » Thu Feb 02, 2012 7:12 pm

I tried configure-mounts_0.2.sh on another system after adding "mapper" to line 213. Told it to clear fstab, and it preserved lines for /boot, swapfile, nfs mount, and lines with /dev/mapper. When the encrypted volumes were open, it made lines for the raw partitions. When the encrypted volumes were closed, they were skipped. In all cases, the program hung on the last partition (sdb3, one of the encrypted parts.) I forgot to add lines for an error log, but I'll bet it hung while trying to mount one of the encrypted parts.
User avatar
fsmithred
 
Posts: 2101
Joined: Wed Mar 09, 2011 9:13 pm

Re: Automatic fstab update scripts

Postby dzz » Fri Feb 03, 2012 12:44 am

The very last chunk of the script is simply "mount -a", maybe encrypted parts don't like that. That could easily change to be more selective.

As for ("raw partitions" lines of) encrypted volumes, don't know enough about them yet, at least till some get set up here and I can take a look what happens...
dzz
 
Posts: 647
Joined: Wed Apr 27, 2011 11:53 am
Location: Devon, England

Next

Return to Experimental

Who is online

Users browsing this forum: No registered users and 0 guests

cron
suspicion-preferred