LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-15-2021, 02:15 PM   #1
Danielb630
LQ Newbie
 
Registered: Mar 2021
Posts: 3

Rep: Reputation: Disabled
Script that mount LVM


hey guys, im trying to write a script that gets a .csv file and creates lvm's and mount points , mount the lvm to the mount points and writing into /etc/fstab,
but it seems that the for loop isnt working for me and it prints insted of creating my
variables , can someone give it a look and tell me where are my mistakes?

the .csv file
Code:
ProductsLV, /products
OracleLV, /products/Oracle
PlsqlLV, /products/plsql
my script:
Code:
#!/bin/bash


for i in 'cat list.csv'
do
    lv_name1='echo $i | cut -d, -f1 | sed '1!d''
    lv_name2='echo $i | cut -d, -f1 | sed '2!d''
    lv_name3='echo $i | cut -d, -f1 | sed '3!d''
    mount_point1='echo $i | cut -d, -f2 | cut -c 2- | sed '1!d''
    mount_point2='echo $i | cut -d, -f2 | cut -c 2- | sed '2!d''
    mount_point3='echo $i | cut -d, -f2 | cut -c 2- | sed '3!d''
    vg_name='centos'
        
        lvcreate -L 10m -n $lv_name1 $vg_name
	lvcreate -L 10m -n $lv_name2 $vg_name
	lvcreate -L 10m -n $lv_name3 $vg_name
	
	mkdir -p $mount_point1
	mkdir -p $mount_point2
	mkdir -p $mount_point3
	
	mount -t ext4 /dev/$vg_name/$lv_name1 $mount_point1
	mount -t ext4 /dev/$vg_name/$lv_name2 $mount_point2
	mount -t ext4 /dev/$vg_name/$lv_name3 $mount_point3
	mount -a
	
	echo "/dev/$vg_name/$lv_name1 $mount_point1 ext4 defaults 0 0">>/etc/fstab
	echo "/dev/$vg_name/$lv_name2 $mount_point2 ext4 defaults 0 0">>/etc/fstab
	echo "/dev/$vg_name/$lv_name3 $mount_point3 ext4 defaults 0 0">>/etc/fstab
done
 
Old 03-15-2021, 08:27 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
Welcome to LinuxQuestions.

You need to understand loops and how reading lines from a file works.

https://www.cyberciti.biz/faq/unix-h...ine-from-file/

Is this homework?
 
Old 03-15-2021, 09:41 PM   #3
computersavvy
Senior Member
 
Registered: Aug 2016
Posts: 3,345

Rep: Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484Reputation: 1484
First, as has been said you need to understand reading from a file and loops.

In your script you use the same line read and process it 3 different times, creating 3 different LVs with each.

Since you read one line, process that one line each time through the loop. The second time through the loop you process the second line, etc.

You also need to understand how LVM names and accesses LVs.
Although it is possible to mount things like you did, the standard way of referencing an LV for mounting is like any other partition.
"mount /dev/mapper/$vgname-$lvname /mount/point"
Why are you creating an lv for what appears to be a simple mount point for the second and 3rd LVs? Will it also contain data or is it a simple mount point?

After the LV is created it needs to be formatted with a file system before mounting.
"mkfs.ext4 /dev/mapper/$vgname-$lvname"
After the first LV is created and mounted, before you create the mount point for the second and subsequent LVs (inside it) you should make certain the first is actually mounted so the rest of the structure creation is done properly

The processes you are performing requires that this script be executed with root (sudo) privledges.

CSV files use "," for separators; not ", " (comma-space)
cut does quite nicely for taking part of a line. No need for multiple cuts or sed on those simple lines.
 
Old 03-16-2021, 12:27 AM   #4
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
One mistake is illustrated by this line:
Code:
for i in 'cat list.csv'
Your intention was to use backquotes instead of normal quote characters. Hard to see the mistake, since they look so similar. Better use $(cat list.csv). You make the same mistake repeatedly in the loop body.

Next, the for loop doesn't assign entire lines to $i, but words. For this reason, your echo | cut pipelines won't work.

Finally, you create logical volumes, but you don't format them as filesystems. Thus, they can't be mounted.

And I don't understand what the sed commands are for.
 
Old 03-16-2021, 03:10 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,849

Rep: Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309Reputation: 7309
additionally you might want to try shellcheck.
Code:
$ shellcheck myscript
 
Line 4:
for i in 'cat list.csv'
^-- SC2034: i appears unused. Verify use (or export if used externally).
         ^-- SC2041: This is a literal string. To run as a command, use $(..) instead of '..' .
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script that mount LVM Danielb630 Linux - Newbie 1 03-17-2021 08:53 AM
mount multiple lvm to single mount point mplike Linux - Software 3 03-02-2010 04:43 AM
Mount. Umount. Mount. Umount. Mount. Umount. Mount.. cwizardone Slackware 10 03-22-2007 09:30 AM
mount refusing to mount lvm volumes linuxmandrake Fedora 2 07-20-2005 06:06 PM
To LVM or not to LVM? illtbagu Linux - Software 3 12-29-2003 04:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration