Skip to content

Instantly share code, notes, and snippets.

@thugcee
Created March 31, 2019 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thugcee/d2b6c866e695d08079c38ce196089256 to your computer and use it in GitHub Desktop.
Save thugcee/d2b6c866e695d08079c38ce196089256 to your computer and use it in GitHub Desktop.
create, format and mount lvm volume for lxc container
#!/bin/bash
VGROUP=lxcGuestsShared
name_regex="^[a-zA-Z0-9_]+$"
size_regex="^[0-9]+G$"
usage() {
echo -e "$1\nUsage: $0 <NAME> <SIZE>\n\t\
where:\tNAME - the nameof the new container (should match '$name_regex')\n\
\t\tSIZE - size for lvmcreate\n\
\texample: $0 myMachine 20G" 1>&2
exit 1
}
if (( $# != 2 )) || [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
usage
fi
[[ $1 =~ $name_regex ]] || usage "ERROR: Bad container name; it shoud match '$name_regex'\n"
[[ $2 =~ $size_regex ]] || usage "ERROR: Bad container size; it shoud match '$size_regex'\n"
NAME=$1
SIZE=$2
set +x
CONTAINER_PATH=/var/lib/lxc/$NAME
test ! -e "$CONTAINER_PATH" || usage "ERROR: container already exists; path: $CONTAINER_PATH\n"
echo lvcreate -L "$SIZE" -n "$NAME" "$VGROUP"
echo mkfs.ext4 -L "$NAME" "/dev/$VGROUP/$NAME"
echo mkdir "$CONTAINER_PATH"
echo echo -e "\"/dev/$VGROUP/$NAME\t$CONTAINER_PATH\text4\terrors=remount-ro\t0\t0\"" \>\> /etc/fstab
echo mount "$CONTAINER_PATH"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment