分类: LINUX
2011-05-04 17:20:36
In this example we have a /dev/sda1 partition with about 6GB of size, and right behind that partition we have about 4GB of unused space. We want to add those 4GB of unused spave to our /dev/sda1 partition (this doesn't work of these 4GB don't come right behind our /dev/sda1 partition, but are elsewhere on the hard disk!).
First, we collect some details again about our current partitioning:
df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 6047868 4224140 1639408 73% /
varrun 95480 132 95348 1% /var/run
varlock 95480 0 95480 0% /var/lock
udev 10240 52 10188 1% /dev
devshm 95480 0 95480 0% /dev/shm
lrm 95480 17580 77900 19% /lib/modules/2.6.17-10-generic/volatile
df -B 4k
Filesystem 4K-blocks Used Available Use% Mounted on
/dev/sda1 1511967 1056035 409852 73% /
varrun 23870 33 23837 1% /var/run
varlock 23870 0 23870 0% /var/lock
udev 2560 13 2547 1% /dev
devshm 23870 0 23870 0% /dev/shm
lrm 23870 4395 19475 19% /lib/modules/2.6.17-10-generic/volatile
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 5.8G 4.1G 1.6G 73% /
varrun 94M 132K 94M 1% /var/run
varlock 94M 0 94M 0% /var/lock
udev 10M 52K 10M 1% /dev
devshm 94M 0 94M 0% /dev/shm
lrm 94M 18M 77M 19% /lib/modules/2.6.17-10-generic/volatile
fdisk -l
Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 789 6337611 83 Linux
/dev/sda2 1248 1305 465885 5 Extended
/dev/sda5 1248 1305 465853+ 82 Linux swap / Solaris
fdisk -s /dev/sda1
6337611
Then we shut down the system and boot into our Knoppix Live-CD (or your rescue system) (if the partition you want to resize doesn't hold any system files, you can do everything from the original system; the steps are the same, just omit booting into Knoppix/your rescue system).
shutdown -r now
After Knoppix has booted, open a terminal and become root by running
su
/dev/sda1 should be unmounted by default, but you can run
umount /dev/sda1
to go sure.
Then run
fsck -n /dev/sda1
fsck 1.38 (30-Jun-2005)
e2fsck 1.38 (30-Jun-2005)
/dev/sda1: clean, 159036/765536 files, 1080014/1536000 blocks
Next we remove the journal from /dev/sda1, thus turning it into an ext2 partition:
tune2fs -O ^has_journal /dev/sda1
The output looks like this:
tune2fs 1.38 (30-Jun-2005)
Now we use fdisk to delete our current /dev/sda1 partition and create a bigger one (don't be afraid, no data will be lost):
fdisk /dev/sda
(Yes, it's /dev/sda, not /dev/sda1.)
The number of cylinders for this disk is set to 1305.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
(e.g., DOS FDISK, OS/2 FDISK)
Type m to get a list of all commands:
Command (m for help): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Let's print out the partition table:
Command (m for help): p
Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 789 6337611 83 Linux
/dev/sda2 1248 1305 465885 5 Extended
/dev/sda5 1248 1305 465853+ 82 Linux swap / Solaris
Now we delete partition no. 1 (/dev/sda1):
Command (m for help): d
Partition number (1-5): 1
Next we create a new /dev/sda1 partition. It was a primary partition before, so we choose p again, and again it is our partition no. 1:
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
p
Partition number (1-4): 1
Now we must specify the first and the last cylinder of our new /dev/sda1 partition. We know the first cylinder, can can take it from the fdisk -l output before:
First cylinder (1-1305, default 1): 1
Now fdisk tells us the highest possible cylinder of our new partition (1247 in this example), so we simply enter this number:
Last cylinder or +size or +sizeM or +sizeK (1-1247, default 1247): 1247
Let's print out our new partition table:
Command (m for help): p
Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 1 1247 10016496 83 Linux
/dev/sda2 1248 1305 465885 5 Extended
/dev/sda5 1248 1305 465853+ 82 Linux swap / Solaris
Our original /dev/sda1 had the bootable flag (see the fdisk -l output from the beginning of this chapter), so we must add it to our new /dev/sda1 again:
Command (m for help): a
Partition number (1-5): 1
Now let's write our new partition table and exit fdisk:
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table.
The new table will be used at the next reboot.
Syncing disks.
Now we reboot the system, and again we boot into our Knoppix system (rescue system; original system if resized partition doesn't hold system files):
shutdown -r now
Become root again (on Knoppix run
su
)
Then run
e2fsck -f /dev/sda1
Now we must resize the file system in our /dev/sda1 partition. If we don't specify a size for the resize2fs command, it will assume the biggest possible size so we don't have to calculate. So we run
resize2fs /dev/sda1
The output looks like this:
resize2fs 1.38 (30-Jun-2005)
Resizing the filesystem on /dev/sda1 to 2504124 (4k) blocks.
The filesystem on /dev/sda1 is now 2504124 blocks long.
Next we run
fsck -n /dev/sda1
fsck 1.38 (30-Jun-2005)
e2fsck 1.38 (30-Jun-2005)
/dev/sda1: clean, 159036/1254176 files, 1062544/2504124 blocks
and create the journal on /dev/sda1, thus turning it into an ext3 partition again:
tune2fs -j /dev/sda1
tune2fs 1.38 (30-Jun-2005)
Creating journal inode: done
This filesystem will be automatically checked every 30 mounts or
0 days, whichever comes first. Use tune2fs -c or -i to override.
Now we are done. Shut down the system and boot into the original system:
shutdown -r now
If everything goes well, the original system will boot up, and no data has been lost. Now we can gather some details about our new partitioning and compare them with the information we collected at the beginning of this chapter:
df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 9859036 4224032 5234348 45% /
varrun 95480 132 95348 1% /var/run
varlock 95480 0 95480 0% /var/lock
udev 10240 52 10188 1% /dev
devshm 95480 0 95480 0% /dev/shm
lrm 95480 17580 77900 19% /lib/modules/2.6.17-10-generic/volatile
df -B 4k
Filesystem 4K-blocks Used Available Use% Mounted on
/dev/sda1 2464759 1056008 1308587 45% /
varrun 23870 33 23837 1% /var/run
varlock 23870 0 23870 0% /var/lock
udev 2560 13 2547 1% /dev
devshm 23870 0 23870 0% /dev/shm
lrm 23870 4395 19475 19% /lib/modules/2.6.17-10-generic/volatile
df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 9.5G 4.1G 5.0G 45% /
varrun 94M 132K 94M 1% /var/run
varlock 94M 0 94M 0% /var/lock
udev 10M 52K 10M 1% /dev
devshm 94M 0 94M 0% /dev/shm
lrm 94M 18M 77M 19% /lib/modules/2.6.17-10-generic/volatile
fdisk -l
Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 1247 10016496 83 Linux
/dev/sda2 1248 1305 465885 5 Extended
/dev/sda5 1248 1305 465853+ 82 Linux swap / Solaris
fdisk -s /dev/sda1
10016496
========================================================================================================
xen虚拟机在创建虚拟磁盘文件后,如果初始大小设置不合理,后期维护时可能会遇到磁盘使用空间不足的情况,所以需要对虚拟磁盘文件进行扩容,以增加磁盘使用空间。
xen虚拟机的分区方式主要有两种:1.直接在虚拟机磁盘上创建分区表。2.先在虚拟机上创建LVM(Logical Volume Manager )系统,再在LVM上面建立虚拟分区。对于第一种情况,网上相关的资料较多,这里主要讨论的是第二种情况的虚拟机磁盘扩容方法。
操作步骤:
1. 关闭虚拟机
$ xm shutdown myvm1.faladay.cublog.cn
2. 增加虚拟磁盘文件空间(4G)
$ dd if=/dev/zero bs=1024k count=4096 >> /home/myvm/myvm1.faladay.cublog.cn.img
3. 重新创建虚拟磁盘分区表
$ losetup -f
/dev/loop0
$ losetup /dev/loop0 /home/myvm/myvm1.faladay.cublog.cn.img
$ losetup -a
/dev/loop0: [fd01]:10977283 (/home/myvm/myvm1.faladay.cublog.cn.img)
$ fdisk /dev/loop0
下面是fdisk界面的输入和输出信息(摘要):
Command (m for help): p
Device Boot Start End Blocks Id System
/dev/loop0p1 * 1 6 48163+ 83 Linux
/dev/loop0p2 7 764 6088635 8e Linux LVM
Command (m for help): d
Partition number (1-4): 2
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (7-1281, default 7):
Using default value 7
Last cylinder or +size or +sizeM or +sizeK (7-1281, default 1281):
Using default value 1281
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)
Command (m for help): p
/dev/loop0p1 * 1 6 48163+ 83 Linux
/dev/loop0p2 7 1281 10241437+ 8e Linux LVM
Command (m for help): w
4. 以root用户登录虚拟机
$ xm create myvm1.faladay.cublog.cn
$ ssh root@myvm1.faladay.cublog.cn
5. 修改LVM物理分卷大小
$ pvscan
PV /dev/xvda2 VG sys lvm2 [5.75 GB / 0 GB free]
Total: 1 [5.75 GB] / in use: 1 [5.75 GB] / in no VG: 0 [0 ]
$ pvresize +4096M /dev/xvda2
$ pvscan
PV /dev/xvda2 VG sys lvm2 [9.75 GB / 3.97 GB free]
Total: 1 [9.75 GB] / in use: 1 [9.75 GB] / in no VG: 0 [0 ]
$ lvscan
ACTIVE '/dev/sys/root' [4.00 GB] inherit
ACTIVE '/dev/sys/home' [1.28 GB] inherit
ACTIVE '/dev/sys/swap' [512.00 MB] inherit
$ lvextend -L +1024M /dev/sys/root
$ lvextend -L +3072M /dev/sys/home
$ resize2fs -f /dev/sys/root
$ resize2fs -f /dev/sys/home
6. 退出虚拟机,执行下列命令
$ losetup -d /dev/loop0
至此,虚拟机磁盘扩容完毕,通过df -h命令可以看到虚拟机磁盘空间root已经扩大1G,home已经扩大3G。