I have a Live CD that I am creating that will access all physically attached disks on a machine. There is no user intervention involved and I need to find and mount all drives that can be mounted. I also need to be able to handle if the disk or disks are using LVM or not and mount each volume group.
2 Answers
Here is what I came up with to find and mount all disks available. This script will also keep track of all failed mounts (which I needed in my situation as well, not part of the question but could be useful to others).
Here I scan all partitions in /proc/partitions
and attempt to mount each partition I find. LVM partitions will be listed in this file as well after scanning and activating them.
# Scan for all volume groups
lvscan
# Activate all volume groups
vgchange -a y
# Get all partitions (-n+3 skips first 3 lines since they do not contain partitions)
# Also skip partitions that are loop devices which is actually the ISO cd itself
all_partitions=$(tail -n+3 /proc/partitions | awk '{print $4}' | grep -v loop)
# Array of failed mounts
declare -a failed_mounts=()
# Mount each partition to /mnt/{partition name}
for partition in ${all_partitions}; do
mountdir=/mnt/${partition}
mkdir -p ${mountdir}
mount /dev/${partition} ${mountdir} &>>${INIT_LOG}
if [ $? -ne 0 ]; then
echo "Failed to mount ${partition}"
rm -rf ${mountdir}
failed_mounts+=(${partition})
fi
done
This may or may not be dependent on the distro but since I am integrating this into a live CD it does not have to be distro independent.
Only the LVM part
Activate all volume groups first:
vgchange -a y
Then
lvdisplay -c | sed -e 's/ //; s/:.*//'
should give you a list of the activated LVM volumes. They should be of the form /dev/VGNAME/LVNAME
, you can now use this to create mountpoints as you wish.
-
Thanks but this is a somewhat incomplete answer. I also need to be able to figure out which disks are possible to mount.– bpedmanCommented Jan 11, 2013 at 17:39
-
edit: For instance, what other disks are there that are not necessarily using LVM that I need to mount as well?– bpedmanCommented Jan 11, 2013 at 17:46